If Hungarian notation is mostly disparaged why is “UpperCamel for constructors vs lowerCamel for everything else” so popular? [closed]

StackOverflow https://stackoverflow.com/questions/12175276

Question

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.)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top