Lesson 5
Stepping into Refactoring Code in Ruby
Stepping into Refactoring Code

Welcome to our captivating session on refactoring, a powerful tool for tidying up code, much like organizing a messy toy box or finding a faster route to school.

Each line of code is as essential as a brick in a building; clumsy code may result in an unstable structure. Today, we'll focus on enhancing the readability, maintainability, and performance of our code through refactoring.

Recapping Crucial Concepts

Let's briefly revisit a few key concepts:

  • Code Smells: Indicators that our code needs refactoring, akin to clutter calling for cleanup.

  • Refactoring Techniques: We've familiarized ourselves with Extract Method, Rename Method, and Substitute Algorithm techniques in earlier lessons.

  • OOP in Refactoring: We've learned how to leverage Object-Oriented Programming principles to enhance our code's structure.

  • Code Decoupling and Modularization: Methods to make code easier to manage by minimizing dependencies.

We'll use these concepts as guiding stars as we traverse the cosmos of refactoring.

Practice Problem 1: Taming a Complex Function

We'll start with rewriting a complex game score computation function. Let's look at it:

Ruby
1def compute_score(player, monsters) 2 score = 0 3 monsters.each do |monster| 4 if player.power > monster 5 score += player.power - monster 6 else 7 score -= player.power - monster 8 end 9 end 10 score 11end

The parts player.power > monster and player.power - monster recur in this function, indicating room for refactoring. We'll apply the Extract Method and Rename Method to untangle this:

  • We'll extract the scoring logic into a separate method, score_change.
  • We'll rename the original method to compute_game_score.

With these adjustments, our improved code might look something like this:

Ruby
1# New method to calculate score changes. 2def score_change(power, monster) 3 if power > monster 4 power - monster 5 else 6 monster - power 7 end 8end 9 10# Refactored method to calculate the game score. 11def compute_game_score(player, monsters) 12 score = 0 13 monsters.each do |monster| 14 score += score_change(player.power, monster) 15 end 16 score 17end

This refactoring has simplified the function and made it easier to modify in the future.

Practice Problem 2: Refactoring with OOP and Code Decoupling

Let's consider another example where the game has multiple types of monsters. Each monster type behaves differently when encountered by a player.

Ruby
1def monster_reaction(monster_type, player) 2 if monster_type == 'ghost' 3 if player.power > 5 4 puts "The ghost flees in terror!" 5 else 6 puts "The ghost grumbles and attacks!" 7 end 8 elsif monster_type == 'goblin' 9 if player.power > 3 10 puts "The goblin groans and retreats!" 11 else 12 puts "The goblin hacks with its sword!" 13 end 14 end 15 # more monster types... 16end

This scenario could also benefit from refactoring using OOP and Code Decoupling:

  • First, we'll introduce a class Monster with a method reaction that could be overridden by each type of monster.
  • Then, we'll create child classes Ghost and Goblin that inherit from Monster and implement their own reaction methods.

Under the revised structure, our game code would look like this:

Ruby
1class Monster 2 def reaction(player) 3 # Default behavior (if any) or abstract method 4 end 5end 6 7class Ghost < Monster 8 def reaction(player) 9 if player.power > 5 10 puts "The ghost flees in terror!" 11 else 12 puts "The ghost grumbles and attacks!" 13 end 14 end 15end 16 17class Goblin < Monster 18 def reaction(player) 19 if player.power > 3 20 puts "The goblin groans and retreats!" 21 else 22 puts "The goblin hacks with its sword!" 23 end 24 end 25end 26 27monsters = [Ghost.new, Goblin.new, Ghost.new, Goblin.new] 28monsters.each do |monster| 29 monster.reaction(player) 30end

Now, our code dealing with multiple monsters is easier to manage and can be extended to accommodate more types of monsters.

Wrapping Up and Looking Ahead

Phew! We've done an excellent job working through two practical problems, enhancing our refactoring skills, and learning how to identify code smells and apply refactoring techniques.

The more you practice, the better you'll become at spotting code that could benefit from refactoring. Brace yourself for more practice tasks, and remember, always keep your code lean and efficient!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.