I\'ve been using Javascript for quite a while, but I\'ve always ignored the comm
ID: 647149 • Letter: I
Question
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?
Explanation / Answer
Let's see what tell two most popular style conventions:
JavaScript naming rules in Google JavaScript style guide suggest camel case.
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?
Your code will be inconsistent with code written by other developers,
When working in a team, your colleagues may tell you that your style sucks and ask you to change it,
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,
Other people using your code may mistakenly use new when they shouldn't,
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,
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.