Page copy protected against web site content infringement by Copyscape
CountersNew Postings :- Career Column :- Tips For Grads - fresh job seekers . View Point:- Niagara Falls - The massive water falls in the world. [ TOPIC GUIDE ] Interested experiences, thoughts and views to share...?? Click on 'View Point'. Tips and topics related to Career, Technology...??? Click on 'Career Column'. Wanna share fun and jokes.....??? Click on 'Fun Fountain'. Wanna shed your views on good and bad sides of politics and current affairs.....??? Click on 'Political Nuke'. © 2007 All rights are reserved. This site is best viewed in Firefox. Please post your problem with IE to prathap2k@inbox.com; QUOTE OF THE DAY :- "Simplicity of character is the natural result of profound thought"

Friday, July 11, 2008

Potential Java Code Violations.

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.

If it makes/doesn’t sense to you then please feel free to post your views, comments and complements.

Please free to share if you find still more and not mentioned below.

Thanks and enjoy.

1. Variable naming conventions:

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 abstract class ShouldBeAbstract{

public Object couldBeAbstract(){

// Should be abstract method ?
return null;
}
public void couldBeAbstract(){
}
}

…to be continued…

No comments: