Hyphens and underscores and camel case (Oh my)

There’s a poll going on over at CSS Tricks asking whether coders and programmers prefer hyphens, underscoring or camel-casing when writing multi-word function names, CSS classes and IDs, et cetera.

The results of the poll are unfortunately going to be worthless due to the way the poll and the options were written. You’re only allowed to choose one of the three and, judging by the comments, the real answer for most people is “It depends.”

Still, it’s an interesting question to ask every once in a while, especially with Web languages where just about everyone these days knows enough to be dangerous, to see whether people are still holding to the established coding styles, or whether new sources are emerging as the leaders.

My most recent projects have involved the Cake PHP framework. It uses camel case pretty consistently throughout the PHP code, and even generates camel-cased IDs for HTML elements. For better or worse, I’ve sort of adopted a hybrid approach in my CSS, using camel case for element IDs because of Cake’s style, and using hyphens for classes because of my own preference. And it all does boil down to preference.

Obviously there’s much more that goes into coding style than just how you separate multi-word names. But multi-word naming style, like every other aspect of your overall coding style, just needs to be consistent. If you’re starting a project from scratch, use what works best for you and use it consistently. If you’re inheriting code or working on an existing project or framework, use whatever style has already been established (even if you prefer something else) and use it consistently.

Leave a Reply