代码大全学习-33-布局和风格(Layout and Style)

说到布局和风格,首先要阐明的一点就是代码是写给人看的。自然,代码写出来是要给计算机去运行,但在编译器如此发达的现在,这个不是问题。问题是代码还要给人看,被人维护和修改的。一份代码被读的时间要远远超过它被写出来的时间。所以,可读性可维护性是写代码时必须要注意的,布局和风格就是讲怎样布局,采用什么样的风格使得代码更易读,更易理解。

用好的布局和风格写出来的代码视觉效果很好,可以很好的展示程序的逻辑结构。若两者冲突,优先选择展示程序的逻辑结构。其判断标准包括准确性,一致性,可读性和可维护性。所有的布局几乎都用空白来实现,包括空格,Tab,空行等。方式有很多,篇幅原因,这里不一一列举了,可以参考下面的checklist去检查是不是都注意到了。值得一提的是有些布局很好看,很清晰,但是可维护性不好,修改起来很费劲,这种布局也不太提倡,不好修改的最后结果就是不会被修改,于是乎布局会越来越乱。

选择什么样的布局和风格有时候像是一种信仰,这个不要太坚持了,有时候可能好几种方式都很好,可是你一套,我一套,和起来就不好了,还是要在一定范围内统一才好。

Checklist: Layout And Style

General

  • Is formatting done primarily to illuminate the logical structure of the code?
  • Can the formatting scheme be used consistently?
  • Does the formatting scheme result in code that's easy to maintain?
  • Does the formatting scheme improve code readability?

Control Structures

  • Does the code avoid doubly indented begin-end or {} pairs?
  • Are sequential blocks separated from each other with blank lines?
  • Are complicated expressions formatted for readability?
  • Are single-statement blocks formatted consistently?
  • Are case statements formatted in a way that's consistent with the formatting of other control structures?
  • Have gotos been formatted in a way that makes their use obvious?

Individual Statements

  • Is white space used to make logical expressions, array references, and rou-tine arguments readable?
  • Do incomplete statements end the line in a way that's obviously incorrect?
  • Are continuation lines indented the standard indentation amount?
  • Does each line contain at most one statement?
  • Is each statement written without side effects?
  • Is there at most one data declaration per line?

Comments

  • Are the comments indented the same number of spaces as the code they comment?
  • Is the commenting style easy to maintain?

Routines

  • Are the arguments to each routine formatted so that each argument is easy to read, modify, and comment?
  • Are blank lines used to separate parts of a routine?

Classes, Files and Programs

  • Is there a one-to-one relationship between classes and files for most classes and files?
  • If a file does contain multiple classes, are all the routines in each class grouped together and is the class clearly identified?
  • Are routines within a file clearly separated with blank lines?
  • In lieu of a stronger organizing principle, are all routines in alphabetical se-quence?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值