7

Reading through Invoking a function without parentheses it is stated multiple times in the comments and answers to not use such code in production. Why please?

I am a beginner at JavaScript as you can guess from the question. If someone could phrase their answer in layman's terms that would be great, though please also counter for the experienced JS folks among you that might need a more detailed and technically detailed reply.

Examples of what could or does go wrong with using functions without parentheses in production would be a great addition to an answer.

Here is example code of invoking functions without parentheses taken from the answers to that question.

var h = {
  get ello () {
    alert("World");
  }
}

Run this script just with:

h.ello  // Fires up alert "world"

or

var h = {
  set ello (what) {
    alert("Hello " + what);
  }
}

h.ello = "world" // Fires up alert "Hello world"
lowtechsun
  • 1,915
  • 5
  • 27
  • 55
  • 3
    Better suited for the [programmers.se] site. – JJJ Mar 12 '16 at 13:30
  • 3
    If you're intentionally creating parentheses-free functions just for the sake of doing it, don't. If you have a valid reason, however, I see no reason *not* to. – Hatchet Mar 12 '16 at 13:30
  • 2
    Getters and setters are supposed to *only* get and set values, but the point of them is that the calling code uses them as if they were plain properties. – nnnnnn Mar 12 '16 at 13:41
  • 1
    Terms like `good` and `bad` are subjective values. If the code is only for you and you always code like this, then you will known the meaning of your own style. When working in a team, it is important to define and (strictly) follow some coding guidelines in order to avoid such questions resp. discussions resp. confusion. Confusion means loosing time to understand trivial code, which leads to frustration and prolonging time in projects often leads to higher costs. – keenthinker Mar 12 '16 at 13:51
  • @Juhana when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Mar 12 '16 at 18:47
  • @gnat & @ juhana I posted this here since the linked question is here as well. This I did so JS beginners like myself can also understand **why** it is a bad idea to use functions without parentheses. Though yes, I do understand and take in both your points. – lowtechsun Mar 12 '16 at 19:03

1 Answers1

7

It doesn't really matter whether it's in production or not.

It's because it doesn't look like a function call and that's confusing for other developers.

Imagine how difficult it might be to track down some complex behaviour in a large application when any property access and assignment could be calling out to some arbitrary code.

Although there are valid uses for getters, setters and eventually proxies, they are suited to very specific behaviours in small surface APIs. They probably shouldn't ever be used as a general programming technique.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • 3
    `...confusing for other developers.` - how true. Maintenance is time and thus money! – keenthinker Mar 12 '16 at 13:39
  • 3
    I agree with Dan - Revisiting code that you wrote a year or two previously can prove a challenge when program flow is less clear. It can be made more difficult for someone else to support your code. The performance advantages can be negligible, but the head aches can be mind numbing when you have nested loops inside functions and trying to implement an extra feature without breaking existing functionality. –  Mar 12 '16 at 13:39
  • 1
    The code we write today could be reviewed and maintained by other developers so, why don't write it clear and readable to make more easy their job? Let's start to think to the other! – Andrea Girardi Mar 12 '16 at 14:47