南京大学-软件工程-18-代码设计教材

上传人:我** 文档编号:115370067 上传时间:2019-11-13 格式:PPT 页数:73 大小:861.50KB
返回 下载 相关 举报
南京大学-软件工程-18-代码设计教材_第1页
第1页 / 共73页
南京大学-软件工程-18-代码设计教材_第2页
第2页 / 共73页
南京大学-软件工程-18-代码设计教材_第3页
第3页 / 共73页
南京大学-软件工程-18-代码设计教材_第4页
第4页 / 共73页
南京大学-软件工程-18-代码设计教材_第5页
第5页 / 共73页
点击查看更多>>
资源描述

《南京大学-软件工程-18-代码设计教材》由会员分享,可在线阅读,更多相关《南京大学-软件工程-18-代码设计教材(73页珍藏版)》请在金锄头文库上搜索。

1、丁二玉 南京大学软件学院,软件工程与计算II Ch18 代码设计,主要内容,设计易读的代码 设计易维护的代码 设计可靠的代码 使用模型辅助设计复杂代码 为代码开发单元测试用例 代码复杂度度量,2,18.1 设计易读的代码,维护的需要 团队协作的需要,3,代码规范,格式 命名 注释 .,4,布局格式,使用缩进与对齐表达逻辑结构,5,缩进与对齐格式示例,6,How To Write Unmaintainable Code,if(a) if(b)x = y; else x = z;,7,How To Write Unmaintainable Code,11. Never use an automat

2、ed source code tidier to keep code aligned. Lobby to have them banned on the grounds they create false deltas in CVS or that every programmer should have his own indenting style held forever sacrosanct for any module he wrote. You are now free to accidentally misalign the code to give the optical il

3、lusion that bodies of loops and ifs are longer or shorter than they really are, or that else clauses match a different if than they really do.,How To Write Unmaintainable Code,13. Never put in any surrounding your if/else blocks unless they are syntactically obligatory. If you have a deeply nested m

4、ixture of if/else statements and blocks, especially with misleading indentation, you can trip up even an expert maintenance programmer.,布局格式,将相关逻辑组织在一起 类的逻辑组织 成员变量声明; 构造方法与析构方法; public 方法; protected 方法; private 方法。 使用空行分割逻辑,10,逻辑组织清晰的代码,11,布局格式,语句分行,12,How To Write Unmaintainable Code,9. Try to pack

5、 as much as possible into a single line. This saves the overhead of temporary variables, and makes source files shorter by eliminating new line characters and white space. Tip: remove all white space around operators. Good programmers can often hit the 255 character line length limit imposed by some

6、 editors. The bonus of long lines is that programmers who cannot read 6 point type must scroll to view them.,13,How To Write Unmaintainable Code,31. Nest as deeply as you can. Good coders can get up to 10 levels of ( ) on a single line and 20 in a single method. C+ coders have the additional powerfu

7、l option of preprocessor nesting totally independent of the nest structure of the underlying code. You earn extra Brownie points whenever the beginning and end of a block appear on separate pages in a printed listing. Wherever possible, convert nested ifs into nested ? : ternaries.,14,命名,使用有意义的名称进行命

8、名。例如对“销售信息”类,命名为Sales 而不是ClassA。 使用名词命名类、属性和数据; 使用名词或者形容词命名接口; 使用动词或者“动词+名词”命名函数和方法; 使用合适的命名将函数和方法定义的明显、清晰,包括返回值、参数、异常等。 名称要与实际内容相符。例如,使用Payment 命名“账单”类明显比使用“Change”更相符,因为“账单”类的职责不仅仅是计算“Change”,还要维护账单数据。 如果存在惯例,命名时要遵守惯例。例如,Java 语言的命名惯例是:使用小写词命名包;类、接口名称中每个单词的首字母大写;变量、方法名称的第一个单词小写,后续单词的首字母大写;常量的每个单词大写

9、,单词间使用“_”连接。,15,命名,临时变量命名要符合常规。像for 循环计数器、键盘输入字符等临时变量一般不要求使用有意义的名称,但是要使用符合常规的名称,例如使用i、j 命名整数而不是字符,使用c、s 命名字符而不是整数。 不要使用太长的名称,不利于拼写和记忆。 不要使用易混字符进行命名,常见的易混字符例如“I”(大写i)、“1”(数字1)与“l”(小写L)、0(数字零)与O(字母)等。使用易混字符的命名例如D0Calc 与DOCalc。 不要仅仅使用不易区分的多个名称,例如Sales与Sale,SalesLineItem与SalesLineitem。 不要使用没有任何逻辑的字母缩写进行

10、命名,例如wrttn、wtht、vwls、smch、trsr,16,How To Write Unmaintainable Code,3. Make sure that every method does a little bit more (or less) than its name suggests. As a simple example, a method named isValid(x) should as a side effect convert x to binary and store the result in a database. 4. Use acronyms to

11、 keep the code terse(简洁). Real men never define acronyms; they understand them genetically.,17,How To Write Unmaintainable Code,10. Cd wrttn wtht vwls s mch trsr. When using abbreviations inside variable or method names, break the boredom with several variants for the same word, and even spell it ou

12、t longhand once in while. This helps defeat those lazy bums who use text search to understand only some aspect of your program. Consider variant spellings as a variant on the ploy, e.g. mixing International colour, with American color and dude-speak kulerz. If you spell out names in full, there is o

13、nly one possible way to spell each name. These are too easy for the maintenance programmer to remember. Because there are so many different ways to abbreviate a word, with abbreviations, you can have several different variables that all have the same apparent purpose. As an added bonus, the maintena

14、nce programmer might not even notice they are separate variables,18,How To Write Unmaintainable Code,15. Use very long variable names or class names that differ from each other by only one character, or only in upper/lower case. An ideal variable name pair is swimmer and swimner. Exploit the failure

15、 of most fonts to clearly discriminate between ilI1| or oO08 with identifier pairs like parselnt and parseInt or D0Calc and DOCalc. l is an exceptionally fine choice for a variable name since it will, to the casual glance, masquerade as the constant 1. Create variable names that differ from each oth

16、er only in case e.g. HashTable and Hashtable.,19,How To Write Unmaintainable Code,23.To break the boredom, use a thesaurus to look up as much alternate vocabulary as possible to refer to the same action, e.g. display, show, present. Vaguely hint there is some subtle difference, where none exists. However, if there are two similar functions that have a crucial difference, always use the same word in describing both functions (e.g. print to mean write to a file, and to a pri

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号