The Eclipse IDE offers to perform certain actions each time you save Java source code. These so-called Save Actions can be found here:
Window -> Preferences -> Java/Editor/Save Actions
The following is a list of my personal favorites:
- Format Source Code
Make sure you protect your comments from being formatted. Else the Eclipse formatter may be quite frustrating. Shortcut Ctrl + Shift + F.
- Organize Imports
In most cases this feature allows you to save time when refactoring the code. May also be performed manually using Ctrl + Shift + O.
- Add ‘this’ qualifier to unqualified field accesses/Add ‘this’ qualifier to unqualified method accesses
If you always qualify class members with the this qualifier, you can easily review, e.g. how difficult it may be to refactor out a method. In the rare case that you open your Java sources in a text editor, you may immediately recognize what variables are class members and what methods are static/non-static.
- Convert control statement bodies to block except for single return or throw statement
Often, I am tempted to not wrap one-line blocks below if/else with braces. But it is possible that you will need to add a second line and if you are in a hurry, you will not recognize that the second line is indeed outside the scope of the if/else.
- Add final modifier to private fields/method parameters/local variables
To my opinion, the final qualifier is a valuable means to make the code more expressive in terms of “what will not change from here on”. Unfortunately, there is nothing like the const keyword in C++ available for Java.
- Add missing ‘@Override’ annotations
Similar to the argumentation for final.
- some discussions about the use of final