2

I had recently begun learning and implementing objects in JavaScript. But it made me think, if the functionality of an object can be put into one method, then why use multiple methods like Example.prototype.update = function() with Example.prototype.draw = function() if one of these methods were to not require arguments? I could just define and then call on one method like Example.prototype.do = function() and put all of the functionality the two methods had. It's easier and less code, but maybe it's better to have a structure in my object with several, specific methods? Thank you.

EDIT: I ask this about methods without parameters because if you grouped all of the functionality of several methods with parameters it would be too much tedious work inputting a dozen arguments into one function.

Thanks for downvoting!

Roman
  • 173
  • 2
  • 2
  • 12
  • 3
    if `.update` does **exactly the same** as `.draw` you have a point ... if they do different things, you're barking up the wrong tree ...not sure how the lack of parameters makes functions do the same thing, clearly they don't – Jaromanda X Aug 05 '16 at 04:44
  • [`Thing.do()`, really?](http://steve-yegge.blogspot.de/2006/03/execution-in-kingdom-of-nouns.html) – Bergi Aug 05 '16 at 04:46
  • @JaromandaX Different things? – Roman Aug 05 '16 at 04:46
  • 1
    @JaromandaX is correct. Consider the methods `.dump()` which requires no arguments and returns a dump of your object's state and `.delete()` which deletes all your object state. Both require no arguments but I'd consider code implementing both as a single function to be crazy code. – slebetman Aug 05 '16 at 04:47
  • 1
    The "update" and "draw" methods are separate to make it possible to call one of them without also calling the other. – hugomg Aug 05 '16 at 04:47
  • "*It's easier and less code*" - I highly doubt that. Please try it and post the results of both approaches for your actual code. – Bergi Aug 05 '16 at 04:48
  • @Bergi a bunch of `if/else if/else if` in a function called `do` is far easier to read than separate functions with meaningful names /sarcasm – Jaromanda X Aug 05 '16 at 04:49
  • @Roman perform different functions, i.e. if one reprograms your VCR and the other neuters your pet – Jaromanda X Aug 05 '16 at 04:50
  • @slebetman I see the point. What if I wanted to `.draw()` on a condition, I'd need it to be separate from the update function. – Roman Aug 05 '16 at 04:54
  • @Roman: Exactly. If they do different things then it makes sense to have them different. For the example you've given it's likely one of them (`draw`?) merely draws the graphic in the buffer without drawing to screen and the other draws to the buffer and then copy the buffer to screen. Blitting is an expensive process so it makes sense to draw as much as possible before blitting. It's like why `.flush()` exist for file-io API. – slebetman Aug 05 '16 at 04:57
  • OK no need for fancy terminology you've answered my question thank you very much. (I'm too beginner to understand blitting, file-io API, etc.) – Roman Aug 05 '16 at 05:01
  • @slebetman: I guess OP meant to `update` the application state, not the screen. – Bergi Aug 05 '16 at 07:00
  • @Bergi: We can only guess. Different API use different terms. In Tk for example `update` immediately enters the event loop synchronously and updates everything including drawing to screen (there's nothing similar in js). Whereas various `draw` functions merely updates the graphics buffer. So it depends on the API – slebetman Aug 05 '16 at 07:03
  • @Bergi No, I meant to `update()` the object, i.e. the position, velocity, etc. properties and another function `draw`s the updated object to the screen... – Roman Aug 05 '16 at 07:06
  • @Roman: That's exactly what application state means, yes. – Bergi Aug 05 '16 at 07:07

2 Answers2

3

There are different reasons:

Overview

In Clean Code: A Handbook of Agile Software Craftsmanship, Robert Martin says:

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that. Functions should not be 100 lines long. Functions should hardly ever be 20 lines long.

Along with that comes also the maintainability, I mean if the function is long, the human understanding of the coding begins to lack.

Reusability

The whole point about function is to reuse them. So imagine a function that calls another function of the same object several times. To merge it you potentially need to duplicate your coding.

Other Reasons

  1. Performance: Maybe you don't want to do all the things at the same time (maybe because it's time consuming or you only want to do it when you need it...) or you don't want to do some things at all?
  2. Logic: Some things just don't belong together like a drawing method and a erasing method. Just makes no sense to merge them to a single function.
  3. Encapsulation: Every function has it's own scope, so if you would only use one function you would probably mess up the scope
ScientiaEtVeritas
  • 5,158
  • 4
  • 41
  • 59
  • Could you explain how using one function would "mess up the scope"? – Roman Aug 05 '16 at 04:57
  • I talk about the scope of this single function. You declare variables and variables for loops inside of this function which would have all the same scope. Imagine ``var i = 0``, it's available in the whole function, you shouldn't use the variable a second time... – ScientiaEtVeritas Aug 05 '16 at 05:02
  • I see, having one function for everything would also be very limiting in this sense. – Roman Aug 05 '16 at 05:04
0

The accepted answer is correct, but I wanted to provide a more simplified answer as well.

The way you're thinking about combining the update() and draw() functions might seem like it makes sense because you always call one right after the other, but it won't make sense for 99.99% of other functions.

You can think of a function as telling the computer to do one thing (the definition of "one thing" is purposely very broad), and the whole point of programming is calling functions in different combinations to make the computer do something interesting. If you combined functions, you'd lose this ability.

Consider example functions named walkForward(), turnLeft(), and turnRight(). By calling these functions in different orders, you can tell your actor to walk any path you want.

If we applied your logic and combined those three functions into one walk() function, what exactly would that function do? How would we tell the actor to walk a specific path?

Even with the update() and draw() functions, it doesn't always make sense to call them one after the other. Maybe you want to run a simulation without drawing every frame, so you might call update() a bunch of times without calling draw(). Or maybe you want to pause the animation, so you'll stop calling update() and just call draw() instead. If you combined them into a single function, you'd lose this freedom.

Programming is all about freedom to tell the computer to do what you want, and combining functions takes this freedom away.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107