Search This Blog

Wednesday 31 October 2012

Formatting code using Eclipse

We all write code. But how many of us write clean well formatted code ? Do we care if our classes are correctly indented ? Does it affect us when our IDE shows tiny yellow warnings? Warnings that scream "unused variable" or "unchecked code" ?
Keeping the code clean is sooooooo important. I often find myself looking at code that is neither aligned correctly nor is formatted. There are unused imports, crazy indentations, unused variables, a lot of blank lines and dozens of commented lines of code.
The funny thing is that our IDEs provide real good support in the area of code clean up and formatting. I have been using Eclipse for the last four years. Intermittently I use its cousins MyEclipse and STS. The Eclipse Suit includes a whole lot of goodies in the area of code maintenance. One of the simplest options is to select the code and click Crtl, Shift and F keys together - This will format the code. Clicking Ctrl, shift and I keys together will indent the selected code correctly.
Eclipse also has this really cool Clean Up option. Simply right click on the project and select the properties option. This opens the properties window from where we can configure the clean up settings:

Eclipse even allows us to control the clean up options. Do we want the clean up process to format the code? Remove white spaces ? Enforce blocks for all if cases? What to do of unused imports? There are a whole lot of options that can be configured from here. Once you set up the profile, clean up is easy. Simply right click a java class or package and select "source-> cleanup" from the left click menu. And the codes' all cleaned up.
Whats better is once you have created a custom profile you can export it as an XML file and use it on other machines. So all developers of a single project can adopt a uniform coding style.
 Eclipse also allows to change formatter settings. So if you think the Eclipse 80 characters per line setting is too small, you can always increase it. This too can be exported. If you have multiple projects, the settings can be applied at a workspace level too. So do it once and all projects are setup for clean and formatting control.
What I prefer to do is simply automate the process. Ensure that every time a file changes, Eclipse runs the cleanup and formatting rules. This ensures that developers do not have to manually undertake the task of maintaining the code formatted. For this Eclipse provides us with Save Actions

This is also a part of the properties window. The screen allows us to configure what actions eclipse should take everytime we save a java file. So I set it to format my file, organise imports and also some additional actions such as including the final modifier, setting annotations etc. if we need to customise this behaviour use the Configure button on the screen.
This opens a window similar to the Clean up window and we can set our selections here. I am yet to find how to import these settings.
So now every time the file is saved, Eclipse performs the save action - thus maintaining my code very nicely. Only concern could be if you do not have sufficient memory. This can slow down the save process a bit.Are there any other cool settings you came across? Or is this all avoidable and too much pain? What do you think ?

5 comments:

  1. The problem with relying on the IDE to do the clean up is that if you have multiple people working on the same code base and if you are using a version control system, the IDE formatting change will mangle things up such that if you do a diff to get the list of changes between commits, all of the formatting changes will all show up and then it becomes difficult to merge changes.

    ReplyDelete
    Replies
    1. Need to make sure everyone in your group has there preferences configured the same.

      Delete
    2. Thats true, but then again, if somebody doesn't have it , it will be detected once somebody imports that persons code from the repository.

      Delete
  2. Hi Prem,
    The thing about eclipse's formatting is that you can export the settings and import it to other developer's eclipse work spaces. Once everybody in the team is using the same settings chances of conflict are far reduced. I have been using the above settings for the last year and have used it on two projects. The entire team used to have the same configuration for their workspace and things worked fine.
    I feel Managing clean code manually is a waste of developer's productivity and achieving the consistency of an IDE is not an easy task.

    ReplyDelete