Navs
Code Smells Catalog
Special Case

Last Revision — April 19, 2022

1 Min Read


  • Also Known As

    Complex Conditional

  • Obstruction

    Change Preventers

  • Occurrence

    Conditional Logic

  • Expanse

    Within a Class

  • Related Smells

    - Null Check (family)
    - Temporary Field (caused)

  • History

    William C. Wake in book (2004): "Refactoring Workbook"

Special Case

Wake addresses the complex conditional situation as a Special Case code smell with two symptoms - a complex if statement and/or value checking before doing the actual work [1].

Causation

There was a need for a special case to handle. A hotfix that was never adequately fixed could also be the reason for the smell.

Problems:

Comprehensibility

The method is doing a specific task, but there is "one special case" to consider.

Increased Test Complexity

Special case has to have an extra special test.

Example

Smelly

def parse_foo(foo: Foo) -> Goo:
    if 'zoo' in foo.sample_attribute:
        ...
        ...
        ...

    ...
    ...
    ...
    ...

Refactoring

  • Consolidate Conditional Expression
  • Replace Conditional with Polymorphism
  • Introduce Null Object
  • Replace Exception with Test

Exceptions

Recursive methods always have to have a base case to stop the recursion.


Sources
  • [1], [Origin] - William C. Wake, "Refactoring Workbook" (2004)