That is not my name, please!

I have cause to meet some java newbies over the past week. And one thing 90% of them have in common is that they all had exposure to some other languages. PHP and Python in the majority. Another thing is that most are confused on the java naming convention. One of them even think naming conventions add to the terseness of the language. This post is to clear some of those misconceptions.

The reason for a convention, is so that everyone will be on the same page. Naming conventions in java allows developers to work together without much problem. When you use the convention properly, merely looking at your code, I know which is a class, which is a method, which is an object or variable, which is a constant. In fact if you follow some general programming naming conventions, I can know which class declares what object, what type a variable is and so on. I will highlight these naming conventions here and how I think they should be used.

  1. Classes: All classes should start with a capital letter. If your class’ name comprise more than one word, subsequent words should also start with capital letter. Every other character should be small letters. If your class has an abbreviation (e.g SQL, URL etc), they should be ALL CAPS no matter where they appear. Examples of good class names are TextDirectionChanger, InvalidOption, Controller, URLManager, NewSQLConnector
  2. Variables and Objects: All variables and objects should start with small letter. If your variable or object has more than one word, what do you do? yes, you capitalize subsequent words just like we did for classes. So valid objects or variables will be something like counter, classIntegrator, messageObject. But good programming practice should make us want to different between objects and normal variables of inbuilt classes. So this is what I normally do, when declaring variables, I use any name, but when declaring objects, I do something like this InvalidOption invalidOption, JOptionPane jOptionPane, etc. Also I put the name of the built-in type as part of the variable name when it is appropriate, e.g int intID, String strName, Double dblPrice. This later part are my own conventions, different programmers have different ways of doing this sort of things.
  3. Methods: Your methods take the same format as your objects/variables. The only difference is that methods have parameters list. Even if your method don’t have any parameters, you MUST put an opening and close parenthesis. One example that comes to mind is this getContentPane(), although this method returns an Object, it still follows normal method naming convention and although the method take no arguments, we still put the argument list delimiter ().
  4. Constants: All Constants should be ALL UPPER CASE. examples are EXIT_ON_CLOSE, HIDE_ON_CLOSE, etc. From those examples, you will see that the rule are a little different for constants. If your constant name has more than one word, separate the words with an UNDERSCORE.

There are other conventions, like proper use of interfaces, proper naming of methods, documentations etc. These are usually left to the programmers discretion. It is however good practice to follow popular conventions.

Advertisements

3 Comments »

  1. Dear Poster,

    Thanks for your comment. Few Nigerian developers know what they know very well. Well, there are hidden developer who move away from Java because Suns Microsystem is not really focusing on them. Microsoft is all over the place and it is what you are explosure to that you will do. Please, kindly join hremsoft now in starting a enterprise software development training for them. You input will be appreciated. Check http://www.hremsoft.com.

    Best Regards,
    Akinniran O.

  2. 0xCafa0xBabe said

    I am a nigerian and i dont know about other developers out there but the standard naming conventions of a langauge be it java, c++,php, AS3, etc are pretty the most basic rule of thumb any experienced programmer should stick with. I dont think it should be much of a problem. Compared to other prblematic areas of programming that is!

  3. topriddy said

    Nice blog. A lot of newbies don’t see a lot of reason to follow programming naming conventions, but as their program codes multiplies they then get the necessity for it.

    One part that amuses me is the – dblPrice, strName, looks nice on the blog. But what then do you do for :

    i) a boolean variable – succes. Would you write boolSuccess :D. I guess not. You would rather do isSuccess which makes it obvious that its a boolean type. Also another point to note is that using variable names such as these makes creating accessor (getters/setters) somehow clumsy. Would you rather write getDblPrice or just getPrice???

    The Sun suggested naming convention is http://java.sun.com/docs/codeconv/

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: