3 Using Functions, to Break Up Long Functions

This chapter covers:

  • Identifying long methods that are likely doing too much with our first rule: Five Lines [R3.1.1].
  • Working with code without looking at the specifics.
  • Breaking up long methods with the refactoring pattern Extract Method [P3.2.1], and transforming comments into method names.
  • Identifying methods that mix multiple levels of abstraction with the rule: Either Call or Pass [R3.3.1].
  • Separating low-level and high-level operations with Extract Method [P3.2.1].
  • Utilizing good naming to improve readability.
  • Identifying methods that are doing multiple things with our rule if Only at the Start [R3.5.1].
  • Isolating if statements with Extract Method [P3.2.1].

Code can easily get messy and confusing, even when following “Don’t Repeat Yourself” and “Keep It Simple, Stupid”. Some strong contributors to this messiness are:

  • methods are doing multiple different things,
  • we use low-level primitive operation (array accesses, arithmetic operations, etc.), and
  • we lack human-readable text, like comments or good method and variable naming.

Unfortunately, knowing these issues is not enough to know exactly what is wrong, let alone how to deal with the issues.

3.1. Our First Rule, Why Five Lines

3.1.1. Rule: Five Lines

3.2. Our First Refactoring Pattern

3.2.1. Refactoring: Extract Method

3.3. A Second Rule

3.3.1. Rule: Either Call or Pass

3.3.2. Applying It

3.4. Naming

3.5. Refining Further

3.5.1. Rule: if Only at the Start

3.5.2. Applying It

3.6. Summary

sitemap