π Bookshelf
π Contents
π Prev
π Next
Chapter 05: Formatting
-
We would like a source file to be like a newspaper article. The name should be simple but explanatory. The name, by itself, should be sufficient to tell us whether we are in the right module or not. The topmost parts of the source file should provide the high-level concepts and algorithms. Detail should increase as we move downward, until at the end we find the lowest level functions and details in the source file.
-
If openness separates concepts, then vertical density implies close association. So lines of code that are tightly related should appear vertically dense.
-
Concepts that are closely related should be kept vertically close to each other. Clearly this rule doesnβt work for concepts that belong in separate files. But then 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.
-
Variables should be declared 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, on the other hand, should be declared at the top of the class. This should not increase the vertical distance of these variables, because in a well-designed class, they are used by many, if not all, of the methods of the class.