《Good Habits for Great Coding Improving Programming Skills with Examples in Python》读书笔记三

  • 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

    理解incremental developement

  • Comments

    Self-documenting code eliminates the need for many comments.

    Comments should tell you why not how.

  • 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

    理解syntactical sugar

    理解dynamic performance analysis

    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

    理解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 negative if 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 early returns rather than later returns.

  • 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.
    1. Fail fast
    2. Use vertical alignment
    3. Try to write robust code
    4. Avoid global variables
    5. The acronym SESE refers to a function having a singe entry point and a single exit point.
    6. Avoid writing a function that returns a single variable of two different data types
    7. Know your code of operations and your Boolean properties
    8. Be aware that general code is easier to re-use, but specific code is earier to write
    9. Avoid so-called magic numbers which means the numbers represented by constants
    10. Do not repeat code(DRY: Don’t Repeat Yourself)
    11. Do not optimize for speed or memory use as you go
    12. Do not write clever code
    13. Beware the curse of Cambridge professor Charles Babbage
    14. Consider pair programming, as opposed to the usual solo programming
    15. Beware advice from experts
  • References

  1. 理解curse of Cambridge professor Charles Babbage
  2. 理解pair programming vs. solo programming
  3. Stueben, M. Good Habits for Great Coding. (Apress, 2018). doi:10.1007/978-1-4842-3459-4.

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/113751937