Index
Day 4 - Formatting
Vertical formatting
-
File size = class size
-
Small files are usually easier to understand than large files
-
Newspaper metaphor:
You read the newspaper vertically. At the top you expect a headline that tell you what the story is about and allows you to decide whether it is something you want to read. The topmost parts should provide high-level concepts and algorithms. Details should increase as we move downward
-
Each group of lines represents a complete thought. Those thoughts should be separated from each other with blank lines
-
Lines of code that are tightly related should appear vertically dense, without having to move my head or eyes much
-
Concepts that are closely related should be kept vertically close to each other.
Closely related concepts should not be separated into different files unless you have a very good reason. Indeed, this is one of the reasons that protected variables should be avoided (use get/set instead!)
-
Variable declarations should be as close to their usage as possible. Because our functions are very short, local variables should appear at the top of each function
-
Instance variables should be declared at the top of the class (or at the bottom: C++)
-
Dependent functions: if one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible. This gives the program a natural flow
-
Conceptual affinity: certain bits of code want to be near other bits. They have a certain conceptual affinity. The stronger that affinity, the less vertical distance there should be between them