Friends,
As a Java family member, today I would like to share some useful information regarding potential code violations that are done usually by Java Programmers. I know how much pain we take to develop the projects, but keeping this information in mind while coding helps us to nullify the code optimization which is kind of rework in project development phase.
As per the “Variable naming convention rule” variables that are not final should not contain underscores in their names.
private String _pError; // Wrong
…….
private String pError; // Correct
Rule says final variables that should be fully captitalized and non-final variables that should not include underscores.
2. Instantiating Wrapper classes:
Try to avoid instantiating the wrapper classes.
- Use Integer.valueOf() instead new Integer();
- Avoid instantiating Boolean objects; reference Boolean.TRUE or Boolean.FALSE or call Boolean.valueOf() instead.
3. Method Naming Conventions:
Method names should not contain underscores.
4. Calling overridable method in constructor.
Calling overridable methods during construction poses a risk of invoking methods on an incompletely constructed object and can be difficult to discern. It may leave the sub-class unable to construct its super class or forced to replicate the construction process completely within itself, losing the ability to call super(). If the default constructor contains a call to an overridable method, the subclass may be completely un-instantiable. Note that this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a private method bar() that calls a public method buz(), this denotes a problem.
5. Throwing raw exceptions:
Avoid throwing certain exception types. Rather than throw a raw RuntimeException, Throwable,Exception, or Error, use a subclassed exception or error instead.
6. Usage of System.out.print(ln):
Consider using loggers rather than System.(out|err).print.
7. Logger variables:
The Logger variable declaration does not contain the static and final modifiers
In most cases, the Logger can be declared static and final.
8. Garbage collection:
Avoid calling triggering a garbage collection explicitly.
9. Method call on object which may be null:
The null check is broken since it will throw a Null Pointer Exception itself.
It is likely that you used || instead of && or vice versa.
class Foo {
String bar(String string) {
// should be &&
if (string!=null || !string.equals(""))
return string;
// should be ||
if (string==null && string.equals(""))
return string;
}
}
10. Usage of equals () comparison
Avoid using equals () to compare against null.
Inexperienced programmers sometimes confuse comparison concepts and use equals () to compare to null.
11. Empty methods in an abstract class should be abstract instead:
An empty method in an abstract class should be abstract instead, as developer may rely on this empty implementation rather than code the appropriate one.
public Object couldBeAbstract(){
// Should be abstract method ?
return null;
}
public void couldBeAbstract(){
}
}
No comments:
Post a Comment