Hello coders, Today I am going to share Eclipse tips which help you a lot. If you’re familiar or know about the features about VSCode. Then that IDE has a great feature which is format on save. That feature makes our code clean and format in a proper manner.
How to Format on Save and Cleanup Code in Eclipse IDE?
Eclipse has a superb feature that formats in keeping with your coding standards. It handles things like indentation, where curly braces are placed if blank lines ought to arise between field declaration and hundreds of different options.
Previously, We discussed some mostly used hotkey for Eclipse tips.
Why code formatting matter?
It’s very important.
In a small program (under a thousand lines, say) – you could get away without doing it – but as programs get more complex, they get harder and harder to understand – and at some point (probably around 10,000 lines – in my experience) – you can’t even understand code that you yourself wrote without being able to re-read it.
The good layout has been proven time after time to make code more readable – and the time it takes to do it neatly ALWAYS repays itself many times over in the future.
If you are working with VSCode also then I made 20 shortcuts are waiting for you.
How you can Format your code manually in Eclipse?
However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F.
- You have to do that every time and
- You have to remember to do it in the first place (and we all know how good developers are at remembering things).
Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free.
How you can Format your code automatically in Eclipse?
By default automatic formatting and cleanup are disabled. To enable it, do the following:
- Go to Window > Preferences > Java > Editor > Save Actions.
- Select Perform the selected actions on save.
- Select Format Source code. Make sure Format all lines are selected
- Make sure Organize imports are selected.
- Select Additional actions.
- Click Ok, edit some code, save it and watch Eclipse format it automatically.
Here’s what the settings should look like.
Eclipse Tips: Format your code automatically in Eclipse
How do this Eclipse tips “clean up” the code?
With “Format source code” enabled, the code is formatted according to the formatting rules you’ve specified under Java > Code Style > Formatter (or project-specific formatting if enabled).
With “Organize imports” enabled, Eclipse will automatically reorder imports, prompt for imports to unresolved classes and remove unused imports. It basically does a Ctrl+Shift+O whenever you save.
When you enabled the option “Additional actions”, you told Eclipse to do more cleanups. You can see what they are and what you want to be done by clicking Configure on the same preference page. Make sure that Additional actions are ticked.
Eclipse can clean up a lot of things, but here’s a list of some of the more useful ones I have enabled:
- Add @Override and @Deprecated tags: Nice for compile-time checking. Under the Missing Code tab.
- Use the final modifier where possible: A nice thing if you want final variables to be marked as final automatically. It can get annoying sometimes, but mostly worth the trouble. Under the Code Style tab.
- Convert for loops to enhanced: Replaces an index-based loop with an enhanced for loop. It just reads better. Under the Code Style tab.
- Use blocks in if/while/for/do statements: Forget religious wars, I’ve programmed long enough to know that you always use braces even for single-line blocks. Eclipse can do this automatically. Under the Code Style tab.
- Remove trailing whitespace: Nice one if you’re obsessive about those blanks at the end of lines. Under the Code Organizing tab.
Share with your friends on Facebook!!
Share with your friends on Twitter!!
— Tell Me How (@tellmehowblog) January 30, 2020