惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
博客园_首页
WordPress大学
WordPress大学
博客园 - 聂微东
P
Privacy International News Feed
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
NISL@THU
NISL@THU
美团技术团队
T
Tailwind CSS Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Hacker News
The Hacker News
B
Blog
P
Palo Alto Networks Blog
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
The Register - Security
The Register - Security
S
Securelist
A
Arctic Wolf
MyScale Blog
MyScale Blog
H
Help Net Security
N
Netflix TechBlog - Medium
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Latest
Security Latest
T
Tor Project blog
V
Vulnerabilities – Threatpost
V
V2EX
AI
AI
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Heimdal Security Blog
Google Online Security Blog
Google Online Security Blog
Know Your Adversary
Know Your Adversary

博客园 - SoRoMan

Scene Management - Scene Graph 思考:矩阵的级联顺序与坐标系的关系 MFC的一些宏的整理 (DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE) 关于STL Improve Performance of C++ Codes (2) -- 如何消除临时对象? Improve Performance of C++ Codes (1) -- 使用初始化列表还是赋值语句? 关于万向节死锁(Gimbal Lock)(续) 思考:矩阵及变换,以及矩阵在DirectX和OpenGL中的运用问题:左乘/右乘,行优先/列优先,... 读书笔记:Writing Solid Code (4) 读书笔记:Writing Solid Code (3) 读书笔记:Writing Solid Code (2) A 3D Real Time Fluid Solver Demo (三维实时流体解算器) 笔记:Delaunay三角剖分(Delaunay Triangulation)相关知识 学习经验:逐步建立学习资源列表-计算机图形学 SL专题6:谈谈Second Life中的游戏制作(Game Dev in SL) SL专题4:Second Life畅销商品 SL专题3:Second Life市场分析 SL专题2:加入并熟悉Second Life世界 SL专题1:Second Life(第二生命)--迟早火爆世界的游戏与Web的集大成者
读书笔记:Writing Solid Code
SoRoMan · 2007-08-06 · via 博客园 - SoRoMan

<<Writing Solid Code>> - by Steve Maguire, 1993.
编写可靠的/健壮的/没有bug的代码

【注】虽然书中很多例子是基于C语言的,但是编程的思想是相通的,而且书中提出的解决问题的方法及原理是语言无关的。这本书是由多条指导方针组成的,正如作者所说,这些指导方针是多年长期思考及实践的结果,指导方针看似简单,却是经验的总结。除次之外,书中对于解决问题的思考过程显得尤为珍贵。

以下摘取每章的概述以及每条指导方针,附翻译及简单注释。
------------
Introduction
------------

Summary:
With the growing complexity of software and the associated climb in bug rates, it's becoming increasingly necessary for programmers to produce
bug-free code much earlier in the development cycle, before the code is first sent to Testing. The key to writing bug-free code is to become more
aware of how bugs come about. Programmers can cultivate this awareness by asking themselves two simple questions about every bug they encounter:
"How could I have prevented this bug?" and "How could I have automatically detected this bug?" The guidelines in this book are the results of regularly asking these two questions over a number of years.

概述:
随着软件复杂性的增长以及相应的bug发生率的爬升,对于程序员来说,在开发周期中代码拿去测试之前,更早地编写bug-free代码变得更加必要。编写bug-free代码的关键是意识到bugs是怎样出现的。程序员能够通过问他们自己关于遇到的每个bug这样两个问题来培养这个技能。

1.我怎样才能阻止这个bug发生?
2.我怎样才能自动察觉到这个bug?

这本书中的指导方针就是多年规律性地问这两个问题的结果。

--------------------
Chapter 1 A Hypothetical Compiler
一个假定的编译器
--------------------
Summary:
If your compiler could detect every bug in your program-no matter the type-and issue an error message, ridding your code of bugs would be
simple. Such omniscient compilers don't exist, but by enabling optional compiler warnings, using syntax and portability checkers, and using automated
unit tests, you can increase the number of bugs that are detected for you automatically.

概述:
如果你的编译器能够察觉到程序中每个bug ,无论是什么类型,并且还发出一个错误消息,那么让你的代码规避bugs就是简单的事情。这样无所不知的编译器是不存在的,但是通过开启可选的编译器警告选项,使用语法以及可移植性检查器,以及使用单元测试,被你自动察觉到的bugs数目就会增加。

Guidelines:
1.1.Enable all optional compiler warnings.
开启所有可选的编译器警告选项。

1.2.Use lint to catch bugs that your compiler may miss.
使用lint来抓住那些编译器可能遗漏的bugs。

【注】lint是一种代码质量保证工具,能够进行更强的编译检查。如:pc-lint。在有些公司,在check in代码到main branch之前,一般需要通过lint检查,当然最好也开启lint的所有警告开关,久而久之,不需要工具辅助,也能写出健壮的代码。

1.3.If you have unit tests, use them.
如果有单元测试,使用他们。

【注】程序员所做的单元测试侧重于验证代码内部结构和处理过程,大部分利用白盒测试手段,比如设计一些用例测试其内部的逻辑,象控制点(如:条件判断点,循环点,选择分支点等),比如检查内部数据的有效性,甚至检查代码的冗余等。仅仅依赖于黑盒测试是不够的。

--------------------------
Chapter  2 Assert Yourself
断言你自己
---------------------------
Summary:
A good development strategy is to maintain two versions of your program: one that you ship and one that you use to debug the code. By using debugging
assertion statements, you can detect bugs caused by bad function arguments, accidental use of undefined behavior, mistaken assumptions made by other programmers, and impossible conditions that nevertheless somehow show up. Debug-only backup algorithms help verify function results and the algorithms used in functions.

概述:
一个好的开发策略是维护两套版本的程序,一个用来发布一个用来调试。通过使用调试断言语句,你可以察觉到由非法的函数参数,未定义行为的意外使用,其他程序员的错误的假设以及不可能发生的情况仍然不知何故出现了所造成的bugs。只用于调试的备份算法能帮助验证函数的结果以及函数中使用的算法。

Guidelines:
2.1.Maintain both ship and debug versions of your program.
维护发布和调试两套版本的程序

2.2.Use assertions to validate function arguments
使用断言来验证函数参数

【注】常见的使用Assert的一个地方。在入口处检查参数,及早发现问题,使用Assert的好处是在调试版本下能立刻定位到上下文。

2.3.Strip undefined behavior from your code, or use assertions to catch illegal uses of undefined behavior.
去除代码中的未定义的行为,或者使用断言来抓住未定义行为的非法使用。

【注】使用Assert的又一个地方。作者举了个c实现内存拷贝的例子,当在两块重叠的内存之间进行拷贝的时候的行为是未定义的。所以加上个Assert来断言传入的两个指针所指内存之间足够大。

2.4.Don't waste people's time. Document unclear assertions.
不要浪费人们的时间。为不清楚的断言加上说明

2.5.Either remove implicit assumptions, or assert that they are valid.
要么消除隐式的假设,要么断言它们是合法的。

【注】使用Assert的又一个地方。作者举了个c实现内存设值的例子,有时为了加快memset,会将byte*指针转换成long*的。但是byte*的指针可能是个奇地址,如果转换成long*的,在有些系统上会出问题,因为long*的指针在这些系统上不能以奇地址开始。

2.6.Use assertions to detect impossible conditions.
使用断言来察觉不可能的情况。

2.7.Don't hide bugs when you program defensively.
在进行防御式编程时,不要隐藏bugs。
【注】防御式编程是一种编程风格,主要目的是保护系统不受“非法”输入的破坏,但这样很容易掩盖bugs。

2.8.Use a second algorithm to validate your results.
使用第二套算法来验证你的结果。

2.9.Don't wait for bugs to happen; use startup checks.
不要等着bugs发生,使用启动检查。

to be continued...