Mastering your IntelliJ IDEA: 6 Tricks to help with Refactoring
An integrated development environment (IDE) is a tool developers use daily, so we should master it.
We usually use some shortcuts daily, like CTRL + C and CTRL + V, but we can also use many others, to do many things, like formatting the code, identifying errors, and also using the IDE to suggest solutions for these errors.
All the Keyboards shared here can be used with Windows & Linux, at the end of this article you can have a look at a cheat sheet with the reference for Mac, and also other shortcuts.
Let's talk about 6 tricks that would help us in code refactoring.
1. Rename — Shift +F6
One thing many developers do all the time is give names to things. But let’s be honest, giving a good name is not an easy task, so renaming is something we should do when it is necessary.
For that, we can use Shift + F6 to rename a variable, method, class, or anything we need it.
Another great way is to find and replace everywhere. We can do a Replace in the path using the shortcut: Ctrl + Shift + R. But make sure that you want to replace all of them based on your search criteria.
2. Extract Method — Ctrl + Alt + M
When we are refactoring, we usually need to extract things into methods. Remember that it is a good practice to have small methods, that are concise and easy to read.
For that, you can use the function extract method using the shortcut Ctrl + Alt + M
3. Extract Variable — Ctrl + Alt + V
Creating variables is an easy way to make the code easier to read.
For that, we can use the extract variable function Ctrl + Alt + V which allows us to extract the selected code to a variable, and also replace all the usages with the variable.
4. Extract Constant — Ctrl + Alt + C
A magic number is a value in the code without an obvious meaning, making it hard to understand or refactor. We should avoid magic numbers, for that, we can use constants.
The easiest way to create a constant based on a magic number is using the extract constant function Ctrl + Alt + C.
5. Go to declaration — Ctrl + B, Ctrl + Click
When we are refactoring, we usually need to see who is using the method, or getting inside the method definition.
For this, we can use the function Go to declaration using Ctrl + B or Ctrl + Click, which navigates to the initial declaration of the instantiated class, called method, or field.
6. Select All Occurrences Hotkey — Ctrl + Alt + Shift + J
Sometimes during a refactoring, we need to select all the occurrences of a variable, or method inside a file.
We can use the function Select All Occurrences Hotkey using Ctrl + Alt + Shift + J.
Conclusion
These are only a few shortcuts that can help a lot with our daily activities. I highly recommend you to use them. It can be hard in the beginning because you are not used to it, but the key is consistency.
I hope these shortcuts can help you, and if you know any other shortcuts that you like to use, share them here in the comments 😁😁
Access this cheat sheet to see most of the shortcuts you can use with IntelliJ: IntelliJ Shortcuts Cheat Sheet.