I have my way of naming classes and IDs, and I've seen things all over the board. Here are the guidelines I follow:
- Keep it simple
- Keep it semantic
- Keep it short
- Use underscores between words
Keep it Simple and Semantic
I've never been too crazy for style names that really have no meaning like:
:class => 'H2603A'
Okay maybe I'm exaggerating, but I think you get the point. Try to use something that has meaning and can be recognized for what it is, like:
:class => 'header'
...but by the same token try not to get super specific about the contents. For example it would not be good to use something like:
:class => 'johns_comments'
What happens if John gets replaced by Frank?
Keep it Short
Try to keep it under 10 characters! I've seen some pretty long class names out there in the wild, and I'm not too crazy about them. They take up too much room. On the flip side, use caution when choosing something super super short.
Use Underscores Between Words
CamelCase, Underscores, Hyphens, Concatenated? Here are your choices:
:id => 'pageHeader'
:id => 'page_header'
:id => 'page-header'
:id => 'pageheader'
Obviously, one-worders are best, but when nothing fits I personally use underscores to separate words, and in some cases I concatenate, but not too often. I concatenate only when the words are small and distinguishable from one another. For example, .nowrap. I like to use underscores because when you double-click on one of the words, all of the words are selected.
Whatever your style I don't think it's worth a holy war with other developers, but do be consistent, i.e. if you're going to use hyphens, don't use underscores elsewhere and vice versa, and never use both in the same name. It's confusing. Again, I think it's okay to sprinkle in some concatenation like I describe above.
Here are some more opinions on the matter:
As far as naming classes and IDs, here's an article that may help you define your own style:
The following articles may also give you some ideas:
Want to know when the next article is published? Subscribe here. Thanks for reading!