1.不要忽视异常处理
如果像下面的代码这样,对catch后的异常作空处理,就像埋下地雷一样让人感觉到毛骨悚然:
错误的做法:
[java] view plaincopyvoid setServerPort(String value) { try { serverPort = Integer.parseInt(value); } catch (NumberFormatException e) { } }正确的做法(1):
在方法声明时抛出异常,由客户程序员去负责消化这个异常。
[java] view plaincopyvoid setServerPort(String value) throws NumberFormatException { serverPort = Integer.parseInt(value); }正确的做法(2):
[java] view plaincopy/** 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 }正确的做法(3):
[java] view plaincopy/** 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):
[java] view plaincopyvoid setServerPort(String value) throws ConfigurationException { try { serverPort = Integer.parseInt(value); } catch (NumberFormatException e) { throw new ConfigurationException(“Port ” + value + “ is not valid.”); } }
2、不要偷懒而捕捉一般异常
下面代码一概捕捉Exception异常,大小通吃是不对的,这样会让你在错误出现时难以定位到错误原因,一般异常无法用统一方法进行异常处理。
错误的做法:
[java] view plaincopytry { 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! }1.顶部版权声明2.包和引入块(每一块以空白行分隔)3.类或接口的声明。 在Javadoc注释,描述的类或接口的用途。
[java] view plaincopy/* * 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.必须传入什么样的参数给这个方法。@param6.异常处理。@throws7.这个方法返回什么。@return
* 非公有,非静态字段命名以m开头。 * 静态域命名以s开头。* 其他字段以小写字母开头。* public static final 字段(常量) 全部大写,并用下划线连起来。
[java] view plaincopypublic 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. public2. protected3. 包级别(没有访问修饰符的,默认为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.htmlhttp://wenku.baidu.com/view/ce17addd5022aaea998f0fad.h
新闻热点
疑难解答