logo资料库

rulechecker_java_编码规范.docx

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
asscal (don’t use Assignment inside Function Calls) 1) 描述:不允许在函数调用中使用赋值运算符(=, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=,++,--)。 用意:去除有关赋值顺序的不明确性。 配置:无。 例子: 正确写法 n=f(b); b++; 错误写法 n=f(b++); 2) asscon (don’t use Assignment inside conditions) 描述:不允许在 if , while , for , switch 控制指令中的条件表达式中使用赋值运算符(=, +=, -=, *=, /=, %=, >>=, <<=, &=, |=, ^=,++,--)。 用意:这个语句 if(x=y) 是不清楚不明确的,也许有人认为作者想写成 if (x= =y)。 配置:无 例子: 正确写法 x -= dx; if (x) { ... for (i=j=n; i > 0; i--, j--){ ... 错误写法 if (x -= dx) { ... for (i=j=n; i-- > 0; j--) {.. 3) assexp ( don’t nest assignments inside expressions) 描述:在表达式内部  一个运算数只能被赋值一次。  如果有多个赋值运算符,一个被赋值的运算数能且只能出现在它被赋值的位置。 用意:去除有关赋值顺序的不明确性 配置:无 例子: 正确写法 错误写法 i = t[i++]; a=b=c+a; i=t[i]=15; 4) blockdecl (place Declarations at the beginning of blocks) 描述:声明必须在语句体的开始处。 用意:代码易读性。 配置:无 例子: 正确写法 错误写法 5) brkcont (Break and Continue forbidden, except in switch statements) 描述:break 和 continue 指令禁止使用在控制语句(for, do ,while)的条件表达式中。然
而,break 指令被允许使用在 switch 语句的结构体中。 用意:像 goto 一样,这些指令会改变代码的结构。在循环中禁止使用它们将使代码比较 容易理解。 配置: “in switch”选项允许在 switch 里面使用 break. 其他地方都禁止 “without label”选项允许所有不带标签的 break 和 continue。 “with_label”表示所有带标签的 break 和 continue 都是允许的。 例子: 正确写法 错误写法 6) condop (the ternary operator (? : ) should not be used) 描述:三重条件操作符? ... : ...禁止使用。 用意:代码易读性。 配置:无 例子: 正确写法 错误写法 7) const (literal constants should not be used) 描述:数字和字符串必须被作为常量声明,而不是被直接作为文字形式在程序中使用。 可以指定允许使用的文字常量。默认情况下允许直接使用"", " ", "0" and "1"。 用意:代码易维护性。 配置:一个字符串列表用来表示允许直接使用的常量。 例子: 正确写法 #define TAB_SIZE 100 enum i_val { ok =7; ko =11}; const char HelloWorld[] = "Hello World.\n"; char tab[TAB_SIZE]; i_val i; ... if (i == ok) { p = HelloWorld;} 错误写法 char tab[100]; int i; ... if (i == 7) { p = "Hello World.\n";} 8) constrdef (Each class must contain an explicit default constructor) 描述:每个类都必须明确包含它的默认构造函数。 用意:确保作者已经考虑到初始化该类的方法。 配置: 无 例子:
错误写法 正确写法 class aClass { ... aClass(); ... }; 9) ctrlblock (Block statements should always be used in control structures) 描述: 在控制语句段(if , for , while , do)中必须使用{} 用意:消除结构范围的不清楚性以及使代码易读易改。 配置: 无 例子: 正确写法 if (x == 0) { return; } else { while (x > min) { x--; } } 错误写法 if (x == 0) return; else while (x > min) x--; 10) declord:成员的声明顺序 描述: 在一个类中,声明必须按照特定顺序执行(根据规则中的参数) 用意:使代码更加易读。 配置:可以有各种灵活的搭配,设定任意顺序。 { 错误写法 class aClass int f() {} public F() {} } 正确写法 class aClass { public aClass(){} public int f() {} int i; } class aClass { static public void p() {} public int f(int j) {} int f() {} static final int ID = 123; class subClass { } } 11) dmaccess (access to a class’ data members should be restricted) 描述: 类的接口必须是纯粹的函数:数据成员的定义被限制。 用意:访问一个对象状态的好的方法是通过它的方法,而不是对象的数据成员。类的数
据成员应当是 private 或者至少是 protected。 配置: 一个字符串的列表对应于禁止访问的数据成员。 例子: 正确写法 错误写法 12) exprcplx (syntactic tree complexity must be limited) 描述: 表达式的复杂度必须小于一个给定的值。复杂度的计算与语法树以及它的节点 个数有关。默认情况下,最大的审核值复杂级为 13。 用意:代码易读性。 配置: 一个数用来表示最大的审核值复杂级 例子: (b+c*d) + (b*f(c)*d) 有 8 个操作符和 7 个操作数.,语法树有 16 个节点,复杂度为 16。 13) exprparenth(Parenthesses should be used to indicate evaluation order) 描述: 在表达式中,每个二元的和三元的操作符必须放在()中。 如果使用参数 partpar,则: 当右边的操作符与当前操作符同是+或者*则,禁止为右边的操作数使用(); 同上,赋值操作符右边的操作数禁止使用(); 禁止在表达式的第一级使用(); 用意:减少运算顺序的不明确性。 配置:参数 partpar 例子: // do not write result = fact / 100 + rem; // write result = ((fact / 100) + rem); // or write, with the partpar option result = (fact / 100) + rem; // with the partpar option, write result = (fact * ind * 100) + rem + 10 + (coeff ** c); // instead of result = ((fact * (ind * 100)) + (rem + (10 + (coeff ** c)))); 14) Headercom(Modules must be preceded by a structured comment) 描述: 类或函数必须有一个头注释。头注释的格式依赖于在 meitric 类型中定义的模块类型。 根据 item 的类型来确定注释的格式: module, interface, class, method, attribute. 注释的顺序和之前定义类的声明顺序应该是一致的。
参数:Five or six lists of character strings concerning the five cases listed above. Each list begins with one of the five strings (func_glob_def for instance), followed by a string representing the regular expression. 默认情况下,头注释如下: /////////////////////////////////////////// ///// // Name: program // Author: Andrieu // Date: 08/07/96 // Remarks: example of comments /////////////////////////////////////////// ///// 参数设定内容: STANDARD headercom ON LIST "module" LIST "class" LIST "interface" LIST "attribute" LIST "method" END STANDARD "/\*" END LIST "/\*\*" "@author" "@version" END LIST "/\*\*" "@author" "@version" END LIST "/\*" END LIST "/\*" END LIST 15) identfmt(Identifiers must have a format corresponding to the type of the identifier) Definition: ----------- The identifier of a function, type or variable declared in a module must have a format corresponding to the category of the declaration. In the case of the Ada rule, checking is limited to the objects exported by the compilation unit. It is possible to define a format for specific categories in each language. For a complete description of the available categories, please refer to each manual. Parameters: ---------- A list of couples of character strings; the first string of the couple represents the declaration category name, the second one the regular expression
associated to that declaration category (see Headercom: Module header comments). 参数设定内容: STANDARD identfmt ON LIST "any" ".*" "package" "interface" "class" "constant" "var" "var-local" "[a-z]*" "[A-Z][A-Za-z0-9]*" "[A-Z][A-Za-z0-9]*" "[A-Z][A-Z0-9_]*" "[a-z][A-Za-z0-9]*" "[a-z][a-z0-9]*" END LIST END STANDARD 16) identl (the length of an identifier must be between a minimum and maximum value) 描述: 函数、类型、变量等的命名方法。见相关文档。 Definition: ----------- The length of a function, type or variable identifier has to be between a minimum and a maximum value. Parameters: ---------- A list of couples of character strings; the first string of the couple represents the declaration category name (refer to the table of the identfmt standard), the second one the MINMAX expression associated. 17) identres (some identifiers may be prohibited) 描述: 一些标识符被禁止用于声明。默认情况下,没有限制。 用意:提高代码的可移植性。 配置: 被禁止用于声明的标识符。 18) mclcass(每个文件只声明一个类) 描述:嵌套类可以除外。 19) mname(Filename must be built from the name of the class declared or defined in the file ) 描述:文件名必须来自于在文件中声明或者定义的类的名字。文件类型的字符例外 (如:.h)。 用意:程序的易理解性。 配置:文件名长度的最大最小值 20) package(保留包) 描述:禁止使用某些包的名字。
参数:保留包的名字 21) parse(Logiscope parsing error ) 描述:定义一些不能被解析的模块。 22) proxdecl(变量在靠近使用处声明) 描述:尽可能近地靠近使用处声明,If a variable is used in a loop (do, while, for) or a multiple alternatives statement (switch)it can be declared in the enclosing block.Local variables that are declared but not used is a violation of the rule. 用意:增加易读性和可靠性。 23) sgdecl(A Single Variable per Declaration ) 描述:变量的声明必须是如下的格式: type variable_name;。 禁止在一个声明中同时声明多个变量。 用意:代码易读性。 例子: 正确写法 int width; int length; 错误写法 int width, length; 24) slstat(One Statement per Line ) 描述:每一行不能多于一个语句。一个语句跟随一个{或者一个{跟随一个语句在一行之 内都是允许的,但是如果一行之内语句跟随{再跟随语句就是不允许的。 用意:。代码易读性。 例子: 正确写法 x = x0; y = y0; while (IsOk(x)) { x++; } 错误写法 x = x0; y = y0; while (IsOk(x)) {x++;} while (IsOk(x)) {x++; } 25) swdef(a default case is required within a switch construct ) 描述:在 switch 语句体中,为了覆盖没有 case 的其它情况强制使用 default。 用意:。在 switch 中,可以考虑到所有的情况。 配置:last 参数,如果使用,代表 default 必须排在所有的 case 之后。
26) swend(End of Cases in a "switch" ) 描述:在 switch 语句体中,每一个 case 都必须使用 break,continue,goto,return,或 者 exit 结束。 用意:代码易读性同时减少出错的概率。 配置:nolast,表示,最后一个 case 可以不以 break,continue,goto,return,或者 exit 结束 27) varinit(Variables Must Be Initialized Before Being Used ) 描述:所有的变量在使用之前必须初始化。 用意:并非所有的编译器都给予相同的默认值。 局限: 如果是数组、结构、类,只要它们中的一部分初始化,即视为初始化 如果有条件的初始化,认为是初始化违例: 正确写法 int a[2]; int b[2] = {6, 7}; int h; a[0] = b[0]; // no violation h = a[1]; // no violation struct { int i; int j; } e, f; e.i = 0; g = e; // no violation 错误写法 int i, j, k; j = func(); if (j) i = 0; k = i; // violation int i, j, k; j = func(); if (j) i = 0; else i = 5; k = i; // violation for (int i=0; i
分享到:
收藏