Beginning with Comments
Its good practice to begin the java source files with a comment that lists the class name, version information, date, Author and copyright notice:
* Class Name :
* Copyright notice
* Change History
* Date Added/modified By Version Number Description
Package and Import Statements
The first line of Java source files after the comment is package statement. After that import statements can follow.
Class and Interface Declarations
Parts of a class or interface declaration is as bellow and the order should be as listed.
1) Class/interface documentation comment ( /**…*/)
2) Package statement
3) Import statement
4) class or interface statement
5) Class variables (static variables)
6) Instance variables
Java Naming Conventions
Names should be in lowercase and meaningful.
Class names should start with capital letter and each new word should begin with a capital letter. Try to use nouns because a class is normally representing something in the real world:
Method names should be in mixed case. Use verbs to describe what the method does:
variable name should be in mixed case. The names should represent what the value of the variable represents:
If you want to use a variable in a loop which is having short life , we can use short names like bellow.
for (int i=0; i<10;i++)
//variable i has short life
All the constant’ss names should be in uppercase.
static final int DEFAULT_WIDTH
static final int MAX_LENGTH
Avoid long names: (< 15 characters is a good idea).
Use abbreviations sparingly :
if you want to use a short form for the word “number,” then choose one of nbr, no, or num, document which one you chose (it doesn’t really matter which one), and use only that one.
Avoid names that are similar and differ only in case.
Comments are required to the clarity of your code. The reason why you document your code is to make it more understandable to you, your team memebers, and to any other developer who comes after you. Keep the comments simple.
Indent Your Code
One way to improve the readability of the code is to indent your code within the scope of a code block. Any code within braces forms a block. Hence that the code within a block should be markable as one unit.
Organize Your Code Sensibly
Compare the two code examples presented below.
1) – Code is not organized
maxRate = 100;
2) – Code is organized
maxRate = 100;
Avoid empty catch blocks
Its very bad idea to have an empty catch block. When the exception occurs, nothing happens, and the program fails for unknown reasons.
In general, when a exception occurs, it can be thrown up to the caller, or it can be caught in a catch block. When catching an exception, some options include :
inform the user
log the problem,using any log tool
send an email describing the problem to an administrator
Deciding what exactly to do seems to depend on the nature of the problem
Don’t overuse Java code in HTML pages:
Putting all Java code directly in the JSP page is OK for very simple applications. But overusing java code in HTML leads to
performance issues ,
code will be not easy to read and understand.
Choose the right include mechanism:
Keep all the Static data such as headers, footers, and navigation bar content in separate files and not regenerated dynamically.
Don’t mix business logic with presentation:
If you are not mixing the business logic with presentation , you can change the way data is displayed without affecting business logic.
Use Strings with care.
Always use Strings carefully in your code. A simple concatenation of strings can reduce performance of program. For example if we concatenate strings using + operator in a for loop then everytime + is used, it creates a new String object. This will affect both memory usage and performance time, in such scenario you can use StringBuffer instead of String.