O’REILLY Learning「Five Lines of Code」1〜5

https://learning.oreilly.com/library/view/five-lines-of/9781617298318/

1 Refactoring refactoring

This chapter covers
・Understanding the elements of refactoring
・Incorporating refactoring into your daily work
・The importance of safety for refactoring
・Introducing the overarching example for part 1

リファクタリングとは、"何をするのかを変えずにコードを変更する “ことを意味する。

定義
良いコードは、人間が読むことができ、保守が容易で、意図したことを正しく実行する。

定義
リファクタリング-コードを変更することで、その内容を変えずに、より人間が読みやすく、保守しやすいものにすること。

私たちが考えるリファクタリングのタイプは、オブジェクト指向のプログラミング言語での作業に大きく依存している。

2 Looking under the hood of refactoring

This chapter covers
・Using readability to communicate intent
・Localizing invariants to improve maintainability
・Enabling change by addition to speed up development
・Making refactoring part of daily work

本書で使用しているリファクタリングの定義「リファクタリングとは、コードを変更することなく、より良いものにすること」を再確認。

リファクタリングの3つの柱
1.意図を伝えることで、読みやすさを向上させる
2.不変量の局所化による保守性の向上
3.範囲外のコードに影響を与えることなく1と2を行うこと

私が推奨する日々のリファクタリングの2つのバリエーション
・レガシーシステムでは、変更を加える前に、リファクタリングから始めます。その後、通常のワークフローに従います。
・コードに変更を加えた後、リファクタリングを行う。

「常に、自分が見つけた場所より良い場所を残す。」(ボーイスカウトのルール)

Part 1. Learn by refactoring a computer game

3 Shatter long functions

This chapter covers
・Identifying overly long methods with Five lines
・Working with code without looking at the specifics
・Breaking up long methods with Extract method
・Balancing abstraction levels with Either call or pass
・Isolating if statements with if only at the start

DRY(Don’t Repeat Yourself)
KISS(Keep It Simple, Stupid)

本書では最も基本的なルールを紹介する:5行
どのメソッドも5行以上あってはならない、というシンプルなルールです。
本書では、「5行」を究極のゴールとしています。
メソッドは、{と}を除いて、5行以内とする。

「メソッドは一つのことをするべきだ」

「関数の内容は同じ抽象度であるべきだ」

良い名前の普遍的なルールを提供することはできませんが、良い名前が持つべきいくつかの特性を提供することは可能。
・正直であるべきです。機能の意図を説明するものでなければならない。
・完全であるべきです。その機能が行うことをすべて把握する必要があります。
・そのドメインで働く人が理解できるものであること。
 自分が働いているドメインの単語を使う。
 これは、コミュニケーションを効率化し、チームメイトや顧客とコードについて話しやすくなるという利点もある。

4 Make type codes work

This chapter covers
・Eliminating early binding with Never use if with else and Never use switch
・Removing if statements with Replace type code with classes and Push code into classes
・Removing bad generalization with Specialize method
・Preventing coupling with Only inherit from interfaces
・Removing methods with Inline method and Try delete then compile


分からなくもないが、納得が出来ない。

5 Fuse similar code together

This chapter covers
・Unifying similar classes with Unify similar classes
・Exposing structure with conditional arithmetic
・Understanding simple UML class diagrams
・Unifying similar code with Introduce strategy-pattern (P5.4.2)
・Removing clutter with No interface with only one implementation (R5.4.3)

条件は常に純粋であるべき

memoO'REILLY Learning

Posted by shi-n