-
Overview
Page 94-180
There are two competing design philosophies in computer programming:
- Do the right thing : precision
- worse is better : simplicity
The goal of perfection in school programs can be pedantic.
The words font and type or typeface are often used interchangeably.
A font is associated with an attribute of a typeface.
-
Step-wise Refinement
-
Comments
Self-documenting code eliminates the need for many comments.
Comments should tell you
why
nothow
. -
Stop Coding
The moment you start to feel confused, stop coding.
If your program starts to get so complicated that you can’t understand it, then you must refactor or start over completely.
You must spend time thingking-before-coding about a project that you find complicated.
One way to outline on a keyboard is to just write the function names :stubs with no code inside, or mocks, which return bogus data.
def doIt(x): # <-- STUB pass def doIt(x): # <-- MOCK return 0
Failing to plan is planing to fail: Think twice, code once.
Code in haste and debug forever. Remember that one week of debugging can save an entire hour of planning.
-
Testing
Eternal vigilance is the price of liberty. When writing code this means test as you go, test each key function immediately after it is written, not waiting until then entire program is written.
Early testing is probably the best idea ever for reducing coding errors.
Testing as you go is called systematic testing and incremental prototyping.
Keep our code simple, and test as we go.
The may to discove errors is :
- to run test data
- place error traps
-
Defensive Programming
-
Refactoring
Redesigning, not to optimize, but to make working code easier to understand, to debug, to modify, and to integrate with other code is common engough to have a name: refactoring.
Breaking complex code into parts that are easier to understand is called “factoring”(aka decompostion) a term invented by programmers in the 1980s.
With simple
if
statements, avoid negativeif
tests where you can, because negations are harder to parse than positive statements.Complications resulting from multiple nested
if
statements were the single most common cause of logic bugs.If your code has several
returns
, consider rewriting it to have earlyreturns
rather than laterreturns
. -
Write the Tests First
The first step in testing is called domain testing: testing of variables, constraints, and correct types.
Next is unit testing (aka functional testing aka white-box testing): the testing of individual functions;
Finally there is black-boc testing: the testing of the entire program.
-
Expert Advice
- The most important tip is to read other people’s code, especially well-written code.
- Fail fast
- Use vertical alignment
- Try to write robust code
- Avoid global variables
- The acronym SESE refers to a function having a singe entry point and a single exit point.
- Avoid writing a function that returns a single variable of two different data types
- Know your code of operations and your Boolean properties
- Be aware that general code is easier to re-use, but specific code is earier to write
- Avoid so-called magic numbers which means the numbers represented by constants
- Do not repeat code(DRY: Don’t Repeat Yourself)
- Do not optimize for speed or memory use as you go
- Do not write clever code
- Beware the curse of Cambridge professor Charles Babbage
- Consider pair programming, as opposed to the usual solo programming
- Beware advice from experts
-
References
- 理解curse of Cambridge professor Charles Babbage
- 理解pair programming vs. solo programming
- Stueben, M. Good Habits for Great Coding. (Apress, 2018). doi:10.1007/978-1-4842-3459-4.