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. VC++Debug查看堆对象内容,即使符号已经超出作用范围

    Sometimes you'd like to watch the value of an object (on the heap) even after the symbol goes of sco ...

  2. sql privot

    http://www.studyofnet.com/news/295.html 本文导读:T-SQL语句中,Pivot运算符用于在列和行之间对数据进行旋转或透视转换,PIVOT命令可以实现数据表的列转 ...

  3. 设计模式之简单工厂模式(Simply Factory)摘录

    从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫静态工厂方法(Static Factory Method)模式.但不属于23种GOF设计模式之中的一个.简单工厂模式是由一个工厂对象决定创建出 ...

  4. Java中匿名内部类

    匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...

  5. 广药帅气外教~看妹纸如何HOLD住

    看他吉他弹的好,看来我要努力练习我的吉他了,一个月了没有任何长进啊. shit~!

  6. java获取地址全路径

      String basePath = request.getScheme()+"://"+request.getServerName()+":"+reques ...

  7. windows 系统如何安装 mysql 8.0.15 数据库?

    windows 系统如何安装 mysql 8.0.15 数据库? 1. 下载安装包 下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0. ...

  8. 基于Requests和BeautifulSoup实现“自动登录”

    基于Requests和BeautifulSoup实现“自动登录”实例 自动登录抽屉新热榜 #!/usr/bin/env python # -*- coding:utf-8 -*- import req ...

  9. Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡

    一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...

  10. Vijos p1303导弹拦截(LIS+贪心)

    传送门:https://vijos.org/p/1303 背景 实中编程者联盟为了培养技术精湛的后备人才,必须从基础题開始训练. 描写叙述 某国为了防御敌国的导弹突击,研发出一种导弹拦截系统. 可是这 ...