- Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Refactoring basics
Note: To undo the last refactoring, switch focus to the project tools window, then press Ctrl + Z.
IntelliJ IDEA provides a comprehensive suite of automated code refactoring that can significantly improve productivity when used correctly.
First, don’t bother selecting anything before applying refactoring. IntelliJ IDEA is smart enough to figure out which declarations you’re going to refactor and only ask for confirmation if a choice is possible.
Tip: The real time saver is the ability to extract partial string expressions with the help of extract refactoring. Simply select a string fragment and apply refactoring to replace all selected fragment uses with introduced constants or variables.
Looking for to use
The Find use helps you quickly find all code segments that reference a component in a caret (cursor), whether that symbol is a class, method, field, parameter, or other statement. Simply press Alt+F7 and get a list of references grouped by usage type, module, and file.
To set custom options for using algorithms for lookup, press Ctrl + Shift + Alt + F7, or click the first button on the right panel and search for results.
If you are looking for plain text, press Ctrl + Shift + F and use “Find in Path”.
check
Check is a built-in static code analysis tool that helps you find possible errors, find dead code, detect performance issues, and improve the overall code structure.
Most checks not only tell you where the problem is, but also provide a quick fix. Press Alt + Enter to select quick Fix.
When code analysis is performed on the entire project, the checks are too complex to run. You can use one of the following two ways to perform this operation: from the main menu select “Analyze | Inspect Code”; Or select “Analyze | Run Inspection by Name”, as its Name.
Note that while checking provides quick fixes for potentially problematic code, intent helps you apply automatic changes to the correct code. To get a list of intents for caret code, press Alt + Enter.
Tip: The editor lets you quickly browse through highlighted issues with keyboard shortcuts. Press F2 to go to the next question, then Shift + F2 to go to the previous one.
Code style and formatting
IntelliJ IDEA automatically applies the code style you configured in the code style Settings when editing, and in most cases does not require an explicit call to the “reformat code” operation.
Useful formatting shortcuts:
Note that by default, IntelliJ IDEA uses regular Spaces instead of tabs. If you have heavily indented files, you may want to optimize their size by enabling the “Use TAB character” option in your Java code style Settings.