Book Review of "The Practice of Programming" (Ⅰ)
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 itRight 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" (Ⅰ)的更多相关文章
- Book Review of “The practice of programming” (Ⅳ)
The practice of programming Chapter 4 Interfaces A good programmer should always be good at designin ...
- Book Review of “The practice of programming” (Ⅲ)
The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...
- Book Review of “The practice of programming” (Ⅱ)
The practice of programming Chapter 2 Algorithms and Data Structures Searching sequential search (li ...
- 2015年第2本(英文第1本):《The Practice of Programming》
2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...
- net programming guid
Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...
- FRP represents an intersection of two programming paradigms.
FRP represents an intersection of two programming paradigms. Functional programming Functional progr ...
- [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...
- (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
原文:http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html 1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也 ...
随机推荐
- Lenna图-莱娜·瑟德贝里
莱娜·瑟德贝里(瑞典文:Lena Soderberg),1951年3月31日出生于瑞典,在1972年11月期的<花花公子>杂志中,她化名为莱娜·舍布洛姆,成为了当期的玩伴女郎. 她的中间折 ...
- VC++ 给你的代码强制加一个硬断点
类似与Javascript的 debugger; Hard code a debugger breakpoint If you need to insert a hard breakpoint in ...
- poj 3653(最短路)
题目链接:http://poj.org/problem?id=3653 思路:题目意思很简单,就是二维平面上的图,要求起点到终点的最短路.建图略坑,需要坐标映射,化二维为一维.然后就是Dijkstra ...
- IntelliJ Idea Hide excluded folders 隐藏或显示你需要的文件夹
IntelliJ Idea,以前用idea时,经常maven编译就出现了exclude下的文件夹通常是target,如何隐藏自己不想看见的文件夹,或显示偶尔会用到的文件夹 点击齿轮右下小标 选中文件夹 ...
- cocos3.x 实现android沉浸式模式(全屏,隐藏导航栏即虚拟键)
只有在Android 4.4及以上系统才支持沉浸式模式,修改 AppActivity代码如下: @Override public Cocos2dxGLSurfaceView onCreateView( ...
- [MySQL] 变量(参数)的查看和设置 [转]
[MySQL] 变量(参数)的查看和设置 类似于Oracle的参数文件,MySQL的选项文件(如my.cnf)用于配置MySQL服务器,但和Oracle叫法不一样,在MySQL里, 官方叫变量(Var ...
- db2 相关命令
db2ilist ---列出主机所有实例 db2icrt instanceName ---创建实例名为instanceName的实例 db2idrop instanceName ---删 ...
- 160711、Java 多线程核心技术梳理
本文对多线程基础知识进行梳理,主要包括多线程的基本使用,对象及变量的并发访问,线程间通信,lock 的使用,定时器,单例模式,以及线程状态与线程组. java 多线程 基础知识 创建线程的两种方式:1 ...
- android studio 新建项目 界面一直停在 【“building ‘ 项目名’ gradle project info”】
安装了android studio 之后,按照上文所述的那篇博文下载安装gradle,配置环境变量, 启动android studio,新建项目,发现还是新建不了,界面一直停在 ["buil ...
- 第15章—数据库连接池(DBCP2)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...