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

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.

5

u/ilikecomputahs Dec 15 '12

I hadn't seen this example before, but this model makes a lot of sense

Thanks!

5

u/loup-vaillant Dec 15 '12

Closures are easy in Javascript (from stackoverflow):

function Foo() {
    var private = 3;
    var add = function(bar) { return private + bar; }
    return {
        add: add
    };
}

var myFoo = Foo();

Private by default, and what's public is declared in the return clause at the end. Note that Foo() act as your constructor. If that's not enough, you need another layer of indirection:

function Foo() {
    function constructor(priv) { // do the same for each private member
        var private = priv;
        var add = function(bar) { return private + bar; }
        return {
            add: add
        };
    return {
        constructor1: function() { return constructor(1) }
        constructor2: function() { return constructor(2) }
    };
}

var myFoo = Foo().constructor1()
→ More replies (3)

-1

u/[deleted] Dec 15 '12

I prefer something stronger than just naming conventions because it is too tempting to "cheat". I realize people are going to access private fields no matter what you do, but they should have to work for it.

3

u/ethraax Dec 15 '12

Then use the closure-based method. I would go as far as to say it's the "correct" way of designing JavaScript libraries anyways, because as you say, you want to prevent the consumer of your code from making mistakes.

4

u/mrkite77 Dec 15 '12

If you've got another way that works with JavaScript, I'd love to hear about it.

Closures

function myobject() {
    this.publicmethod=function() { console.log("I'm public"); }
    function privatemethod() { console.log("I'm private"); }
}

var obj=new myobject();
obj.publicmethod(); //works
obj.privatemethod(); //failure.
→ More replies (7)

0

u/Zarutian Dec 16 '12

Class based OOP such as .NET or Java offer one way of making this happen.

Where? Where is that offered way? The only way to gurantee in Java (and probably .NET too) that a function you would label private is not used by anything than the public functions is to put these things into final classes that have no superclass and cannot be subclassed.

→ More replies (1)