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

推荐订阅源

The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
S
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Recorded Future
Recorded Future
I
Intezer
云风的 BLOG
云风的 BLOG
博客园 - Franky
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
The Hacker News
The Hacker News
T
The Blog of Author Tim Ferriss
Attack and Defense Labs
Attack and Defense Labs
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
Webroot Blog
Webroot Blog
W
WeLiveSecurity
H
Heimdal Security Blog
博客园 - 三生石上(FineUI控件)
V
Vulnerabilities – Threatpost
G
Google Developers Blog
O
OpenAI News
V
V2EX
罗磊的独立博客
博客园_首页
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
博客园 - 叶小钗
T
Tor Project blog
AI
AI

博客园 - 蛙蛙王子

蛙蛙推荐: TensorFlow Hello World 之平面拟合 [蛙蛙推荐]SICP第一章学习笔记-编程入门 快速学习C语言四: 造轮子,ArrayList 快速学习C语言三: 开发环境, VIM配置, TCP基础,Linux开发基础,Socket开发基础 快速学习C语言二: 编译自动化, 静态分析, 单元测试,coredump调试,性能剖析 快速学习C语言一: Hello World 转:python获取linux系统及性能信息 转帖:Python应用性能分析指南 蛙蛙推荐:快速自定义Boostrap样式 蛙蛙推荐:WEB安全入门 蛙蛙推荐:五分钟搞定网站前端性能优化 蛙蛙推荐:AngularJS学习笔记 蛙蛙推荐:如何实时监控MySql状态 蛙蛙推荐:Backbone和seajs搭配最佳实践探讨 蛙蛙推荐:让SecureCRT好使起来 蛙蛙推荐:一个程序员2012年技术学习总结 时髦的互联网公司都在用什么技术? 蛙蛙推荐:如何编写高质量的python程序 蛙蛙推荐:第一堂编程课提纲
蛙蛙推荐:笨办法提高代码质量
蛙蛙王子 · 2013-01-29 · via 博客园 - 蛙蛙王子

软件中的大多Bug都与各种低级错误有关,而大多低级错误是靠测试测不出来的,更多的需要Code Review来发现问题。

培养好的习惯,不靠任何假设编程,可以先从写好每个函数开始,我想用注释驱动来提醒自己经常Review每个函数。

以下是我拟定的Code Review Comments:

/* ### Code Review
 * ### Reviewer: wawa
 * ### Last Review Date: 2013-01-29
 *
 * - todo: input review
 * - todo: output review 
 * - todo: exception review 
 * - todo: null reference review
 * - todo: out of bounds review 
 *
 * */

每个函数都加上这么一段注释,某项review通过后把todo改成done,如果代码修改后要重新review,并修改Last Review Date。

这么玩肯定会很麻烦,但我的理念就是代码写的越慢越好,通过Check List来强制对代码进行某些思考,从而慢慢养成习惯。

比如下面这段用户登录的代码。

function login(username, password){
    var account = accountDAO.getAccount();
    if (account['password'] == password){
        return {code:200, message:'login success.'} 
    }else{
        return {code:403, message:'login faild.'} 
    }
}

很明显有很多问题,然后经过review,修改如下

/* ### Code Review
 * ### Reviewer: wawa
 * ### Last Review Date: 2013-01-29
 *
 * - done: input review
 * - done: output review 
 * - done: exception review 
 * - done: null reference review
 * - done: out of bounds review 
 *
 * */
function login(username, password){
    if (username === null || password === null 
        || username === '' || password === ''){
        return {code:400, message: 'input invalid'};
    }

    try{
        var account = accountDAO.getAccount();
        if (account === null){
            return {code:404, message: 'account not found'};
        }
    }
    catch(err){
        return {code:500, message: 'server error'};
    }

    if (!('password' in account)){
        return {code:500, message: 'server error'};
    }
    if (account['password'] == password){
        return {code:200, message:'login success.'} 
    }else{
        return {code:401, message:'login faild.'} 
    }
}

第一步就先做这几个基本的检查吧。

  1. input review: 输入检查,主要是参数有效性检查,安全检查
  2. output review:输出检查,对返回值进行契约检查,比如一个计算工资的函数,不能返回负数;
  3. exception review: 异常检查,分两方面
    1. 一是有可能你调用的某个方法会抛出异常,你是要捕捉,还是继续往上抛;
    2. 二是你这个函数有可能会抛出什么异常,都要进行思考,做到心中有数;
  4. null reference review: 空引用检查,你得到的某个变量,是否可能为空,空引用是最常见的低级错误;
  5. out of bounds review: 索引越界检查,对数组和字典的使用,都有可能造成索引越界;