Welcome to our captivating session on refactoring, a powerful tool for tidying up code, much like organizing a toolbox for efficiency.
In C++, each line of code is akin to a foundational component in a complex system, and poorly organized code can lead to unwieldy and unstable software. Today, we'll focus on enhancing the readability, maintainability, and performance of our code through refactoring.
Let's briefly revisit a few key concepts:
-
Code Smells: Indicators that our code needs refactoring, such as tangled dependencies or redundancy that call for cleanup.
-
Refactoring Techniques: We've familiarized ourselves with
Extract Method
andRename Method
techniques in earlier lessons, using C++ functions and member functions to maintain clarity and abstraction. -
OOP in Refactoring: We've harnessed Object-Oriented Programming principles to enhance our code's structure.
-
Code Decoupling and Modularization: Methods to manage code by minimizing dependencies while organizing through header and implementation files.
We'll use these concepts as guiding stars as we journey through the refactoring process in C++.
Let's begin by refactoring a complex game score computation function. Observe the initial scenario in C++:
Here, the repetition of the player.getPower() > monster
and player.getPower() - monster
calls suggests an opportunity for refactoring. We'll apply the Extract Method
and Rename Method
to improve this:
- We'll extract the scoring logic into a separate function,
calculateScoreChange
. - We'll rename the original function to
computeGameScore
.
The refactored code will enhance clarity and modifiability:
This refactoring makes the code easier to understand and adapt for future changes.
Next, consider a scenario where a game includes various types of monsters, each reacting differently when encountered by a player. Initially, our code might look like this:
To improve this, we'll refactor using OOP principles and Code Decoupling:
- We'll introduce a base class
Monster
with a virtual methodreaction
. - We'll create derived classes
Ghost
andGoblin
inheriting fromMonster
, each implementing its ownreaction
method.
In the revised structure, our code will look like this:
Now, managing different monster types is cleaner, and adding new types becomes straightforward through the extension of the class hierarchy.
Excellent work! We've refactored two practical problems, sharpening our ability to identify code smells and apply effective refactoring techniques.
As you continue to practice, you'll become more adept at recognizing and improving sections of code that can be refactored. Stay tuned for more practice tasks, and keep your code clean, efficient, and maintainable!
