2

It might be a wiki question, but still. Which approach conforms more to the Javascript spirit:

var Report = function(data) {
    var that = this;
    that.send = function() { ... };
};

var r = new Report(data); // create and validate the data
r.send(); // encode and send the data

or

var sendReport = function(data) {
    ...
    // create, validate, encode and send the data
    ...
};

The first approach seems to be more OO, the second more functional? In the first it might seem that the functions of Report are more testable (though for example we have only send() at this stage). Also, I like to see object of type Report in debugger when required to examine the state.

The second approach might be more "simple", but it seems to be less testable.

I am personally with the first approach, but the question is if I try to apply the OO "principles" in the "wrong" domain (please help me to word my question better).

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
BreakPhreak
  • 10,940
  • 26
  • 72
  • 108
  • The OO principle has nothing to do with the "new" operator. I develop all my classes with your second aproach. – some_coder Jul 18 '12 at 09:37
  • @some_coder The way I see it, the second one is a simple function, not a class. It takes data and sends it and then it's done. – Esailija Jul 18 '12 at 09:39
  • @Esailija in JS you dont really have "classes" like in other languages (like c++/java). the first approach is also just a function (you can see it as an functionpointer or so). the only difference is that you use "var x = new Report(data)" to create an instance. in the second approach you use just "var x = sendReport(data)" to create an instance. Or i just dont understand the question. – some_coder Jul 18 '12 at 09:42
  • @some_coder no I meant that you'd just do `sendReport(data)`. What is the purpose of `x` if you already sent the data? – Esailija Jul 18 '12 at 09:48
  • @Esailija ok then i got it the wrong way. i thought the second approach was just an other way of creating classes and instances and so on. but i guess it should show some functional approch. – some_coder Jul 18 '12 at 09:52
  • @some_coder there are indeed as many ways as there are programmers to realize the concept of a class in javascript :P – Esailija Jul 18 '12 at 09:55

5 Answers5

1

Your question doesn't actually seem to be about the new operator; it's more about whether you should use OO, or functional programming.

In this regard, OO is completely within the scope of JavaScript. You can see from the API (e.g. e.g.) that JavaScript is a very object orientated language.

HTML5 has taken this further and given you better control on your objects, by adding new methods to the Object object.

Touching on the use of new, and whether to avoid it or not, I'd point you to this excellent answer (and tell you to use new).

Community
  • 1
  • 1
Matt
  • 74,352
  • 26
  • 153
  • 180
  • Looking more carefully, this question looks to me to be more about "Do I make a class or just a function that does it all", which is not really about javascript. I can make a god function in any language. – Esailija Jul 18 '12 at 09:31
  • @Esailija: I'm not sure if it is? *the question is if I try to apply the OO "principles" in the "wrong" domain* – Matt Jul 18 '12 at 09:33
  • we have a lot of debates through code review, hence it's about the "proper" programming approach, am I trying to code C++/Java in Javascript? am I using or abusing the language - that is the question. again, hope that the wording is right. PS: @Esailija: I did found your (erased) answer very helpful and educative. – BreakPhreak Jul 18 '12 at 09:35
  • @BreakPhreak If my first comment is right and you are actually asking that, it might be off-topic since there is gonna be debate. If you are actually asking mechanics of javascript then I actually deleted my answer about that :P – Esailija Jul 18 '12 at 09:36
  • I was asking about the comparative analysis of both approaches (design, performance, testability etc), also about their validity. Still proud that I had an opportunity to read and educate from your answer :) – BreakPhreak Jul 18 '12 at 09:38
1

The way I see it now, is that the second one is a simple function that takes data and sends it. And you are wondering whether it is advantageous to make a class of it and break it into multiple methods.

So instead of this:

sendReport(data);

You would do:

var a = new Report(data);
a.send();

Where Report has 2 internal methods for validating and encoding. It would definitely be more testable since they would be 3 different functions instead of one that just does it all in one batch.

The performance is nothing to consider, it's 2 additional function calls and 1 additional object creation. You are probably not going to send reports millions and millions of times per second. Especially since it implies network I/O.

There is also nothing requiring you to construct a new instance, you could also do:

var report = {
    encode: function(){},
    validate: function(){},
    send: function( data ) {
        if( this.validate( data ) ) {
            data = this.encode(data);
        }
        else {
             //?
        }

        //send
    }
}

You could then gain the benefits mentioned above without having to construct new instance:

report.send(data);
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

It depends, but generally you want to go with the second when your code is small and migrate to the first as it gets larger. There's no point in making everything object-oriented, but when you have groups of functionality that make sense as an object, that's when you want to make objects.

Your second option is only less testable when single functions do too much. Otherwise it's very testable, just by varying the parameters to the function and seeing the results.

Anthony Mills
  • 8,676
  • 4
  • 32
  • 51
0

it's when you want to instantiate an object of the variable, works more with javascript objects, that when you can use the prototypes of js and also allows you to override methods and variables.

Prog Mania
  • 615
  • 9
  • 14
0

I don't think this really is about OO vs functional, as others have suggested.

The real question is whether a Report is a useful abstraction on its own. If it is -- i.e., if you want to perform other operations on it than just sending it right away -- then implementing an abstraction for it is useful. In OO, that would be a class or the equivalent (in JS, a constructor function). In functional languages, you typically introduce a module or an abstract data type in such cases.

If you don't need an abstraction, just use a single procedure/function.

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72