Hello, explorer! Today is about refactoring. Consider it as organizing your favorite toys in the toybox. We're going to learn about the Extract Method
, Rename Method
, and Substitute Algorithm
refactorings. Refactoring helps us make our code cleaner and neater while keeping the functionality the same!
Imagine having a complex map. Refactoring transforms it into simpler directions. Our code gets rearranged to make it more readable and efficient without altering what it does. Let's consider a small code snippet before and after refactoring:
Ruby1# Before refactoring 2def calculate(total, quantity) 3 price = total / quantity 4 tax = price * 0.2 5 total_price = price + tax 6 total_price 7end 8 9# After refactoring 10def calculate_total_price(total, quantity) 11 price = calculate_price(total, quantity) 12 tax = calculate_tax(price) 13 price + tax 14end 15 16def calculate_price(total, quantity) 17 total / quantity 18end 19 20def calculate_tax(price) 21 price * 0.2 22end
Both versions of the code do the same thing, but the latter is simpler and easier to understand!
Imagine a large recipe for a complete breakfast. The Extract Method
technique is like having separate recipes for eggs, toast, coffee, etc., instead of one large recipe. Take a look at this code:
Ruby1# Before refactoring 2def greet_user(username) 3 username = username.strip.downcase # Prepare the username 4 message = "Hello, #{username}!" # Prepare the message 5 message # Return the prepared message 6end 7 8# After refactoring 9def clean_username(username) 10 username.strip.downcase # Returns a cleaned version of the username 11end 12 13def greet_user(username) 14 username = clean_username(username) # Clean the username 15 message = "Hello, #{username}!" # Prepare and return the message 16 message 17end
Here, we moved the username preparation from greet_user
into its own function clean_username
. Nice and tidy!
Clear method names make it easy to understand our code, just as clear street names make navigating a city more accessible. Let's look at renaming a method:
Ruby1# Before refactoring 2def fx(x) 3 3.14 * (x ** 2) # Calculates a value that is pi times the square of x 4end 5 6# After refactoring 7def calculate_circle_area(radius) 8 3.14 * (radius ** 2) # Calculates the area of a circle with a given radius 9end
Renaming the function fx
to calculate_circle_area
makes it easier to understand its purpose.
Substitute Algorithm
involves replacing a part of a code (an algorithm) with a simpler one, analogous to discovering a faster route to school. Here's an example:
Ruby1# Before refactoring 2def find_smallest(numbers) 3 smallest = nil 4 numbers.each do |num| 5 # Assigns the first value as the smallest one and then checks the next ones 6 if smallest.nil? || num < smallest 7 smallest = num 8 end 9 end 10 smallest 11end 12 13# After refactoring 14def find_smallest(numbers) 15 numbers.min # Returns the smallest number from 'numbers' 16end
Just like the min
method in Ruby, it performs the same job as our previous function, but with less code.
Great work! We've learned how to use the Extract Method
, Rename Method
, and Substitute Algorithm
to keep our code clean and efficient. Now, it's time for some hands-on practice with real examples. Remember, practice makes perfect. Let's do some refactoring!