🔧

Refactoring Techniques for Cleaner Code

Jul 31, 2024

Refactoring Code: Lecture Notes

Introduction to Refactoring

  • Definition: Refactoring is a technique for restructuring existing code without changing its behavior.
  • Importance: It improves the quality of existing code, making it easier to understand and modify for future updates.
  • Key Point: Always ensure the behavior of the code remains unchanged during refactoring.

Safety in Refactoring

  • Best Practice: Have a comprehensive suite of tests to verify that behavior has not changed.
  • Testing: Essential for ensuring safe refactoring; run tests before and after making changes.

Refactoring Exercise Overview

  • Project: Tennis Refactoring Coding Assignment.
  • Repository: Contains several implementations in various programming languages; focus on C implementation.
  • Components:
    • Tennis class library with an iTennisGame interface and six implementations.
    • Test project validating these implementations.

Tennis Scoring System

  • Scores are represented as: 0 (love), 15, 30, 40.
  • Special cases:
    • Advantage: When both players have 40.
    • To win: Reach 40 before the opponent or win by two points after deuce.

Implementation Walkthrough

  1. onePoint Method:

    • Updates the state of the game when a player wins a point.
    • Suggested improvements:
      • Add curly braces for clarity in if statements.
      • Remove unused player name fields.
      • Consistent naming conventions (e.g., use underscores for private fields).
      • Use player names from fields instead of hardcoded names.
      • Make player name fields read-only.
  2. getScore Method:

    • Initially lengthy (50 lines).
    • Suggested improvements:
      • Refactor into switch expressions for concise logic.
      • Immediately return results instead of assigning to variables.
      • Introduce descriptive variable names for scores.
      • Use constants for numeric values (e.g., scores).
  3. Refactoring Techniques:

    • Use extract method refactoring to simplify complex logic.
    • Remove redundant code and unused methods.
    • Ensure clarity in condition checks and variable usage.

Other Implementations

  • Tennis Game 2:
    • Complex getScore method; simplify and remove unused methods.
  • Tennis Game 3:
    • Update field names for clarity.
    • Extract scoring logic into static fields for performance.
  • Tennis Game 4:
    • Complicated structure; difficult to refactor due to numerous classes.
  • Tennis Game 5:
    • Concise implementation; challenge to improve without loops.
  • Tennis Game 6:
    • Focus on extracting methods to reduce complexity.

Conclusion

  • Final Thoughts: Refactoring is an essential skill that improves code quality and maintainability.
  • Next Steps: Suggested watching more videos on refactoring best practices and related courses on clean architecture.

Additional Notes

  • Engaging with refactoring exercises enhances understanding and application of coding principles.
  • Practice makes perfect; continual refactoring practice is encouraged.