"Hello again."


"Today I'm going to tell you about refactoring. Refactoring is changing the code in a project without changing its functionality."

"But how is that possible?"

"Well, in the simplest cases, we could rename variables and/or methods. After all, changing the name of a variable won't make the program work differently, will it?"

"Of course not."

"You can also break large methods into several smaller ones."

"You can pull repeated code snippets into a separate method."

"Some functions can be declared static and then moved into utility classes."

"But this is a narrow interpretation of refactoring."

"Refactoring sometimes means rewriting (improving) a project's architecture without adding new functionality. This is refactoring in the broad sense of the term."

"IntelliJ IDEA became the most popular integrated development environment (IDE) specifically because it was the first to introduce very powerful refactoring tools."

"What are these magical tools?"

Magic trick #1: Change a method's name.

"Imagine that your code has a method that's called from 100-500 different places. You've decided to change its name into something more understandable. Let's say it is run(), and you want it to be runDownloadTaskAsync(). How quickly can you do that?"

"Well, first you need to change the method's name, then find all the places in the program where that method is called, and change the name there too."

"And how would you find those places?"

"I'd just run the program and IntelliJ IDEA would show me all of the places where a non-existent method is called."

"Okay. But now suppose that every method has an explanatory comment (JavaDoc) that explains what the method does — and the old method name is written there."

"I'd change the comments too."

"But there may also be variables whose names are associated with the method's name. It would be good to change those too:"

Before After
Task task = manager.run();
Task asyncTask = manager.runDownloadTaskAsync();

"Yeah, it would be good to change the names of those variables too. It wouldn't hurt."

"Well, all of this can be done in just a few seconds using IntelliJ IDEA!"

"You place the cursor on the name of the method (or click it with the mouse), and then press Shift+F6 and simply start typing the desired method name."

"Here's an example of editing a method name."

"Start editing:"

IDEA: refactoring - 1

"Specify a new name:"

IDEA: refactoring - 2

"Simply enter a new name, press enter, and that's it. This method will be renamed everywhere it is called in the project."

"IntelliJ IDEA also usually asks if you want to rename variable names and method names in comments. Just click 'Yes' and it will change everything."

"What's more, if the project compiled before this refactoring operation, then it is guaranteed to compile after the refactoring."

"Sounds very promising."

"By the way, you can change variable names the same way. After clicking a name, simply press Shift+F6 — You can then enter a new name, and IntelliJ will change the variable name wherever it's used."

"If the variable is a field in a class and has a getter and a setter, then the names of the getter and setter will also be changed to match the variable's new name."

"I tried it using variables. Everything works just like you said, Ellie. Refactoring is awesome!"

"Do you think that's all there is to refactoring? Refractoring is such a broad topic — we haven't even scratched the surface."

"Wow. What else is there?"

Magic trick #2: Extract a variable.

"Sometimes certain expressions are repeated so often in code that you'll want to move them into a separate variable. For example:"

public void printInfo(User user)
How you want it to look:
public void printInfo(User user)
 Address address = user.getProfile().getAddress();



"And code can be much more complicated, with a lot of repeated parts."

"Programmers don't write it that way intentionally. But you'll often need to add something to a method, then something else — and over time the number of repetitions goes through the roof."

"By creating a separate variable, we can give it a proper name and improve the readability of the code."

"For example, in the example above, perhaps we aren't talking about a home address. Maybe we're talking about the address of an emergency contact. Then you could call this variable emergencyContactAddress, instead of just address. Then, a programmer seeing this code for the first time will understand what's going on here."

"Yes, I agree. Adding such variables makes sense."

"So, how do you do it?"

"Moving an expression into a separate variable is very simple."

Step 1: Use the mouse to select the expression.

IDEA: refactoring - 3

Step 2: Press Ctrl+Alt+V

IDEA: refactoring - 4

"A window opens where IntelliJ IDEA asks us whether we want to replace only the selected expression or all instances of the expression (4 occurrences)?"

"Select the second option to replace all occurrences (Replace all 4 occurrences)"

Step 3: Press Enter.

IDEA: refactoring - 5

"IntelliJ IDEA will prompt you to enter a variable name. It will also make its own suggestion for the name. Not bad, eh?"

"Uh-huh. Right on. We were also planning on naming the variable 'address'. How did it know?"

"By using the name of the last method in the expression, which returns an address. Thus, it is likely that the variable will be used to store an address."

"That worked out really well. Great stuff, Ellie."

Magic trick #3: Extract code into a separate method.

"But we could have done something else. We could have declared a new method, e.g. printAddress(), and moved all of this code into it."

"Let's try doing that."

Step 1: Select the 4 lines of code that use the address variable:

IDEA: refactoring - 6

Step 2: press Ctrl+Alt+M

IDEA: refactoring - 7

"IntelliJ IDEA determines which variables the method will need, and suggests what it should look like. "The only thing left is to enter a name for the method."

Step 3: Enter printAddress as the method name and press Enter.

IDEA: refactoring - 8

"How do you like that?"

"It's awesome. Not only did IntelliJ IDEA extract the code into a separate method, it also added all the required variables. What's more, it guessed all of the names correctly."