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

  • Overview

    In writing code, readability comes first, if optimization is not necessary, and if the time cannot be better spent elsewhere.

    ​ - Maybe the absolute rule in programming

    Nice book,it’s acctually for the topic Refactoring, Unit test, TDD etl. But with common words that every begineers could understood.

  • Ways to evaluete functions

    Speed

    Readability: descriptive

    Memory : memory hogs are impractical

    Complexity: cohesion or coupling; understanding or modifying;

  • Trick in Programming

    Introducing a look-up table is a standard trick in programming.

    Python functions are classes. they have class variables. A Python function’s class variable must be declared BELOW the function.

    Using formulas with floats to produce large numbers is a terrible idea, because floats are limited in precision, and therefore eventually will output INCORRECT values. Integers in Pyuthon are limited only by the avaiable memory of the computer.

    You should not read technical material passively, you need to check the logic and math behind these calculations.

    Remembering tricks will help you mature as a programmer. How do we remember them? We write code that uses what we have recently learned.

    Readability, I have been told, is not everything. Neither is breathing, but it does come before whatever comes next.

    Special purpose syntactic constrcuts in programming language are known as “idioms”.

    理解idioms in Programming

    It has often been said that a person does not really understand something until he teaches it to someone else. Acutally, a person does not really understand something until he can teach it to a computer, i.e., express it as an slgorithm.

    Introduce memoization to avoid recalculating the same numbers.

    In Python a default parameter usually should not be set to the empty set (or empty list).

  • Principles

    Create Functions that do only one task.

    Any change in the common code would need to be done only once; which means Don’t Repeat Yourself(DRY).

    Programmers Brian Kernigham and P.J. Plauger once mentioned that the median size of their functions was 15 lines, and the mean was 19 lines.

    函数拆分的太小,如果并未降低复杂度,则会增加调试难度,此时prefer cohesion over coupling.

    即便命令只有一行也写成函数,可能因为提升了readability,比如多重判断。

    The rules of limiting a function to a sinle task and breaking up its parts by using stepwise refinement are important and need to be followed.

    理解stepwise refinement in Programming

    Special cases aren’t special enough to break the rules.

    Morality is valuable so long as it is recognized as a means to an end; It is a good servant, but a terrible master.

    • The key to readability in computer code is self-dcumenting code.

      Class and Variable names should be nouns or noun phrases;

      Function names should be action verbs or verb-object phrases; Or noun describing the reutrned item;

      All Boolean functions should begin with is;

      like function name doIt(verb-object)

  • References

  1. Stueben, M. Good Habits for Great Coding: Improving Programming Skills with Examples in Python. (Apress : Imprint: Apress, 2018). doi:10.1007/978-1-4842-3459-4.

猜你喜欢

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