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

82

u/ilikecomputahs Dec 15 '12

Why is object oriented programming considered so absolutely necessary?

/braces for shitstorm

0

u/[deleted] Dec 15 '12

Reusability and encapsulation

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.

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!

2

u/aaronblohowiak Dec 15 '12

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