The Practice of Programming

In the preface, the author illustrates four basic principles of programming - simplicity, clarity, generality, automation.

I suppose that everyone has his own programming experience and preference, but with predecessors' valuable guidence, chances are that we can write more beautiful code.

Pieces of summaries and abstracts are organized as follows:


Charpter 1 STYLE

The purpose of style is to make the code easy to read for yourself and others, and good style is crucial to good programming.

  Names

  • descriptive names for globals, short names for locals
  • names with p for pointers; initial capital letters for Globals; all capitals for CONSTANTS; active names for functions
  • be consistent

  Expressions and Statements

  • avoid negative expressions possibly
  • use parentheses in mixed unrelated operators
     the relational operators (< == !=) have higher precedence than the logical operators (&&  || );
    the logical operators bind tighter than assignment ( = );
    the bitwise operators (& |) have lower precedence than relational operators ( == )
  • break up complex expressions
  • be careful with side effects:  (e.g. the following expression is wrong)
    scanf("%d %d", &yr, &profit[yr]);X  

  Consistency and Idioms

  • Use a consistent indentation and brace style
  • Use idioms for consistency
    Wrong code:
    gets(buf);X  //never use 'gets', 'fgets' is better
    p = malloc(strlen(buf));
    strcpy(p, buf);X  //strlen does not count the '\0' that terminates a string, while strcpy copies it
    Right code:
    p = malloc(strlen(buf)+);
    strcpy(p, buf);
  • the return value from malloc, realloc, strdup, or any other allocation routine should always be checked

  Function Macros

  • One of the most serious problems with function macros is that a parameter that appears more than once in the definition might be evaluated more than once

  Magic Numbers

  • By giving names to the principal numbers in the calculation, we can make the code easier to follow

  • Define numbers as constants, not macros

    const int MAXROW = . MAXCOL = ;
    static final int MAXROW = , MAXCOL = ;
    C also has const values but they cannot be used as array bounds, so the enum statement remains the method of choice in C.
  • Use the language to calculate the size of an object

    sizeof (int)
    sizeof(array[])
    sizeof(buf)

  

  Comments

  • Comment functions and global data
  • Sometimes code is genuinely difficult, perhaps because the algorithm is complicated or the data structures are intricate. In that case, a comment that points to a source of understanding can aid the reader

  • Don't comment bad code, rewrite it

  • Don't contradict the code. When you change code, make sure the comments are still accurate

Book Review of "The Practice of Programming" (Ⅰ)的更多相关文章

  1. Book Review of “The practice of programming” (Ⅳ)

    The practice of programming Chapter 4 Interfaces A good programmer should always be good at designin ...

  2. Book Review of “The practice of programming” (Ⅲ)

    The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...

  3. Book Review of “The practice of programming” (Ⅱ)

    The practice of programming Chapter 2 Algorithms and Data Structures Searching sequential search (li ...

  4. 2015年第2本(英文第1本):《The Practice of Programming》

    2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...

  5. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  6. FRP represents an intersection of two programming paradigms.

    FRP represents an intersection of two programming paradigms. Functional programming Functional progr ...

  7. [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  8. 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  9. (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    原文:http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html 1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也 ...

随机推荐

  1. mysql_real_connect 端口号说明

    mysql_real_connect语法:  C++ Code  12345678   MYSQL * mysql_real_connect(MYSQL * mysql,                ...

  2. AWS系列-修改RDS时间(time_zone)

    1.1 需求 数据库时间和北京时间相差8小时 开发人员发现app的时间与国内时间相差12小时,需要修改RDS的time_zone参数 1.2 打开RDS参数组 1.3 创建新的参数组 由于默认的参数组 ...

  3. C# .net 多线程中集合数据同步

    from:http://www.cnblogs.com/GavinCome/archive/2008/04/09/1145250.html C# .net 多线程中集合数据同步(转) 集合类通常不是线 ...

  4. 在Windows端安装kafka 提示错误: 找不到或无法加载主类 的解决方案

    在配置好kafka的server.properties文件后,cmd进入命令窗口输入命令:.\bin\windows\kafka-server-start.bat config\server.prop ...

  5. 利用wireshark抓取远程linux上的数据包

    原文发表在我的博客主页,转载请注明出处. 前言 因为出差,前后准备总结了一周多,所以博客有所搁置.出差真是累人的活计,不过确实可以学习到很多东西,跟着老板学习做人,学习交流的技巧.入正题~ wires ...

  6. HDU 1232 畅通工程(Kruskal)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. docker运行oracle11g

    image docker pull registry.cn-hangzhou.aliyuncs.com/qida/oracle-xe-11g 或者自己自动添加表 create role test_ro ...

  8. 《挑战程序设计竞赛》2.6 数学问题-辗转相除法 AOJ0005 POJ2429 1930(1)

    AOJ0005 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005 题意 给定两个数,求其最大公约数GCD以及最小公倍数LCM. ...

  9. Outlook Top of Information Store

    Actually I got to thinking this might make a good blog post so I took a closer look - Try this: On t ...

  10. 区块链 block chain 去信任

    去中心化:不以参与交易的任何一方为中心 去信任:假定参与交易的任何一方都是不可信任的 区块链受到关注的原因 去中心化.去信任化.智能合约等,正好满足未来互联网持续发展所要求的信息的盖度自动化和高度程序 ...