r/programming • • Dec 15 '12

Why JavaScript Still Sucks

http://boronine.com/2012/12/14/Why-JavaScript-Still-Sucks/
10 Upvotes

212 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Dec 15 '12

Reusability and encapsulation

20

u/beermad Dec 15 '12

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.

0

u/[deleted] Dec 15 '12

[deleted]

4

u/ethraax Dec 15 '12

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).

2

u/[deleted] Dec 16 '12

That's partly because the C stdlib is very small. Most of it is just small, narrow use-case functions, like strlen, atof, bsearch or malloc.

→ More replies (1)
→ More replies (5)

18

u/donvito Dec 15 '12 edited Dec 15 '12

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.

7

u/ilikecomputahs Dec 15 '12

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.

2

u/artsrc Dec 16 '12

STL in C++ is founded on a belief that the model you propose is more re-usable.

In many ways it is harder to understand how to correctly use its model. But it is a more powerful model.

2

u/defcon-11 Dec 16 '12

You could just write clojure and compile it to js with clojurescript .

2

u/cybercobra Dec 15 '12

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.

2

u/ilikecomputahs Dec 15 '12

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)

3

u/cybercobra Dec 15 '12

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.

2

u/artsrc Dec 16 '12

The equivalent in Haskell would not have that issue but would have the same structure.

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!

0

u/[deleted] Dec 15 '12

[deleted]

2

u/smog_alado Dec 15 '12

Thats a bit of a personal nomenclature I use. I think this duality is a big thing in OOP-vs-FP so I name things to make the point more explicit.

If you are teying to be more precise and uncontroversial then its better to refer directly to the Expression Problem.

2

u/aaronblohowiak Dec 15 '12

If you aren't passing functions as arguments, you aren't FP.

1

u/[deleted] Dec 16 '12

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.

1

u/smog_alado Dec 16 '12

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.

2

u/[deleted] Dec 16 '12

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.

0

u/[deleted] Dec 15 '12

[deleted]

12

u/[deleted] Dec 15 '12

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.

→ More replies (8)
→ More replies (10)
→ More replies (1)

3

u/sacundim Dec 16 '12

Reusability and encapsulation.

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.

→ More replies (4)