0

It seems to be the case at least here on StackOverflow that Hungarian notation is most often considered to be a bad thing (though a minority are still in favour).

Now in the JavaScript world where I've been doing most of my coding for the past few months seems to have embraced a naming convention which is in essence very similar yet I haven't seen anyone arguing against it, that is UpperCamelCase to name object constructors vs lowerCamelCase to name everything else.

So the differences seem only to be at the surface:

  • Hungarian notation uses prefixes whereas JavaScript uses casing.
  • Hungarian notation can distinguish many things whereas JavaScript can only distinguish "object constructor" and "not object constructor".

Some people like to state that "systems Hungarian" is different from "applications Hungarian" where the former indicates types and the latter indicates things which types don't cover. In this case "systems Hungarian" is usually still considered bad while "applications Hungarian" may be considered good.

The JavaScript camel casing convention seems more like systems Hungarian so wouldn't gain any merit on that point.

The points people argue against Hungarian notation seem to still apply to JavaScript object constructors:

  • The compiler knows the type anyway.
  • The IDE is much better at this.
  • It's too brittle when types change.
  • It makes code harder to read by cluttering it up.

So what is different about this JavaScript convention that makes it OK where the more general Hungarian notation is not OK?

Is it lack of good JavaScript IDE? Does its limited area of use make it not so bad? Is it due to dynamic languages not really having much in the way of types? Is it just not a problem since it can't get overused?

(If this belongs on programmers.SE rather than SO please migrate.)

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • During the [StackExchange summer of love](http://blog.stackoverflow.com/2012/07/kicking-off-the-summer-of-love/) make sure you don't add any comments while voting to close, otherwise you might risk constructively letting me know what's wrong with this question. – hippietrail Aug 29 '12 at 10:01
  • The reason for closing is exactly as stated: `As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion.` – slebetman Aug 29 '12 at 10:03
  • The passive-aggressive comment is rather ironic, but the question is a poster child for "not constructive": *We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit* ***debate, arguments, polling, or extended discussion.*** – JJJ Aug 29 '12 at 10:04
  • I included facts and feel there must be an objective answer. Yes there probably will be debate too I suppose. – hippietrail Aug 29 '12 at 10:05
  • So likely debate / argument / polling / discussion trumps facts? This seems to suggest that we should expect 50% of people on each side of the argument, which I don't expect at all in this case. – hippietrail Aug 29 '12 at 10:07
  • 1
    Out of curiousity, what is the actual problem you want to solve? – Rob W Aug 29 '12 at 10:08
  • @RobW: I'm trying to decide for myself on a coding style for JavaScript and decide whether I should follow a convention that seemed unpopular until now. I want to know the logic if there is any rather than just blindly follow... Much I suppose like these questions: [1](http://stackoverflow.com/questions/3157431), [2](http://stackoverflow.com/questions/6439031), [3](http://stackoverflow.com/questions/6712449) – hippietrail Aug 29 '12 at 10:19

1 Answers1

2

It is more like "applications Hungarian" after all.

Javascript doesn't know whether your function is a constructor or a plain function. And calling a constructor as if it was a plain function may lead to a quite odd undesired outcome (unless you're using ES5 strict mode).

I don't see how IDE will help you there either. The only thing IDE could do is to prevent you from calling var x = new plainFunction(); and var y = Constructor();, which implies using the Hungarian notation first.

I don't see how using the notation makes the code harder to read either.

penartur
  • 9,792
  • 5
  • 39
  • 50
  • I have to admit the way objects and constructors work and/or look in JavaScript has always been a source of confusion for me. They always seemed to be some kind of "class" but called "function". I didn't see those gains either but I wanted to include a factual list of points people often use against Hungarian notation. – hippietrail Aug 29 '12 at 10:21
  • There is no such thing as classes in JavaScript. JavaScript features prototype-based object model; the main difference between calling the function with `new` and without it is whether the `prototype` property of the function object will be copied to the resulting object or not. – penartur Aug 29 '12 at 11:18
  • That's why I used the scare quotes in my comments but didn't use the term "class" at all in my question. But yes it's true that I still don't really grok this stuff... – hippietrail Aug 29 '12 at 16:16