Search This Blog

Sunday, August 17, 2008

Whats in a name?

I have often had discussions with people regarding which is the better way to represent Acronyms in Java. Should a class that represents a Billing Telephone Number be represented as BTN or Btn?
I often fall back to the Java language classes for reference. URL, UUID, URI seem to support the full uppercase naming convention. But is that the correct way? It has to be more than a matter of convention and style to sway one way or another.

I have been reading this excellent book by Gafter and Bloch called "Java Puzzlers" and find it to be an excellent book that often makes me think "WTF?" when reading parts of it. I only walk away humbled regarding my understanding of the java language. I truly recommend this book as a read for any java enthusiast. Just, please, don't use the examples for interview questions. Some hapless bloke like me wouldn't stand a chance :-)

Anyway, back to the naming conventions. Consider the following example:

1 public class Naming {

2 public static void main(String args[]) {

3 System.out.println(A.BTN.NUMBER);

4 }
5 }
6
7 class A {

8 static class BTN {
9 static Integer NUMBER = 9789999;

10 }
11
12 static Btn BTN = new Btn();

13 }
14
15 class Btn {
16 Integer NUMBER = 12345678;

17 }


Will it even compile considering Class A has a static variable called BTN and a class called BTN ?
For the sake of discussion, lets say the class does compile, what then will be the run time output? Will it print out 9789999 from the inner static class of A or will it print out 12345678 from the instance of class Btn?

If you surmised the answer to be the latter you are right, the code does infact print out 12345678. Now why is that?

The Java Language Specifications (JLS 6.5.2) states that when a variable and a type have the sam me name, the variable takes precedence. In other words, the variable name tends to obscure the type name.

Based of the above, if the program mentioned above had used the java naming conventions, i.e., the static inner class of B had been named Btn, the problem would not have surfaced.

With the same said, following the conventions regarding naming of java classes, packages, variables is quite essential.

Lessons Learned:

1. Use Camel case starting with a capital letter for Class Names
2. For static constants use ALL CAPITALS.
3. Package names in all lower case.
4. Avoid top level package or domain names for variable names. For example, don't name classes String, Object; or don't name variable as java, net, org etc. Read the book for more.

The book is really nice, some example's seem to indicate warped cases which the normal developer would not even tread. Regardless it is worth a read. Let me leave you with some other pit falls, maybe convince you to want a read ;-).


1 public static void main(String args[]) {

2 System.out.println(2.00 - 1.10);
3 System.out.println('A' + 'B');

4 }



What gets printed? Enjoy!

2 comments:

Dave Stockmann said...

The answer is

0.8999999999999999
131

Any normal developer who didn't know the answer would throw that into eclipse and run the main. :-)

Sanjay Acharya said...

Wow! Didn't know one could do that...How cool ;-)