Wednesday, October 3, 2007

Against CamelCase

I can think of a few reasons why someone would choose camelCase in favor of underscore_delimited text.

1. The underscore character doesn't have a convenient place on the keyboard.
2. The shift key isn't working.
3. You need to inscribe your code on gold leaf, so the extra character costs $.
4. You forgot how to do hungarian notation.

I prefer underscore delimited text. Perhaps in the past there was a premium to be paid for writing software with variable names that were a few characters longer. Back then it made good sense to use a compressed notation. But nowadays, that notion is gone. So why does it continue to perpetuate itself? These are traditions that need to be overcome. I acknowledge that it is important to maintain the coding style of legacy software modules when modifying those modules. But how many new modules of code still adhere to the old conventions?

I prefer underscore notation because it most closely mimics the convention used in natural language for delimiting words. And I don't think you need to capitalize the first letter of nouns (unless you're German), for every New_Word that appears in a variable name. I think the English rules for spelling and clauses are sufficient. I feel the same way about abbreviations. Making abbreviations for simple things like "copy -> cp" might have made sense when working with 2K of system memory, but it doesn't simplify anyone's life when 5-6 years later someone doesn't know if you're trying to "compose", "compress", or "copy" someone's code. The whole camelCase fiasco promotes this idea; since it already looks outlandish- what's the harm in making it more outlandish by incorporating abbreviations?

Abbreviations and camelCase in software implicitly encourage the notion that the hardest part of writing code is typing. Typing is the easy part. If it were the hardest part, we might expect a single developer to be able to write 50,000 lines of code in 6 weeks (that's 50,000 extremely dense lines at 60 wpm, 40 hrs/week).

Nothing says "This is solid, tested code" like:
void initialize_distributed_system(
    int number_of_clients,
    int seconds_before_timeout,
    const char* log_filename)

{ ... }

and nothing induces nausea like:
void initDSys(
    int clientNm,
    int tmoutSec,
    const char* logFname
);
{ ... }

"Long live the underscore_delimiters!" -Dune (1984)

2 comments:

Bossk said...

While the use of the underscore as a word separator had its use once upon a time, it has outlived its usefulness. In the early days of computing, there most keyboards were capable of typing capitals, thus some method of making identifiers was needed and the underscore (a relic from the old days of typewriters when one needed a carriage return and an underscore to underline a word). Hence it should regarded as an abuse of notation.

Next it is easier to read camelCase notation since it gives identifiers the appearance of being one word--as well they should since they linguistically correspond to a name rather than a phrase. Underscores also clutter up names with a new symbol.

Your argument that camelCase constitutes some sort of abbreviation is a straw man. It is not, and never was the purpose that camelCase was intended to shorten the identifier as, e.g. "cp" for "copy."

One final note: your Dune quote is dated 1984, not long before the demise of underscore case.

Paul Houle said...

I agree!

I believe in the underscore convention because there's a 100% algorithm for converting between underscored identifiers and a stream of English words. Not so for DementedCamelCase. Fundamentally, by not wasting upper/lower case on replacing spaces, you can use upper and lower case for other purposes.

For instance, call_FBI() is the correct thing to write when you're using underscores. Is it "callFbi()?" or "callFBI()?" in CamelCase?

It's not such a big deal when you're using static languages and writing your code by hand, but when you're doing code generation or using a dynamic language that lets you generate references on the fly, underscore makes a lot more sense.

Why is CamelCase popular? I think it's like an infectious disease. It became an official part of the Java platform, got copied by C#, and it's just something that spreads because people see it.