JavaBeat
Struts Tutorials | Hibernate Tutorials | JSP Tutorials | Servlet Tutorials | EJB Tutorials | Struts Resources | Spring Resources | Hibernate Resources

back to eclipse articles
Next Page
Previous Page

Refactoring in Eclipse 3.2

by Marcelo Giorgi
11/04/2007

Code Style

In big development team’s scenarios, sometimes, is difficult to maintain a uniform style of coding, because of the different professional backgrounds of the developers. This is highly undesirable because is difficult to maintain the code this way, just for the simple fact that the brain has limited resources, then processing and forcing it to maintain the different conventions of coding in the working memory inescapably implies a low performance in terms of code’s maintenance. So this is where Code Style come into picture, letting us to modify the following aspects of the code:

  • Control statements
  • We can rewrite our if/while/for/do single statements to be enclosed with brackets (blocks statements) always or never, depending on your needs, as a simple example. In the same way, we can check the option “Convert for loops to enhanced”, that let us to convert our for statements into their equivalent version in Java 1.5, which is better as it reduces the amount of code and reduces the number of required down-cast.

  • Expressions
  • Now, we can enforce to use parenthesis on every condition on the code, or use it just when it is necessary. This can be by selecting the appropriate Radio button of the Expressions panel.

  • Variable declarations
  • For compiler optimizations it is a good practice to use final variables whatever possible, for this reason is an interesting feature to allow the Wizard to modify the variable declarations (instance variables, parameter values and local variables) to be final when applies.

Members Access

To rapidly identify the properties of an object that are been used in a method it is useful to use the keyword this (besides it helps us to disambiguate parameter values with instance values).So it is nice, to use this new feature of the Wizard which help us uniform the use of ‘this’ qualifier for a field or a method in your classes. For example:


	 private int value;
		public int get() {
		return this.value + value;
	}
	 
If we check the feature “Use ‘this’ qualifier for field access” Would be converted to:

	private int value;
	public int get() {
		return this.value + this.value;
	}
	 

Other useful function enables us to unify the use of static members and static methods in a class. For that we can configure the access to those artifacts with the following identificators:

  • Access through subtypes
  • Access through instances
Besides that we can access the static fields and static methods by its qualified name by selecting the “Qualified field class” and “Qualified methods access” checkboxes respectively. Below we show the example code that Eclipse shows, as an example of an application of all those directives:

	class E {
	    public static int NUMBER;
		public static void set(int i) {
			E.NUMBER= i;
		}
		public void reset() {
			E.set(0);
		}
	}

	class ESub extends E {
		public void reset() {
			E.NUMBER= 0;
		}
	}
	

Unnecessary Code

The most important feature of this wizard, from my point of view, is the one that let you optimize the written code in terms of eliminating the unused code (which is always a headache when it comes the time to modify it). For instance, we can indicate the Wizard to remove all unused imports, or private unused members / local variables.

Most frequently that it should be, we take a defensive approach an cast more than we need, I take my responsibility for that ;). As you can imagine at this point, we can remove all those unnecessary cast that exist in our code checking in the “Remove unnecessary cast” checkbox of the “Unnecessary code” panel.

Probably you have used the feature “Externalize Strings…”, as you might know, if you want to omit some strings to be externalized, in the target property file, you should use the special keyword //$NON-NLS$ string at the end of the line that contains the string that you are not interested for inclusion. As your code evolves, you certainly find your self with many of those special keywords that are probably not associated with a line that contains a string anymore. In those situations, it could be nice to remove all those unreferenced keywords and that can be accomplished by selecting “Unnecesarry $NON-NLS$” checkbox of the of the “Unnecessary code” panel.

Missing Code

In Java 5.0, it is usual to use the Annotation @Override or @Deprecated. Those are very powerful, because they check for the correctness, of the overwrite behavior in the case of the Override annotation, of the code we generate. For this matter, it is useful to use these annotations on every artifact of out code, this can be accomplished by checking the appropriate Checkbox of the panel Annotations in “Missing Code” Tab of the Wizard.

Another suggestion that is important to keep mind is to define a versionID of an object that implements the interface Serializable. That assure us that this class cannot be misused when is serialized in behalf of another class.

Next Page
Previous Page


JavaBeat 2006, India | javabeat home | About Us | partners directory | Advertise with us