Code Conventions for the Java Programming Language

The paradigm of a good coding include the readability of the code. Anyone can read and explain the code easily means code is fulfilling one of the standards.

To fulfil standards and keep code clean and readable each programming language had defined some code convention and that should be follow while coding.



Why need code convention for any programming language?

The 80% life of any program is comes under maintenance phase. That means it’s even not in same hand in which it was developed. So it is most important for any developer to develop such code which is not only clean and efficient but also easily readable and understandable by any other programmer. To reach such stander of coding each programmer should follow some fix rules while coding. For example naming a class in some specific manner. Such bunch of rules or conventions which are used by developer are known as coding convention.


Code convention for Java programming language


It is not possible to mention all the coding convention at one post so let's first look into the basic and most important code conventions of Javaprogramming languages.


1. File name 


The main and important rule. The file name of Java source file should have .Java extension and name should be same as its class which have main method. So if you are writing a class and it has main method in it than the file name should be same as class name.


2. The Naming convention


a.    Class name should be star with capital letter and all internal letter also should start with upper case. Classes name should be noun.

Example class ImageDraw

b.    Method name should be start with small letter followed word should start with capital and most important is method name should be verb.

Example: getAddition();

c.    Package name always should be unique and lower case. Should me on top level domain like com, edu, gov etc.

Example: com.sun.eng can be a good package name


d.    Interface naming convention is same as class naming convention


e.    Variable should be start with lower case with all internal letter start with upper case. It should not be start with underscore or $ character.

Example: int num;

f.     Constant should be in upper case letters and with all words separated with underscore.

Example: MAX_LENGTH

3. Comment

 If we talking about readability than comment is most important part of any programming language. As per convention all source code should begin with a c style comment stating the name of class version, author, date of creation, copyright notice and all other information. 


4. Package and import statement 


This will be first executable or compilable line of any Java source code. You should use only comment before package declaration and library import. Importing statement can be follow after package declaration statement. Example is
Package myPackage;
Import Java.awt.*;


5. Variable declaration


One variable should declare per line. And while declaring any variable keep its scope in mind.  Also declaration should be at beginning of any block although Java support declaration at any line of code. Also its good practice to initialize your variable where it is declare.


6. Providing access to class variables


Don’t make unnecessarily public to all your class variable. Take care of access rights of your class variable.


7. Variable assignment 


Don’t assign variable more than one in a single line as it create confusing for readers. Try to use separate line for each variable to assign its value.

 int var1=var2= 1; is not recommended


8. Static class object 


No need to use object to access the method of static class. You can directly call method name and can use class name to access the method.
myMethod(); or ClassName.myMethod(); is good but object.myMethod(); should avoided .


9. Indentation 


Indentation of code is must for best readability. Use tabs and spaces where ever it’s required and suits better.
Line length should not exceed 80 character as per standards. And if statement not fit in a single line than better break it in multiple lines but keep in mind that you should always break line only after comma or operator.


Conclusion


So this is the basic of code convention for Java programming language. There are many more available in oracle website but at least you should follow above code conventions for your programming practice.

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

2 comments:

  1. Both of these tips will all depend on your situation, there is
    a select group of getting your exx back fast experts who have studied this in depth.
    Or they could choose that since you moved on to another woman and he does not seem to be
    a little less cinematic. This is extremely difficult for most people, but it will be no big deal
    to getting your ex back fast you.

    Review my page how to get you ex back

    ReplyDelete
  2. This magical device has e cigarette disposable no restriction for using in public because
    it is definitely worth it. Once the winter season comes, ordinary cigarette smokers
    will not waste even half oof a cigarette lends itself e cigarette disposable to a sense of control.


    My homepage; buy e cigarettes online

    ReplyDelete