Eclipse refactoring on steroids

In my last post about common Java violations, I listed a set of mistakes that java developers tend to make. While refactoring a Java project with the objective to resolve those violations, I used the refactoring features of Eclipse extensively to quickly change the code. Below is the compilation of such refactoring techniques.

1. Adding curly braces around block level statements

It is often a good practice to wrap the block level statements with {curly braces}. But still, if there is only one statement in the block, then some developers prefer not to wrap them with {}. But Checkstyle will complain if you do so.

If you want to change this,

if(condition) 
    doSomething;

to this:

if(condition){
    doSomething();
}

Eclipse’s source cleanup is there for your help.

  1. In Project Explorer, right click on the source folder and select Source -> Clean up…using source clean up to add curly braces around block statements
  2. Choose Use custom profile and then click configure next to the custom profile section.
  3. By default, clean up action is configured to do multiple cleanup task. Since we are focused only on adding curly braces, we will disable all other cleanup tasks. To do this, navigate to all the tabs in the Custom Clean ups window and deselect all the cleanups.
  4. Then in the Code Style tab, select the Use blocks in if/while/for/do statements option and click OK.
  5. Then in Clean Up dialog, click Next and the refactoring will occur. You’ll be presented with a review page with the changes made.

2. Joining to if statements into one

Lets say, you have a code like this:

if(isLoggedIn){
  if(isAdmin){
    doSecretStuff();
  }
}

It is safe to combine the two if statements into one, unless you have some other code in between the two if statements. Of course, you can manually edit the code to remove the second if and move the condition to up. But wait, when Eclipse can do this for us, why should we do it ourselves?

  1. Place your cursor on the if keyword of the inner if statement.
  2. Press Ctrl + 1 which will open a context menu.
  3. Select the option Join ‘if’ statement with outer ‘if’ statement.
  4. Voila! the two if statements are now combined into one.

You’ll get:

if(isLoggedIn && isAdmin){
  doSecretStuff();
}


3. Renaming a filed and its getter / setter methods

According to a this, renaming an element is the mostly used refactoring in Eclipse. So, when you rename a field which has setter/getter methods, you’d manually rename those method names. But Eclipse can help here to simplify this.

  1. Place your cursor on the field name that you want to rename.
  2. Press Ctrl + Shift and then press R key two times continuously which will open the ‘Rename Field’ dialog box.
  3. Check the options ‘Rename getter’ and ‘Rename setter’ while providing a new name to your field.
  4. On clicking OK, this will rename the field as well as its getter/setter methods.

4. Inverting if statement

Suppose you have a code like this:

if(!isLoggedIn){
  // ask to login
}else{
  // allow access
}

Above code is 100% valid. But code quality tools such as Checkstyle might complain, because we are using a negativity check in the first condition (i.e. !isLoggedIn). When you have only one case (just the if block), then we can’t do much about it. But when you have both if and else, then you can just invert the conditions to avoid this scenario.

  1. Place your cursor on the first if keyword.
  2. Press Ctrl + 1 and then select Invert ‘if’ statement.
  3. Eclipse will invert the conditions and the corresponding blocks.
  4. Finally you’ll get:
    if(isLoggedIn){
      // allow access
    }else{
      // ask to login
    }

Helps to improve the readability of the code.

Conclusion:

Of course, the above are just the tip of the iceberg. Eclipse is capable of doing much-more advanced refactoring. So, what are your secret refactoring techniques?

Reference: Eclipse refactoring on steroids from our JCG partner Veera Sundar at the Veera Sundar blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× 2 = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close