Friday, 19 August 2011

Coding standards in Java

In college days, When we start coding we used to name the variables and functions (in java we will call it as methods) with meaningless name like a,b.And filename as test1,test2,etc, (Because at that time we don't think about the functionality when giving these names and also a,test1 are simpler to type, :P )

In this post will try to tell what is coding standard ? why should  I care about coding standard ? what makes me difficult if i forgot/ missed to follow coding standard ? what are the coding standards in java?

What is coding standard ?
          It simply means we have to follow some standard when we are giving name to varibles,functions and classes,etc.

Why should i follow coding standard ?
          As every programmer knows,Maintenance occupies major part in the Software Development Life Cycle (SDLC)( what is SDLC ??? :S This we will see in the next post ).
         For any IT development,No one will do the entire coding alone.All the projects are developed by many developers and everyone need to get enough knowledge what others did. So it should be necessary to name the state (In C language, we will call it as variablename) , methods (In java, We will not call as function),Classes .etc., (everything the developer name it ) with proper name so that others can understand the code (To some extent) and maintenance made easier.

what makes me difficult if i forgot/ missed to follow coding standard ?
         Statistics says that developement costs 20% and maintenance costs 80% (In terms of both time and money).If you missed/forgot to follow coding standards maintenace cost get increases a lot and sometimes code may be dumped into a dustbin in future.

what are the coding standards in java?

Classname -- should be CamelCase (Make the first letter of each word in Caps) E.g, PersonDetails

Methodname and State ( Don't call this as variable name) -- Should start with small letter and first letter of the following words should be in caps. E.g, userInfo , getUserDetails()

Interface Name -- same rule applies as class name E.g. PersonInterface

For constants - All should be in caps and sepeate two words by underscore ( _ )

package name -- All in small case e.g com.test.application

Coding standards is not only the name given to what we are defining and also format of the code is also more important

1) Do not define variable (state ) in a single line like
         int age,yearsExp;
 Instead define like below
           int age;
           int yearsExp;

2) Initialize the variable when you declare itself like below
           int age =0;
           int yearsExp=0;

3) Use appropriate comments wherever necessary

4)Do not omit curly brace in the if statemen eventhough the block consists of only one statements.

5) Use appropriate blank spaces

     Two blank lines should always be used in the following circumstances:
             • Between sections of a source file
            • Between class and interface definitions
      One blank line should always be used in the following circumstances:
           • Between methods
           • Between the local variables in a method and its first statement

6) A keyword followed by a parenthesis should be separated by a space. Example:
          
                  E.g while (true){
                                    .......
                                }
There are lot more in java.But this is for those who started their coding (For those who already developed some coding may be aware what we discussed so far) in java.


If you have any queries or comments pls let me know.

Thanks.

No comments:

Post a Comment