In this video tutorial, you will learn how to use Eclipse to refactor Java code. I’ll discuss the following topics:
– What is Refactoring?
– Extract constants and variables
Please subscribe to this channel 🙂
[includeme file=”wp-content/uploads/2015/05/eclipse-tutorial-toc.html”]
Video Transcript
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, I’ll cover the following topics. We’ll first cover what is refactoring? Then, we’ll learn how to extract constants and variables. Okay, so let’s get started.
Time 00:13
What is refactoring? Martin Fowler authored a classic book on refactoring. In the book, he defines refactoring as “a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior,” so some of the benefits of refactoring results in code that is modular and also code that is easier to read and maintain.
Time – 00:48
We will learn how to use refactoring with a sample program. I have a program that would generate a random list of students. The program will also display the list of students. I’ll run the program just so you can see the output. Here, it displays three students. I’ll run the program one more time and we should see a different set of students.
Time – 01:08
Let’s walk through the code to see how it works. At the beginning, we define a list of first names and last names. The code will randomly pick a name later on in the program. We have our main method. We start off by creating an empty list for the students. Then, we initialize a random generator, and then we have a for loop that will walk through and create a random number of students, so we have a for loop going from I to 3. We use code to create a random first name. Also, we get a random last name. Then we get a random age and with this information, we can create a new student object, and then we add this student object to our actual list. Then, the loop continues processing. Once the loop is complete, then we’ll go through and we’ll walk through the loop again, and then we will go and print out the information for each one of the students.
Time – 02:02
The first refactoring we will use is extract constant. This will take a hard coded value and extract it as a constant. I’ll do this with the number 3. I’ll highlight the value. I will right click, select refactor, extract constant. I’ll give the name for the constant. I’ll call it Max Students. I will hit okay, and notice how Eclipse replaces the value 3 with Max Students, and if we look at the top, scrolling up, we will see that Eclipse defined a new constant for us in our file, Max Students equals 3. This makes our code a little bit more maintainable and also easier to read.
Time – 02:41
The next refactoring technique we’re going to take a look at is extract local variables. Let’s look at the piece of code for getting a random name. This code uses the first name’s array and then it uses a random index based on the first name’s array length, so there’s a lot of code going on here and it’s somewhat hard to follow, so what I’d like to do is do it piece by piece.
Time – 03:06
I’d like to first get the random index and then use that to index into the actual array, so I can highlight this section here of random dot next and I can right click and say refactor, extract local variable. Then, I’ll call it first name index, so what this will do is it will actually give us a new variable based on that first name array length, and then I can use this first name index to actually index into the array and get a random first name. This makes the code a little bit more easier to read.
Time – 03:40
I will also repeat the same process for our last name, so again, I’ll highlight the section of code that I want to extract as a local variable. I’ll right click. I’ll choose refactor, extract local variable, and I’ll give the name of last name index, and I’ll hit okay. Again, it’ll create that local variable and then will use it to index into the actual array.
Time – 04:06
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. 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.