Introduction
We every one knows that, how to write the code to meet the requirement. But apart from the requirement many things need to be considered during software development. One of the important thing is Coding Standard.
Why Java Coding Standards?
Software industry is not like other industries to complete project at certain period of time. Software development will never end. Today you can develop a software, but that software should be maintained through out the software's life time. So the code what you have developed should be understandable by other programmers. The ultimate aim of this Coding Standard is to improve the quality of the Coding and more readability
Naming Conventions
Naming Conventions are important to makes programmers more understandable. Below table describe the Naming Conventions,
Identifier Type | Rules of Naming Conventions | Example |
Package | Package name should be written in lower case. Prefix of the package should be any one the top domain name, like com, org, net, edu, gov. Sub package names can be vary based on the organizations standards. | com.accounts.bean com.utility.form com.payments.action |
Classes | Class name should be noun. If your class name contains more than word, first letter of each word should be capitalized. Keep class name simple and meaningful. | class TransactionManager class Dog |
Interfaces | Interface name should be capitalized like class name | interface MessagingService interface Printing |
Methods | Method name should be verb. First letter of method should be small case. In case of more than one word in a method, each internal word should be capitalized. | run(); play(); getConnection(); |
Variables | Variable name should start with small case, in case of more than one word in a variable, each internal word should be capitalized. Variable should not start with underscore(_) or dollar sign($). Variable name should be short and meaningful. Avoid declare the variable with one word. One word variable can be used for temporary purpose. | int rollNumber; double amount; char c; |
Constants | Constant name should be all upper case. In case of more than one word, each word should be separated with underscore(_). | static final int MIN_LENGTH = 10; static final int MAX_WIDTH = 100; |
File Organization
- Creating a class or interface on right package is an import thing. Suppose if you are creating a class for the purpose Utility Payments. So that class should be placed under payments package. You should not place that class in Reports package
- Creating separate source file for each class and interface is the best practise. If there is any private class or interface which is associated with public class, you can put them in single source file.
- All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice: /* * Classname * * Version information * * Date * * Copyright notice */
Indentation
Indentation also an important thing in Java Coding Standards. Tab space can be used as a unit of Indentation. Avoid lines longer than 80 characters. Statement which is more than 80 characters, that can be break with some general rules.
- Break after comma
- Break before operator
- Align the new line with the beginning of the expression at the same level on the previous line
- If breaking a Statement is leading to confusing code, tab space can be used
Comments
Comments should give the overview of the code. Comments should contains only the relevant information of a program. Java program having tow types of comments- Implementation Comment
- Java Documentation Comment
- Implementation comments are delimited with /*...*/ and //.
- Implementation comments describe about the particular implementation.
- Java doc comments describe the java classes, interfaces, methods, constructors and fields
- Java doc comments are delimited with /**...*/
- Java doc comment should be written just before the declaration.
Declarations
- Declare one variable per line is recommended
- Try to initialize the local variable at the time of declaration itself. This is may not possible all the time based on the requirement
- Declaration should be in beginning of the block
- Declare all the necessary variables at beginning of the block itself, don't wait to declare the variables until its first usage. (for loop index variable declaration is an exceptional case)
- No space between a method name and the parenthesis "("
- Open brace "{" at end of the same line in declaration statement
- Closing brace "}" starts a line by itself indented to match its corresponding opening statement
White Space
Blank LinesBlank lines improve the readability of the code, Blank Lines can be used below circumstance
- Two Blank lines can be used between the section of a source file
- Two Blank lines can be used between the class and interface declaration
- One Blank line can be used between the methods
- One Blank line can be used between local variables of method and its first statement
Blank Spaces can be used below circumstance
- A keyword followed by parenthesis should be separated by a space
- A blank space should be used after comma in argument list
- All binary operands except dot(.) should be separated from their operands by a space. Blank space should not separate the unary operators
Java Coding Standard tools
Below open source tools can be integrated with Net Beans IDE for Java Coding Standards- Checkstyle java standard tool
- PMD java standard tool
No comments:
Post a Comment