Saturday, June 18, 2016

This code is crappy!

Have you found yourself looking at code and thinking what a crappy code? Why can't people write good code/follow best practices? Some of the common issues you may observe are Unused methods, unused imports, unused variables, poor formatting, static access etc. You ask dev folks and some of the common statements you may hear "it is difficult to complete functionality, when do i look at best practices", "code has undergone changes so many times, it is difficult to take care of such things". Majority of you may have gone through this situation. 

How do we tackle this situation? 

Here comes Eclipse to our rescue. There are many configuration in eclipse that can take care of such things for us. Let's figure out some of those.     

Removing Unused Imports  Ctrl + Shift + O. Simple enough! 

Removing Unused private / local variables, methods etc.

1 - Go to Windows > Preferences > Java > Code Style > Cleanup

2 - Create New Eclipse Profile. Give a new name in Profile Name text box and click OK.

3 - Select appropriate cleanup actions in "Unnecessary code" tab that you want to use and click OK. In addition to “Unnecessary code”, actions from other tabs can also be configured based on project need/best practices.




4 - Your newly created profile will show up Active Profile drop-down. If not, select the profile that you created and click OK.

5 - Now you can use this newly created eclipse profile. During development, select a project in "Project Explorer" and click Source à Cleanup. Eclipse will apply your profile and perform appropriate code clean up/formatting.


Isn't it easy to keep such issues at bay by simply configuring Eclipse? Happy coding!



No comments:

Post a Comment