您的位置:首页 > 编程语言 > Java开发

Java编码规范

2014-09-12 10:01 260 查看
一、异常
1.不要忽视异常处理

如果像下面的
4000
代码这样,对catch后的异常作空处理,就像埋下地雷一样让人感觉到毛骨悚然:

错误的做法:

void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
}
}

正确的做法(1):

在方法声明时抛出异常,由客户程序员去负责消化这个异常。

void <span style="background-color: rgb(255, 255, 255);">setServerPort</span>(String value) throws NumberFormatException {
serverPort = Integer.parseInt(value);
}

 正确的做法(2):

<span style="font-size:14px;color:#000000;">/** Set port. If value is not a valid number, 80 is substituted. */
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
serverPort = 80;  // default port for server
}</span>

 正确的做法(3):

/** Set port. If value is not a valid number, die. */
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new RuntimeException("port " + value " is invalid, ", e);
}

 正确的做法(4):

void setServerPort(String value) throws ConfigurationException {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new ConfigurationException("Port " + value + " is not valid.");
}
}


 2、不要偷懒而捕捉一般异常

下面代码一概捕捉Exception异常,大小通吃是不对的,这样会让你在错误出现时难以定位到错误原因,一般异常无法用统一方法进行异常处理。

错误的做法:

try {
someComplicatedIOFunction();        // may throw IOException
someComplicatedParsingFunction();   // may throw ParsingException
someComplicatedSecurityFunction();  // may throw SecurityException
// phew, made it all the way
} catch (Exception e) {               // I'll just catch all exceptions
handleError();                      // with one generic handler!
}

二、注释/JavaDoc

1.顶部版权声明

2.包和引入块(每一块以空白行分隔)

3.类或接口的声明。 在Javadoc注释,描述的类或接口的用途。

/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.foo;
import android.os.Blah;
import android.view.Yada;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Does X and Y and provides an abstraction for Z.
*/
public class Foo {
...
}

类/接口注释的内容 (1项 要求写上)
类、接口的文档注释包含如下信息:
1.用途。 开发人员使用某个类/接口之前,需要知道采用该类/接口的用途。

2.如何使用。开发人员需要知道该类/接口应该如何使用,如果必要的话还需要注明不应

该如何使用。

3.开发维护的日志。一个有关于该类/接口的维护记录:时间、作者、摘要。

方法注释的内容 (1,5,6,7项 要求写上)
1.类该方法是做什么的 。

2.该方法如何工作。

3.代码修改历史纪录。

4.方法调用代码示范。
5.必须传入什么样的参数给这个方法。@param

6.异常处理。@throws

7.这个方法返回什么。@return

三、在Imports使用通配符的优劣

四、局部变量应该推迟至使用前声明并初始化

五、域(Field)命名

* 非公有,非静态字段命名以m开头。

* 静态域命名以s开头。

* 其他字段以小写字母开头。

* public static final 字段(常量) 全部大写,并用下划线连起来。

public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sSingleton;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}

六、花括号没有独自一行,它们与它前面的代码占同一行

七、命名规则

1.  小写。

com.chinacache.billing 

com.chinacache.billing.node

2.   大小写字母混合组成,头字母大写。

class Raster; 

class ImageSprite;

3.接口 大小写字母混合组成,头字母大写,常以"able"、"ible"结尾。

interface RasterDelegate;

interface Runna ble ;

interface Accessible ;

4.方法 大小写字母混合组成,第一个单词的首字母小写,其后单词的首字母大写。

run();
r unF ast();
g etB ackground();

5.变量、参数 小写 ,不推荐使用下划线 ,简短明晰。

char c;

int i;

float m yW idth;

6.集合、数组 应该从命名中体现其复数的含义,例如加后缀s或前缀some。

customers ;

postedMessages ;
some Customers ;
some Items ;

八、在定义类时,应该按照访问权限的大小分别排列属性和方法。

1. public

2. protected

3. 包级别(没有访问修饰符的,默认为friendly)

4. private

 

写好代码以后对照代码规范一项一项检查一下吧。

(1)Eclipse 代码格式化

你可以导入development/ide/eclipse下的文件,使得Eclipse按照Android代码风格规则。选择 “Window › Preferences › Java › Code Style,使用 “Formatter › Import” ,导入android-formatting.xml,”Organize Imports › Import” 导入 android.importorder.

(2)eclipse tab 设置为4个空格:

Preferences -> General -> Editors -> Text Editors:

Insert spaces for tabs

(3)自动格式化代码Ctrl+Shift+F

(4)全局 查找并替换 Ctrl+F

 

[参考]
http://source.android.com/source/code-style.html
http://wenku.baidu.com/view/ce17addd5022aaea998f0fad.h
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: