0

I've been using Javascript for quite a while, but I've always ignored the common naming conventions that I see online (camelCase). I know it all depends on the developer's preference, but why camel case for Javascript?

I'm originally a PHP programmer and I kind of have my own naming convention and I use it everywhere I can, including Javascript. Well, I have to sacrifice my naming convention when it comes to Java though as the program flow depends on those names sometimes.

Anyway, my convention goes like:

var variable_name = 'I always use underscores to separate words';
var another_variable = 'I also tend to use single quotes all the time';

function epic_name_here (some_paramaters ...)
{ 
    // Curly brackets on separate lines
    do_something();
}

if (x == y)
{
    // Same goes here
}

I have taken this kind of code-writing style from working a lot with phpBB, their docs encourage people to use this style.

Why do I see a lot of people writing Javascript in camelCase? And will it be a problem if I don't use it?

  • That's called underscore_case and is pretty common... Single quotes instead of double quotes is a separate issue entirely. – Izkata Dec 15 '13 at 1:00
7

Let's see what tell two most popular style conventions:

  1. JavaScript naming rules in Google JavaScript style guide suggest camel case.

  2. Douglas Crockford suggests to start the names with a lower case letter too in JavaScript: The Good Parts. In the same book, Douglas explains that by convention, objects which require new start with a capital letter (see page 123 Constructors and new).

why camel case for Javascript?

No reason. This is a common practice, that's all. Don't look for a proof that this style is undoubtedly better than any other.

Why do I see a lot of people writing Javascript in camelCase?

Because developers tend to be consistent with other developers who write in the same language. Given that camel case is a common practice, new developers would tend to follow it.

And will it be a problem if I don't use it?

  1. Your code will be inconsistent with code written by other developers,

  2. When working in a team, your colleagues may tell you that your style sucks and ask you to change it,

  3. If you contribute to an open source project which uses camel case, and you chose a different convention, other contributors will definitely ask you to change your style,

  4. Other people using your code may mistakenly use new when they shouldn't,

  5. You'll do an additional effort when reading someone else's code. Other people will do an additional effort when reading yours. Neither you, nor others need to waste time for that,

  6. If you use a linter (style checker), you'll have to rewrite the ruleset to fit your particular style. You could have spent this time to do something useful instead.

  • 1
    Point 4 (new keyword) is related to upper vs lowercase initial, it's not related to camelCase vs underscore_case. – mastazi Dec 15 '13 at 5:20
1

MainMa said it all insofar as camelCase (from my point of view, underscores remind me of Cobol coding :)

Insofar as single versus double quotes: that convention probably migrated from C, where single quotes are used for chars & doubles for strings. In javascript, it doesn't matter which you use except in such cases as:

var myStr = "He said, 'She said, it's dark in here. Where's Kevin's flashlight?'" Because the single quote is used for inner quotes, apostrophes and contractions, it makes more sense to use double quotes for the string assignment. Doing it the other way would require escaping, at the least, the contraction.

  • +1 for the contraction, if your strings are written in English it's very common, and you don't want to write every time stuff like this: 'Hey it\'s me! How\'s it going?' – mastazi Dec 15 '13 at 5:23
-2

According to this study:

results indicate a significant improvement in time and lower visual effort with the underscore style. The interaction of Experience with Style indicates that novices benefit twice as much with respect to time, with the underscore style.

so if that matters to you go ahead with underscore style.

Not the answer you're looking for? Browse other questions tagged or ask your own question.