Code Advice #4: Don't capitalize acronyms!

(See intro for a background and caveats on these coding advice blog entries.)

Remember how your ${NATIVE_LANGUAGE} teacher taught you to capitalize your acronyms? Please unlearn it. Thanks.

Seriously though... when you're writing code, you're not writing English. It makes perfect sense to choose symbol names that read well in your preferred human language. But CamelCase, the preferred capitalization scheme in Java code, is not proper English capitalization either. So don't carry over the acronym habit from English.

For example, use HtmlDocument instead of HTMLDocument. Use CssParser, not CSSParser. Unfortunately, many existing APIs violate this rule. Even the JDK has many violations - acronyms like IO, UTF, EOF, XML, SQL, HTTP, URL and URI abound. However, I think the trend is towards uncapitalized acronyms in newer classes going through the JSR process.

So why precisely should acronyms not be capitalized? For readability reasons. The acronyms are treated as words, not as individual letters, so when you're scanning a CamelCase word you can quickly see that HtmlDocument contains the words "Html" and "Document". So it's easier to visually scan, and I think it just looks better.

Unfortunately, it may be tricky to rename existing classes in your code. If you use CVS, changing class names that vary only in capitalization will confuse CVS on some platforms. On Windows two filenames are considered identical if they only vary in case.

Eof.

Comments:

> For readability reasons. The acronyms are treated as words, not as individual letters But why exactly "acronyms are treated as words" -- it's acronyms, not words. I just looked at dictionary: ...enwrap, eolation, eon... No word "eof"! > Even the JDK has many violations AFAIK, "abbrevs are not words" is a rule in many coding stype guides.

Posted by Vladimir on November 20, 2005 at 05:40 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today