原题:

4、When programmers add new elements to an enumeration, they sometimes forget to add new cases to the appropriate switch statements. How could you use assertions to help detect this problem?

附录中的答案:

.
.
.
default:
ASSERT(FALSE); /*We should never get here*/
break;

把这个问题扩展下:

看到上题的时候,想到了在u-boot中加入启动命令函数的时候曾经犯过错(还有一点,boot中的启动命令函数有点分散了,应该集中下),Z先生推荐我看下《write solid code》,无意中看了android中的源码,就有了如下的记录(非常好,以前没用过):

 //keywords.h
#ifndef KEYWORD int do_chroot(int nargs, char** args);
int do_chdir(int nargs, char** args); #define __MAKE_KEYWORD_ENUM__
/*
*定义KEYWORD宏,此处用了宏的“粘贴”预处理,此时只用到了第一个参数
*/
#define KEYWORD(symbol, flags, nargs, func) K_##symbol, enum {
K_NUKNOWN,
#endif
KEYWORD(capability, OPTION, , NULL)
KEYWORD(chdir, COMMAND, , do_chdir)
KEYWORD(chroot, COMMAND, , do_chroot) #ifdef __MAKE_KEYWORD_ENUM__
KEYWORD_COUNT,
}; #undef __MAKE_KEYWORD_ENUM__
#undef KEYWORD
#endif
 //parser.c
/*
*第一次包含keywords.h,根据keywords.h的代码,首先得到一个枚举定义.
*/
#include "keywords.h" /*
*重新定义KEYWORD宏,此处运用了宏的“#”预处理运算符
*/
#define KEYWORD(symbol, flags, nargs, func) \
[ K_##symbol ] = { #symbol, func, nargs + , flags, }, /*
*定义一个结构体keyword_info数组,它用来描述关键字的一些属性
*/
struct
{
const char *name; //关键字的名称
int (*func)(int nargs, char** args); //对应关键字的处理函数
unsigned char nargs; //参数个数,每个关键字的参数个数是固定的
unsigned char flags; //关键字的属性
}keyword_info[ KEYWORD_COUNT ] = {
[ K_UNKNOWN ] = { "unknown", , , ,},
/*
*第二次包含keywords.h,由于已经重新定义了KEYWORD宏,
*所以以前作为枚举值的关键字现在变成keyword_info数组
*的索引了.
*/
#include "keywords.h"
}; #undef KEYWORD

在parser.c中两次包含了keywords.h文件,首次包含时列出了枚举项,可以作为后文中数组的索引;第二次包含时,由于宏已被定义,因此正好可以实现数组中的各个项。

仿照此种实现,完全可以实现习题4的解答(一个枚举项对应一个相应的处理函数即可,而且可以通过索引来完成,比switch-case快);此外可以实现代码的集中管理,否则太分散了(不然就用grep,有点麻烦)。

write solid code Chapter 2 练习题4 的解答与扩展的更多相关文章

  1. write solid code 零散(原文)

    整理下目录,看了这个文件,幸好未删除. 以下是<write solid code>中的原文摘录. 1.How could I have prevented this bug? 2.How ...

  2. Code Complete 读后总结和新的扩展阅读计划

    Code Complete 读后总结和新的扩展阅读计划 用了一年时间终于将代码大全读完了,在这里做一个简单的总结,并安排下一阶段的扩展阅读计划. 1.选择代码大全作为我程序员职业入门的第一本书,我认为 ...

  3. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...

  4. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  5. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  6. Clean Code – Chapter 5 Formatting

    The Purpose of Formatting Code formatting is about communication, and communication is the professio ...

  7. Clean Code – Chapter 4: Comments

    “Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...

  8. Clean Code – Chapter 3: Functions

    Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...

  9. 关于write solid code中的memset

    文中说明memset可以通过操作整形以加速程序执行速度,这一点值得肯定,问题在于unicore或arm中协处理器有地址访问对齐检查,如果我们如此操作,编译器最终使用str指令来完成,那么当地址未对齐时 ...

随机推荐

  1. modelsim与debussy联调环境的搭建

    为了方便查看波形,找来了一款软件——debussy,它的一个优点是任你查看设计内信号,只需一个波形文件,如FSDB文件.而不用像modelsim那样想看某些信号,添加了之后还要重新编译仿真,浪费了很多 ...

  2. SQL查询刚開始学习的人指南读书笔记(二)创建SQL查询

    PARTII: SQL Basics CHAPTER 4Creating a Simple Query 介绍一种怎样创建SQL语句的技术--"Request/Translation/Clea ...

  3. JVM调优总结 + jstat 分析

    jstat -gccause pid 1 每格1毫秒输出结果jstat -gccause pid 2000 每格2秒输出结果不断的在屏幕打印出结果   S0     S1     E      O   ...

  4. 每日英语:Is Bo Xilai the Past or Future?

    Bo Xilai may be in jail, but a struggle is now underway within the Communist Party over the policies ...

  5. ny716 River Crossing

    River Crossing 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Afandi is herding N sheep across the expanses ...

  6. Oracle PLSQL Demo - 19.管道function[查询整表组成list管道返回]

    create or replace function function_demo RETURN emp PIPELINED as Type ref_cur_emp IS REF CURSOR RETU ...

  7. Map Class Example

    Here's a quick look at how to use the Scala Map class, with a colllection of Map class examples. The ...

  8. iOS文件和目录操作,iOS文件操作,NSFileManager使用文件操作:

    NSFileManager常用的文件方法: -(NSData*)contentsAtPath:path 从一个文件中读取数据 -(BOLL)createFileAtPath:path contents ...

  9. C++控制台读取和输出函数

    c中puts()函数用来向标准输出设备(屏幕)写字符串并换行,其调用方式为,puts(s);其中s为字符串字符(字符串数组名或字符串指针). 功 能: 送一字符串到流stdout中 用 法: int ...

  10. linux中vim中文显示乱码

    这里所说的都是全局设定,打开vimrc文件后,只需要在文件最后添加以下代码就可以了: set fileencodings=utf-8,gb2312,gbk,gb18030 set termencodi ...