Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intellij - cleanup code

I was wondering whether intellij has a feature that would allow it to scan through files and perform things such as organising imports, safely deleting unused functions and fields etc. This feature was very helpful in .NET in Resharper plugin. Thanks for any pointers.

like image 252
Bober02 Avatar asked Aug 08 '12 11:08

Bober02


People also ask

How do you do a code cleanup?

You can also run code cleanup across your entire project or solution. Right-click on the project or solution name in Solution Explorer, select Analyze and Code Cleanup, and then select Run Code Cleanup.

What is code cleanup called?

Code cleanup refers to the act of writing code so that it cleans up leftover data structures and other unwanted materials from memory and the filesystem. It is sometimes treated as a synonym of refactoring code, which involves making the source code itself easier to understand, maintain, and modify.

How can I beautify Java code in IntelliJ?

Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).

How do I undo a cleanup code?

Use Visual Studio's Undo action (Edit | Undo menu item) to undo the cleanup after its completion. This action is available when a document is open.


3 Answers

Use Analyze | Inspect Code with the corresponding options enabled to find unused code, in the results you can apply the fix to all the occurrences at once removing all the unused methods and fields.

Use Code | Reformat Code or Optimize Imports for the rest. Reformat Code has an option to optimize imports and you can run it on the whole project to do both operations globally in a single step.

It's also possible to optimize imports on the fly (enabled in Settings) and reformat/optimize imports when committing to the version control. Code inspections can be also executed before the commit to notify you about possible problems.

like image 187
CrazyCoder Avatar answered Sep 19 '22 05:09

CrazyCoder


There is a plugin for IDEA - FindBugs-IDEA. It scans for cases you listed and a lot more.

like image 31
evg Avatar answered Sep 22 '22 05:09

evg


You can try it out using the trial.

As to the imports, yes, you can Organize Imports ctrl-alt-o, and you can choose to do so on the current file or on every file in the project.

Not sure if you can delete all unused methods in a project, but you do get warnings regarding methods/variables that aren't used and can be safely deleted (and IntelliJ does offer the option to safely delete).

Also, in the Preferences, you can setup a ton of things that you choose to be considered as warnings, depending on your coding style, conventions, etc.

like image 41
pcalcao Avatar answered Sep 22 '22 05:09

pcalcao