Refactoring & Code Smells

Definition of Refactoring

  • Process of altering source code while preserving its functionality.

  • Purpose: Improve maintainability, especially enhancement.

Key Aspects of Refactoring

  • What Refactoring Is:

    • Small, safe, incremental changes.

    • Improves code design to be more understandable and flexible.

    • Breaking large methods into smaller ones.

    • Renaming variables and parameters meaningfully.

    • Moving responsibilities to appropriate classes.

    • Creating interfaces for classes.

  • What Refactoring Is Not:

    • An excuse to make unrelated enhancements.

    • Merely adding features or improving error handling.

    • Filling gaps without clear need.

Importance of Refactoring

  • Prevents design decay and code messiness.

  • Simplifies code, increases readability and maintainability.

  • Helps find bugs and reduces debugging time.

  • Indicates a learning process about the application.

Design Decay

  • Occurs in software systems where there is an absence of an upfront design.

  • It describes a condition where code may be functionally correct and satisfying customers today but where the underlying implementation has become inefficient from a maintenance and evolution perspective.

When to Refactor

  • When you add functionality

  • When you learn something about the code

  • When you fix a bug

  • When the code smells

When not to Refactor

  • When tests aren’t passing

  • When you should just rewrite the code

  • When you have impending deadlines

Problems with Refactoring

  • Over-refactoring can lead to endless tinkering.

  • Refactoring code when the tests don’t work leads to potentially dangerous situations.

  • Databases can be difficult to refactor.

  • Refactoring published interfaces can cause problems for the code that uses those interfaces.

Reasons For Reluctance To Refactor

  • Lack of understanding

  • Short-term focus

  • Not paid for overhead task like refactoring

  • Fear of breaking current program

Types of Refactoring (Fowler's List)

  • Add Parameter

  • Change Association Direction

  • Extract Method

  • Extract Class

  • Rename Method

  • Replace Magic Number with Constant

Code Smells

  • Definition: A hint that something may be wrong in the code.

  • Categories include:

    • Comments (indicating unclear code)

    • Duplicated Code

    • Long Methods

    • For Testers Only (methods solely for testing)

How to Identify Code Smells

  • Experience

  • Regular code review

  • Tools that generate metrics. e.g. cyclomatic complexity

Cyclomatic Complexity

  • Definition: Metric that measures the structural complexity of a method (decision points).

  • Formula: CC=extnumberofdecisionpoints+1CC = ext{number of decision points} + 1

  • Impact: Higher complexity means more tests required, complicating maintenance.

Tool Support

  • Tools to identify bad smells and control metrics in a software

  • Examples include PMD, FindBugs, and JDeodorant among others.