In this video tutorial, you will learn how to use Eclipse to refactor Java code. I’ll discuss the following topics:
– Extract Methods
– Rename methods and variables
Please subscribe to this channel 🙂
Time – 00:00
Hi, this is Chad (shod) with luv2code.com. Welcome back to another tutorial on Eclipse. In this video, you will learn how to use Eclipse to refactor Java source code.
Time – 00:12
In this video, we’ll cover the following topics: we’ll learn about extracting methods and we’ll also learn about renaming variables and methods. Let’s get started.
Time – 00:21
All right, let’s move forward to our next refactoring technique, and that’s on extracting method. One thing that we can notice here is that in this for loop, a bulk of the code in here is for actually getting the random information and creating the student. Then, finally, on line 43 is where we actually add the student into the actual list.
Time – 00:46
What I’d like to do is take this code and move it into a separate method, just to make the code more modular and also make it easier to read. I can highlight this section of code, I can right-click, and I can say Refactor, Extract Method. Now I can give the actual name of the method that I want to use.
Time – 01:10
Here I’ll call the method Create Random Student. Notice how the method will automatically pass in that random number generator that we need. I’ll hit Okay. Now it’ll create a new method for me.
Time – 01:23
Notice here that it has this method called Create Random Student. I can navigate or open this declaration and see the actual coding for this. Now there’s a separate method or new method, Create Random Student. It does all the low-level work that we had before: getting the random first name, last name, age, and creating the student object.
Time – 01:44
Notice here that it actually returns a student that it’s created. In our actual looping code in our main method, we have our for loop. Now for the student, we simply call Create Random Student. Then, from there, we simply add that student to the list. Now our code is easier to read and maintain.
Time – 02:02
We can also apply the same refactoring for the loop for displaying the students. I’ll move down to this section of code, I’ll highlight it, right-click, select Refactor, Extract Method, and I’ll give the method name of Display Students.
Time – 02:20
Again, notice how it passes in the appropriate parameters that we need. In this case, they’re going to pass in the list of students that we need to display. I’ll hit Okay.
Time – 02:28
Now we’ll simply have this method called Display Students. This method is defined in the same class, and we’ll see it here on the next set of lines. It simply has that for loop from before, and it’ll print out the student. Again, we’ve applied refactoring to actually extract a given method.
Time – 02:45
The next refactoring technique we’re going to use is rename for variables and methods. So you can easily rename a variable or method, an Eclipse will update all the references for you.
Time – 02:55
In this example, I’m actually going to rename a method. What I’ll do here is I’ll choose a method such as display students and, say, for example, I don’t like that method name anymore. I can select a method, right-click. I can go to Refactor and I can choose Rename. I can give it the new name.
Time – 03:12
Here I just type the name in directly in the editor. I’ll say Show and hit enter. The method now has a new name and also all calls to that method here also makes use of the new name. There’s no need for you to manually go through and search and replace on your code. That’s a way of refactoring or renaming methods.
Time – 03:35
You can also rename variables. In this example, for the student list, say, for example, I want to give it a new name. I want to rename it to something else. Notice how Eclipse will highlight all references for this list in the method.
Time – 03:48
For this variable, I’ll choose student list. I’ll go to Refactor, Rename, and I’ll just call it Data, and hit Enter. Notice here Eclipse updates all references to this variable data Data accordingly. Again, Eclipse really helps you out with renaming or restructuring or refactoring your code.
Time – 04:11
This wraps up our video on refactoring. You learned how to use Eclipse to perform various refactoring techniques to make your code modular and easier to read and maintain.
Time – 04:20
Please subscribe to our channel to view more videos on Eclipse and Java. Also, visit our website, luv2code.com to download the Java source code used in this video.