System Software Software Engineering Department of Computer Science

  • Slides: 54
Download presentation
程序编码 董渊 ( System Software & Software Engineering) Department of Computer Science & Technology

程序编码 董渊 ( System Software & Software Engineering) Department of Computer Science & Technology Tsinghua University 2005 ~ 2008 Copyright @ Tsinghua University Page 1

程序设计风格 – 基本原则 Top-down flow • Have the required action follow soon after the

程序设计风格 – 基本原则 Top-down flow • Have the required action follow soon after the decision that generates it Generality is a virtue • Trade-off: generality, performance, understanding Dependence among components must be visible 2005 ~ 2008 Copyright @ Tsinghua University Page 4

benefit = minimum; if (age<75) goto A; benefit = maximum; goto C; if (age<65)

benefit = minimum; if (age<75) goto A; benefit = maximum; goto C; if (age<65) goto B; if (age<55) goto C; A: if (age<65) goto B; benefit = benefit * 1. 5 + bonus; goto C; B: if (age<55) goto C; benefit = benefit * 1. 5; C: next statement if (age < 55) benefit = minimum; elseif (age < 65) benefit = munimum + bonus; elseif (age < 75) benefit = munimum * 1. 5 + bonus; else benefit = maximum; 2005 ~ 2008 Copyright @ Tsinghua University Page 5

程序设计风格– 基本原则 Do not sacrifice clarity and correctness for speed • • Cost to

程序设计风格– 基本原则 Do not sacrifice clarity and correctness for speed • • Cost to write faster code Cost to test more complex code Cost to maintain more complex/less understandable code Let compiler and other tools do it Choose data structure to simplify the program’s calculation 2005 ~ 2008 Copyright @ Tsinghua University Page 6

1. 2. 3. 4. 5. For the first $10, 000 of income, the tax

1. 2. 3. 4. 5. For the first $10, 000 of income, the tax is 10%. For the next $10, 000 of income above $10, 000, the tax is 12%. For the next $10, 000 of income above $20, 000, the tax is 15%. For the next $10, 000 of income above $30, 000, the tax is 18%. For any income above $40, 000, the tax is 20%. Tax Table Bracket Base Percent 0 0 10 10, 000 12 20, 000 2200 15 30, 000 3700 18 40, 000 5500 20 2005 ~ 2008 tax = base[level] + percent[level] * (taxable_income – bracket[level]) Copyright @ Tsinghua University Page 7

程序设计风格– 基本原则 Localizing input and output • • Hardware/software dependence Easy to change Including

程序设计风格– 基本原则 Localizing input and output • • Hardware/software dependence Easy to change Including pseudocode • Adapt design to chosen language Revising and rewriting, not patching • Module decomposition, control structure, data structure, algorithm, etc. 2005 ~ 2008 Copyright @ Tsinghua University Page 8

程序设计风格– 基本原则 Documentation : written description of WHAT the programs do and HOW they

程序设计风格– 基本原则 Documentation : written description of WHAT the programs do and HOW they do it • Self-Documentation, Source code Internal documentation • External documentation • Header comment block • Program comments • Meaningful variable names and statement labels • Formatting to enhance understanding • Documenting data • Problem, algorithm, data 2005 ~ 2008 Copyright @ Tsinghua University Page 9

内容提要 基本目标 程序设计风格 Java程序设计风格 程序复杂性度量 2005 ~ 2008 Copyright @ Tsinghua University Page 10

内容提要 基本目标 程序设计风格 Java程序设计风格 程序复杂性度量 2005 ~ 2008 Copyright @ Tsinghua University Page 10

命名规则 包(Packages),体系结构 接口(Interfaces),联系(内外部) 类(Classes),基本模块 方法(Methods),服务,消息 变量(Variables),属性 常量(Constants),属性 2005 ~ 2008 Copyright @ Tsinghua University

命名规则 包(Packages),体系结构 接口(Interfaces),联系(内外部) 类(Classes),基本模块 方法(Methods),服务,消息 变量(Variables),属性 常量(Constants),属性 2005 ~ 2008 Copyright @ Tsinghua University Page 12

命名规则 包 • 前缀(顶层) • 小写ASCII字母 • ISO 3166(1981)的Internet域名规范: • • 如com, edu, org,

命名规则 包 • 前缀(顶层) • 小写ASCII字母 • ISO 3166(1981)的Internet域名规范: • • 如com, edu, org, net, gov, mil 如cn, tw, hk, jp, uk • 后继可根据项目结构: • 例如: • 所在组织的组织结构 • 或内部命名规范 • com. sun. security; • org. omg. CORBA • edu. tsinghua. course 11. group 4 • java. awt 2005 ~ 2008 Copyright @ Tsinghua University Page 13

命名规则 接口 • 名词或形容词组合中,每个词的首字母大写 • 尽量简单明确 • 尽量用全名,或用极为通用的缩写如URL,HTML • 例如: • Serializable;Adjustable; • Runnable;Cloneable

命名规则 接口 • 名词或形容词组合中,每个词的首字母大写 • 尽量简单明确 • 尽量用全名,或用极为通用的缩写如URL,HTML • 例如: • Serializable;Adjustable; • Runnable;Cloneable • Action. Listener; • Mouse. Listener • Iterator;Enumeration 2005 ~ 2008 Copyright @ Tsinghua University Page 14

命名规则 常量 • 大写字母 • 单词之间用“_”连接 • 例如: • Color. BLACK,Color. GREEN; • JOption.

命名规则 常量 • 大写字母 • 单词之间用“_”连接 • 例如: • Color. BLACK,Color. GREEN; • JOption. Pane. ERROR_MESSAGE, JOption. Pane. YES_NO_CANCEL_OPTION; 2005 ~ 2008 Copyright @ Tsinghua University Page 18

/* * 文件名 * * 版本信息 * * ��(开���、修改的�史��) * * Copyright notice */

/* * 文件名 * * 版本信息 * * ��(开���、修改的�史��) * * Copyright notice */ package homework; import java. util. Vector; import java. awt. *; 2005 ~ 2008 Copyright @ Tsinghua University Page 20

/** * �的主要功能描述 * * @version 版本名称及开��� * @author 作者名 * @modified by: *

/** * �的主要功能描述 * * @version 版本名称及开��� * @author 作者名 * @modified by: * 更改的�史��,包括作者、�� */ public class Blah extends Some. Class { /* A class implementation comment can go here. */ 2005 ~ 2008 Copyright @ Tsinghua University Page 21

/** class. Var 1 documentation comment */ public static int class. Var 1; /**

/** class. Var 1 documentation comment */ public static int class. Var 1; /** * class. Var 2 documentation comment that happens to * be more than one line long */ private static Object class. Var 2; /** instance. Var 1 documentation comment */ public Object instance. Var 1; /** instance. Var 2 documentation comment */ protected int instance. Var 2; /** instance. Var 3 documentation comment */ private Object[] instance. Var 3; 2005 ~ 2008 Copyright @ Tsinghua University Page 22

/** *. . . constructor Blah documentation comment. . . */ public Blah() {

/** *. . . constructor Blah documentation comment. . . */ public Blah() { //. . . implementation goes here. . . } 2005 ~ 2008 Copyright @ Tsinghua University Page 23

/** *. . . method do. Something documentation comment. . . */ public void

/** *. . . method do. Something documentation comment. . . */ public void do. Something() { //. . . implementation goes here. . . } /** *. . . method do. Something. Else documentation comment. . * @param some. Param description */ public void do. Something. Else(Object some. Param) { //. . . implementation goes here. . . } } 2005 ~ 2008 Copyright @ Tsinghua University Page 24

some. Method(long. Expression 1, long. Expression 2, long. Expression 3, long. Expression 4, long.

some. Method(long. Expression 1, long. Expression 2, long. Expression 3, long. Expression 4, long. Expression 5); var = some. Method 1(long. Expression 1, some. Method 2(long. Expression 2, long. Expression 3)); long. Nm 1 = long. Nm 2 * (long. Nm 3 + long. Nm 4 - long. Nm 5) + 4 * long. Nm 6; // PREFER long. Nm 1 = long. Nm 2 * (long. Nm 3 + long. Nm 4 - long. Nm 5) + 4 * long. Nm 6; // AVOID 2005 ~ 2008 Copyright @ Tsinghua University Page 26

/* * Here is a block comment. */ if (condition) { /* Handle the

/* * Here is a block comment. */ if (condition) { /* Handle the condition. */. . . } if (a == 2) { return TRUE; /* special case */ } else { return is. Prime(a); /* works only for odd a */ } 2005 ~ 2008 Copyright @ Tsinghua University Page 28

if (foo > 1) { // Do a double-flip. . } else { return

if (foo > 1) { // Do a double-flip. . } else { return false; // Explain why here. } //if (bar > 1) { // // // Do a triple-flip. //. . . //} //else { // return false; //} 2005 ~ 2008 Copyright @ Tsinghua University Page 29

程序注释 Documentation Comments • • Javadoc /**………*/ @return @param @see @since @link HTML标识 2005

程序注释 Documentation Comments • • Javadoc /**………*/ @return @param @see @since @link HTML标识 2005 ~ 2008 Copyright @ Tsinghua University Page 30

/** * Returns an Image object that can then be painted on the screen.

/** * Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute {@link URL}. * <p> * This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on * the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. * * @param url an absolute URL giving the base location of the image * @param name the location of the image, relative to the URL argument * @return the image at the specified URL * @see Image */ public Image get. Image(URL url, String name) { …… } 2005 ~ 2008 Copyright @ Tsinghua University Page 31

2005 ~ 2008 Copyright @ Tsinghua University Page 32

2005 ~ 2008 Copyright @ Tsinghua University Page 32

程序声明 每个声明一行,以便于添加注释 //preferred int level; // indentation level int size; // size of table

程序声明 每个声明一行,以便于添加注释 //preferred int level; // indentation level int size; // size of table //avoid int level,size; 类型和标识符间要有空格(或Tab)分隔 int level; // indentation level int size; // size of table Object current. Entry; // currently selected table entry 2005 ~ 2008 Copyright @ Tsinghua University Page 33

程序声明 初始化:尽量在变量声明的同时赋初值 声明的位置:尽量在程序块的开始部分 void my. Method() { int 1 = 0; // beginning of

程序声明 初始化:尽量在变量声明的同时赋初值 声明的位置:尽量在程序块的开始部分 void my. Method() { int 1 = 0; // beginning of method block if (condition) { int 2 = 0; // beginning of "if" block. . . } } 2005 ~ 2008 Copyright @ Tsinghua University Page 34

程序声明 避免局部变量与高层变量重名。 int count; . . . my. Method() { if (condition) { int

程序声明 避免局部变量与高层变量重名。 int count; . . . my. Method() { if (condition) { int count = 0; // AVOID!. . . } 2005 ~ 2008 Copyright @ Tsinghua University Page 35

class Sample extends Object { int ivar 1; int ivar 2; Sample(int i, int

class Sample extends Object { int ivar 1; int ivar 2; Sample(int i, int j) { ivar 1 = i; ivar 2 = j; } int empty. Method() {}. . . } 2005 ~ 2008 Copyright @ Tsinghua University Page 37

语句结构 简单语句 • 每一条语句单独一行 复合语句 • • 用缩进明确语句间的层次关系 if-then-else语句 for while do-while switch try-catch

语句结构 简单语句 • 每一条语句单独一行 复合语句 • • 用缩进明确语句间的层次关系 if-then-else语句 for while do-while switch try-catch 2005 ~ 2008 Copyright @ Tsinghua University Page 38

if (condition) { statements; } else { statements; } if (condition) { statements; }

if (condition) { statements; } else { statements; } if (condition) { statements; } else{ statements; } 2005 ~ 2008 Copyright @ Tsinghua University Page 39

for (initialization; condition; update) { statements; } for (initialization; condition; update); while (condition) {

for (initialization; condition; update) { statements; } for (initialization; condition; update); while (condition) { statements; } while (condition); do { statements; } while (condition); 2005 ~ 2008 Copyright @ Tsinghua University Page 40

switch (condition) { case ABC: statements; /* falls through */ case DEF: statements; break;

switch (condition) { case ABC: statements; /* falls through */ case DEF: statements; break; case XYZ: statements; break; default: statements; break; } 2005 ~ 2008 Copyright @ Tsinghua University Page 41

try { statements; } catch (Exception. Class e) { statements; } finally { statements;

try { statements; } catch (Exception. Class e) { statements; } finally { statements; } 2005 ~ 2008 Copyright @ Tsinghua University Page 42

内容提要 基本目标 程序设计风格 Java程序设计风格 程序复杂性度量 2005 ~ 2008 Copyright @ Tsinghua University Page 45

内容提要 基本目标 程序设计风格 Java程序设计风格 程序复杂性度量 2005 ~ 2008 Copyright @ Tsinghua University Page 45

A开始 A B B C�入 C D K�出 D E L�束 E F J

A开始 A B B C�入 C D K�出 D E L�束 E F J L G G H H�入 程序� 程序流程� 2005 ~ 2008 K Copyright @ Tsinghua University Page 49

1 2 V=1 -2+2 =0+1 =1 1 2 3 4 顺序型 选择型 1 1

1 2 V=1 -2+2 =0+1 =1 1 2 3 4 顺序型 选择型 1 1 2 3 V=3 -3+2 =1+1 =2 While型循环 2005 ~ 2008 2 3 V=4 -4+2 =1+1 =2 V=3 -3+2 =1+1 =2 Until型循环 Copyright @ Tsinghua University Page 51

1 2 3 4 I 5 II IV 6 7 8 9 V=11 -9+2=3+1=4

1 2 3 4 I 5 II IV 6 7 8 9 V=11 -9+2=3+1=4 2005 ~ 2008 Copyright @ Tsinghua University Page 52