Best Practices for Naming Conventions
January 22nd, 2010I’ve recently read that the standard naming convention for variables in JavaScript is CamelCase. That is, putting together capitalized words without spaces. Normally, when people say camel case they really mean lower camelCase where the first letter of the name is lower case and reserve the term PascalCase for “upper camel case” where the first letter of every word is capitalized.
camelCase is the de facto standard in JavaScript and preferred among other programming languages as well, but I don’t come across is that much in the design world where we mainly work with HTML and CSS.
For CSS naming of IDs and classes, using hyphens or underscores to separate words in a name is by far more common:
top-nav-header or top_nav_header as opposed to topNavHeader.
Personally I’ve always preferred the underscore naming convention. Not only for CSS, but for URLs, filenames, etc… However after some digging around about standardized naming conventions I’ve come to the conclusion that from now on I’ll be using the following model:
- hyphen-naming-convention for HTML, CSS, and URL names.
- underscore_naming_convention for file names.
- camelCase for JavaScript.
There really is no preferred method for CSS like there is for JavaScript. I see underscore and hyphen usage equally on the web. Some say hyphens are more legible, but I think that’s a matter of preference too. The one argument that I think makes sense is that it’s quicker to write a hyphen which only requires one keystroke (hyphen) vs. an underscore which requires two keystrokes (shift + hyphen). That might seem like a fraction of a nanosecond difference at face value, but it adds up over time, especially when you’re designing a new template from scratch!
As for URL naming schemes, I’ve read some interesting stuff about the way Google (and some other search engines possibly?) handle web pages named with a hyphen vs. and underscore. Apparently Google views hyphenated URLs as separate words, so if you had a page called “george-is-the-best.html” that page would show up in search for “george”, “best”, or “george is the best”. However if that same page was named “george_is_the_best.html” it would be treated as one long word and only show up in searches for “george is the best”. Crazy, huh? I think that’s more than enough reason to keep to naming pages with hyphens instead of underscores!
As for file names, these are tricky beasts. That “New Year’s 2010″ image on your desktop is named and read just fine like that on your computer. But try accessing that file from terminal (or command prompt if that’s your flavor =)P ) and it sure is annoying. And on the web, that’s not even legal! I didn’t come across any specific article that talked about hyphens vs. underscores for file names, but from personal experience I’ve never had trouble using underscores and see no reason to switch that naming convention now.
The key is to develop a standard naming convention that works for you and is acceptable to most others and stick to it.
