Eclipse Tips: Format on Save and Cleanup Code

0
787
Eclipse Tips: Manually format and cleanup code every time you save
Eclipse Tips: Manually format and cleanup code every time you save

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.

See More:
Mostly used IDE Hotkeys with Example every Developer must know
If you’re an IT developer then these IDE hotkeys would...

1Why 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.

See More:
20 + Bonus VSCode Shortcuts for Every developer should know
Node programming is the trending topic in today’s time among...

2How you can Format your code manually in Eclipse?

Eclipse Tips: Manually format and cleanup code every time you save

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.

  1. You have to do that every time and
  2. 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.

3How you can Format your code automatically in Eclipse?

By default automatic formatting and cleanup are disabled. To enable it, do the following:

  1. Go to Window > Preferences > Java > Editor > Save Actions.
  2. Select Perform the selected actions on save.
  3. Select Format Source code. Make sure Format all lines are selected
  4. Make sure Organize imports are selected.
  5. Select Additional actions.
  6. 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

Eclipse Tips: Format your code automatically in Eclipse

4How 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!!

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...