code complete 2 note
Loop
1 kinds of loop
2 when to use a loop with exit. 主要用于解决半循环的场合。
3 controlling the loop.
未初始化循环变量,不正常的退出,不正确的嵌套,忽略累加变量,下标串话。
1 将循环体封装成子程序
2 简化退出条件
4 entering the loop
1 one entery.
2 循环初始化代码靠近循环。
5 porcessing the Middle of the Loop
1 { } 必要
2 避免空循环
3 housekeeping 放在开始或结束
4 一个循环只做一件事
6 exciting the loop
1 不要乱改for循环下标
2 不要将循环下标用于其它操作
exiting loops early 提前退出循环
考虑在while中使用break,continue而不用布尔标记 简化逻辑
7 using Loop variables
把循环变量的作用域限制在本循环内,不要依赖编译器
8 创建循环 creating loops -from the inside out
1 用注释把需要做的事写下来
2 把注释转变为代码
3 加循环
要点:从具体事件入手,在同一时间只考虑一件事,从简单的部分开始创建。