Java Eclipse Tutorial – Part 6.1: Refactoring Code (Extract Constants and Variables)

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 šŸ™‚

Download Java Source Code

 

# Video Length
1 Installing the Eclipse IDE 03:18
2 Create a Java Application with Eclipse 03:44
3 Exploring the Eclipse User Interface 04:50
4.1 Searching and Navigating Source Code (part 1) 04:18
4.2 Searching and Navigating Source Code (part 2) 06:06
5 Generating Java Source Code 05:12
6.1 Refactoring Code - Extract Constants and Variables 04:13
6.2 Refactoring Code - Extract Methods, Rename Methods and Variables 04:19
7 Debugging Java Source Code 04:34
8 Adding JAR Files 05:17
9 Running JUnit Tests 05:08
10 Importing Projects 01:45
11 Exporting Projects 01:34
12 Setting Eclipse Preferences 04:09
Total 58:27

 

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.

Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Share

Leave a Reply