Hyphen, CamelCase, or Underscore
My choice of word combination patterns depends on the language. I initially tried to use a one-size-fits-all approach, but as my projects became more complex with multiple languages, it got really annoying when the pattern did not match the language. The old standby argument of “readability” quickly loses its validity when you’re dealing with code. For instance, PHP can contain javascript, html and css and uses underscores for functions. Depending on your text editor syntax hi-lighting, a javascript function (camelCase) could use the same coloring as a PHP function (underscore). Since you can’t depend on color alone, using each language’s pattern enables you to immediately know the difference.
There will always be edge cases (PHP uses CamelCase
for classes), but I find that sticking with each language’s word combination patterns make for the least friction possible.
With that said, here are my current choices:
PHP
Variable: underscore
Function: underscore
Class: CamelCase
Javascript
Variable: camelCase
Function: camelCase
Constructor: CamelCase
CSS
Class: hyphen
ID: hyphen
HTML
Custom Attributes: hyphen
With html, the data-
prefix is one defense, even though attributes like tabindex
differ. Also, an underscore is technically meant to augment a letter or word, while a hyphen is used to separate words or syllables.
Regular Expression engines use the same characterizations.
For example, using the text:
some-word some_word
with the regex matching pattern:
\b\w+\b
the resulting array of matches is:
[0] => some [1] => word [2] => some_word
This may seem like I’m over-thinking things, but like I said, I want the least amount of friction when I work.