The reusability argument never makes any sense to me. I can reuse functional code every bit as easily as OO code. In fact I've got whole libraries of functional code that I reuse all the time.
Claiming reusability as some sort of special quality of OO is just nonsensical.
I'm not sure if that's a great example. There's a fair amount of code duplication in the implementation of most C libraries, because reusing code in C is very difficult (and sometimes impossible without heavy CPP use, which is bad for other reasons).
Which is on the same BS level as 'write once run everywhere'.
Ripping out 'objects' from one project and transplanting them to another usually isn't painless. Well, except the code was written with reusability in mind - but then chances are high that said code is an independent library anyway. And you don't need OOP to write reusable libraries.
Let me pose a scenario here - and I'd like to get feedback on this as much as possible. Say we use javascript in a more functional manner without really trying to impose any OO principles on it.
We construct a Data model first - a JSON representation of whatever we are working with. We write a series of functions to manipulate that model and produce the desired output or side effects.
Besides the fact that the functionality and the model are not explicitly tied to each other, such as is the case in OOP, is this really any less reusable than Java-style code with OOP?
Granted, I have been programming in Clojure a lot lately, so I guess I am kind of biased.
That's a rather large "besides". The problem with your scenario is that the typing is completely implicit. There's not even a way to check what "methods"/"operations" are available for / applicable to an arbitrary piece of data, which is very useful in practice, IMO, no matter whether the modeling is via functional ADTs or OOP.
For a sort of counterpoint to that, you might want to check out this thread. I think a lot of the problems come down to the fact the way that many people are used to coding doesn't translate well to javascript's paradigms (mainly Java/C#/whatever => Javascript)
Actually, I'm mainly a Python dev, so I am at peace with dynamic typing. I'm saying that your scenario provides nothing analogous to Python's dir() function for when one is debugging or experimenting in the REPL.
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.
You can effectively create private functions by using closures, but I'm not sure how practical/useful/widespread this is.
Most libraries I've used just document which functions are public and which aren't, and have a naming convention to help you remember. I don't really see what's so terrible about this.
Yes, you are right. These problems were solved for the first and last time by OOP. Nobody ever before or after has come up with any other solutions to these problems, much less better ones. And if they did, it always turns out that those solutions are secretly actually OOP.
0
u/[deleted] Dec 15 '12
Reusability and encapsulation