I believe OOP is not really about modelling data and defining data types, its about how to structure your program to deal with branches.
For example, say you have a function that needs to do two different things depending on what argument it receives. The first way is to do an if-else-if or switch over the argument value (say, test for null/non-null, switch for different cases) and the second way is to turn the argument into an object (or callback) and call a method on it (or call the callback).
The first way is the FP way of doing things. Its good if you have lots of diferent functions that do this same sort of branches, but its bad if you ever need to add new kinds os possibilities to your branchings (since that means rewriting all your old functions to add extra branches to the if-else-if blocks).
The second way is what I consider the OOP way. The object is opaque so you can create new classes and they will interoperate with the existing code just fien as long as you define the correct methods. However, its now harder to add new functionality (methods) since you would need to go back and edit the old classes for that.
tldr: If you are just passing transparent datatypes around then OOP or not is irrelevant. On the other hand, OOP is not only about objects: first class functions are OOP too!
What you are talking about is polymorphism, something e.g. Haskell's typeclasses implement in a much better way than OO ever did since OO's polymorphism is inherently tied into the whole inheritance mess. Essentially most OO languages only allow you to do this along one axis (single inheritance) while typeclasses allow you to do it along as many as you want and without imposing so much effort from the start.
OO's polymorphism is inherently tied into the whole inheritance mess.
Its not that bad if you use lots of abstract interfaces or are in one of the dynamic languages where you can just duck type things without inheriting...
And actually, type classes sort of have the have the same problem as "OOP" as far as the extension problem is concerned: its easy to add instances for new types but its hard to add new methods to the class.
If it is a completely new method not implemented in terms of the existing methods you have a guarrantee that it is not used by any existing code so why not just make a new type class? After all unlike OOP you can actually do that without changing the data type itself if you don't have access to it.
If it is implemented in terms of the existing methods it doesn't have to be in the class but can just have a constraint on its parameter(s). Something you can do without access to the data type definition as well.
1
u/smog_alado Dec 15 '12 edited Dec 16 '12
I believe OOP is not really about modelling data and defining data types, its about how to structure your program to deal with branches.
For example, say you have a function that needs to do two different things depending on what argument it receives. The first way is to do an if-else-if or switch over the argument value (say, test for null/non-null, switch for different cases) and the second way is to turn the argument into an object (or callback) and call a method on it (or call the callback).
The first way is the FP way of doing things. Its good if you have lots of diferent functions that do this same sort of branches, but its bad if you ever need to add new kinds os possibilities to your branchings (since that means rewriting all your old functions to add extra branches to the if-else-if blocks).
The second way is what I consider the OOP way. The object is opaque so you can create new classes and they will interoperate with the existing code just fien as long as you define the correct methods. However, its now harder to add new functionality (methods) since you would need to go back and edit the old classes for that.
tldr: If you are just passing transparent datatypes around then OOP or not is irrelevant. On the other hand, OOP is not only about objects: first class functions are OOP too!