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
ifOnly at the Start [R3.5.1].
ifstatements 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.