java命名规范_编程惯例和代码范例

来源:互联网 发布:vwap 算法交易 编辑:程序博客网 时间:2024/05/18 23:57

1.       编程惯例(Programming Practices)

9.1  提供对实例以及类变量的访问控制(Providing Access to Instance and Class Variables)

若没有足够理由,不要把实例或类变量声明为公有。通常,实例变量无需显式的设置(set)和获取(gotten),通常这作为方法调用的边缘效应 (side effect)而产生。

一个具有公有实例变量的恰当例子,是类仅作为数据结构,没有行为。亦即,若你要使用一个结构(struct)而非一个类(如果java支持结构的话),那么把类的实例变量声明为公有是合适的。

9.2  引用类变量和类方法(Referring to Class Variables and Methods)

避免用一个对象访问一个类的静态变量和方法。应该用类名替代。例如:

classMethod(); //OK

AClass.classMethod(); //OK

anObject.classMethod(); //AVOID!

9.3  常量(Constants)

位于for循环中作为计数器值的数字常量,除了-1,01之外,不应被直接写入代码。

9.4  变量赋值(Variable Assignments)

避免在一个语句中给多个变量赋相同的值。它很难读懂。例如:

fooBar.fChar = barFoo.lchar = 'c'; // AVOID!

不要将赋值运算符用在容易与相等关系运算符混淆的地方。例如:

if (c++ = d++) { // AVOID! (Java disallows)

...

}

应该写成

if ((c++ = d++) != 0) {

...

}

不要使用内嵌(embedded)赋值运算符试图提高运行时的效率,这是编译器的工作。例如

d = (a = b + c) + r; // AVOID!

应该写成

a = b + c;

d = a + r;

9.5  其它惯例(Miscellaneous Practices)

9.5.1        圆括号(Parentheses)

一般而言在含有多种运算符的表达式中使用圆括号来避免运算符优先级问题是个好方法。即使运算符的优先级对你而言可能很清楚,但对其他人未必如此。你不能假设别的程序员和你一样清楚运算符的优先级。

if (a == b && c == d) // AVOID!

if ((a == b) && (c == d)) // RIGHT

9.5.2        返回值(Returning Values)

设法让你的程序结构符合目的。例如:

if (booleanExpression) {

return true;

} else {

return false;

}

应该代之以如下方法:

return booleanExpression;

类似地:

if (condition) {

return x;

}

return y;

应该写做:

return (condition ? x : y);

9.5.3        条件运算符"?"前的表达式

如果一个包含二元运算符的表达式出现在三元运算符" ? : ""?"之前,那么应该给表达式添上一对圆括号。例如:

(x >= 0) ? x : -x;

9.5.4        特殊注释(Special Comments)

在注释中使用XXX来标识某些未实现(bogus)的但可以工作(works)的内容。例如://TODO

FIXME来标识某些假的和错误的内容。

 

2.       代码范例(Code Examples)

10.1           Java源文件范例(Java Source File Example)

下面的例子,展示了如何合理布局一个包含单一公共类的Java源程序。接口的布局与其相似。更多信息参见"类和接口声明"以及"文挡注释"

/*

* @(#)Blah.java 1.82 99/03/18

*

* Copyright (c) 1994-1999 Sun Microsystems, Inc.

* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

* All rights reserved.

*

* This software is the confidential and proprietary information of Sun

* Microsystems, Inc. ("Confidential Information"). You shall not

* disclose such Confidential Information and shall use it only in

* accordance with the terms of the license agreement you entered into

* with Sun.

*/

package java.blah;

import java.blah.blahdy.BlahBlah;

/**

* Class description goes here.

*

* @version 1.82 18 Mar 1999

* @author Firstname Lastname

*/

public class Blah extends SomeClass {

/* A class implementation comment can go here. */

/** classVar1 documentation comment */

public static int classVar1;

/**

* classVar2 documentation comment that happens to be

* more than one line long

*/

private static Object classVar2;

/** instanceVar1 documentation comment */

public Object instanceVar1;

/** instanceVar2 documentation comment */

protected int instanceVar2;

/** instanceVar3 documentation comment */

private Object[] instanceVar3;

/**

* ...constructor Blah documentation comment...

*/

public Blah() {

// ...implementation goes here...

}

/**

* ...method doSomething documentation comment...

*/

public void doSomething() {

// ...implementation goes here...

}

/**

* ...method doSomethingElse documentation comment...

* @param someParam description

*/

public void doSomethingElse(Object someParam) {

// ...implementation goes here...

}

public String toString(){

//…….

}

}

原创粉丝点击