Java code cleanup using eclipse

Have you ever wanted to clean up your code? Remove unused imports or variables?  Eclipse provides this feature, all you have to do is to create a profile !!  Here’s how it’s done.

Steps to Create Profile in Eclipse

Go to Windows >> Preferences >> Java >> Code Style >> Clean-Up Click on New button.

In the next window provide a Profile Name of your choice for eg.  JBT and click “OK”. It will take you to a new window where you can configure your clean up options.

There are different tabs provided that allows one to set their preferences for the profile.

Listed below are the cleanup options.

Code Organization

Formatter

  • Format Source Code
  • Remove Trailing Whitespace
  • Correct Indention

Import

  • Organize Import

Members

  • Sort Members (Caution)

Code Style

Control Statement

  • Use blocks in loop Statements
  • Convert for Loops to Enhanced for loops

Expression

  • Use Parentheses in Expression (Caution)

Variable Declaration

  • Use final modifier wherever possible (Caution)

Member Access

Non Static Access

  • Use ‘this’ qualifier for field access
  • Use ‘this’ qualifier for method access

Static Access

  • Use Declaring class as a qualifier (Caution)

Missing Code

Annotation

  • Add missing Annotation (Caution)

Potential Programming Model

  • Add Serial Version Id

Unimplemented Code

  • Add unimplemented Method (Caution)

Unnecessary Code

Unused Code

  • Remove unused Imports
  • Remove Unused Private Members
  • Remove Unused local Variables

Unnecessary Code

  • Remove Unnecessary Cast

How to Apply the Profile

Select an application where you want to apply the newly created profile (JBT). Right-click on it and go to Source >> Clean Up. A window will appear as below.

Select Project and Profile and click “Finish”.


Now you know how to clean up your code with the help of Eclipse. I have already created a profile and added it for you with preferred settings. Just import the attached file in your eclipse and start using it with your code.

How to Import Profile XML

Go to Windows >> Preferences >> Java >> Code Style >> Clean Up Click Import button and select XML file. JBT

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.