Formatter/Code Clean-up:
In our organization, we have our builds set up so that they fail even if the source has one PMD/Checkstyle error. Until recently, I'm used to running formatter, clean-up and removing unused import options (available via right click context menu) before committing my source code, so that I don't break the build. You could also have clean-up do formatting and remove unused imports as part of clean-up. If you don't know what I'm talking about, look at formatter and clean-up options under Java->Code Style preferences.
How to automate?
While this is good, I recently found that I can automate this. Go to Window->Preferences->Java->Editor->Save Actions preferences page. Here there are options so that formatter, clean up and removing unused imports all run automatically whenever the source code is saved.
Available Options:
In save actions preferences page, Check "Perform the selected actions on save" to enable the other options below. Check "Format source code" and make sure to select "Format edited lines" option so that only the lines you edit are formatted. This would avoid formatter changing every line in the file. Check on "Organize imports" and also check on "Additional Actions". The actions here are (almost) same as the clean-up options available under Java->Code Style->Clean Up preferences. Click on configure button in "Additional Actions" and make sure that they both have the same configuration. Only thing missing here (which would have been really nice!) is that the additional actions does not automatically point to the clean up preferences defined already and so you have to define it again here. Also, you cannot use a clean-up profile here either. But, still the automatic save actions really save me time and makes sure that my code is always clean. Finally, the main thing is, I break our builds much fewer now!
2 comments:
Hi Raja. Yeah that option is really useful. One problem I found is that it is not really good to start using it in a project you have been working since a long time, since every time you save your code, not only it saves your changes but it also performs all the configured formatting changes. In result, when you commit you will see more differences (your changes + format changes) and that is not really good.
In summary, if you are starting a new project this tool is really good, otherwise you will have to be aware of this.
Hi Christian,
To prevent changes to every line in the code, you could use "Format edited lines" option instead of the "Format all lines" option. This will prevent all lines from being formatted and so your diff will not be messed up.
But, clean-up could still cause additional lines to be changed. To prevent this, keep the additional actions to be at a minimum. This way, whenever you change your code, the diff is just what you changed.
This is most useful for new projects, since I have all clean-up options turned on. I love when it cleans-up my code automatically.
- Raja.
Post a Comment