Java语言编程规范——编码规范

  • 明确方法功能,精确(而不是近似)地实现方法设计。一个函数仅完成一件功能,即使简单功能也应该编写方法实现。
    说明:虽然为仅用一两行就可完成的功能去编方法好象没有必要,但用方法可使功能明确化,增加程序可读性,亦可方便维护、测试。

  • 对方法参数的合法性检查由方法本身负责,方法调用者不负责。
    说明:对于模块间接口方法的参数的合法性检查这一问题,往往有两个极端现象,即:要么是调用者和被调用者对参数均不作合法性检查,结果就遗漏了合法性检查这一必要的处理过程,造成问题隐患;要么就是调用者和被调用者均对参数进行合法性检查,这种情况虽不会造成问题,但产生了冗余代码,降低了效率。

  • 明确类的功能,精确(而非近似)地实现类的设计。一个类仅实现一组相近的功能。
    说明:划分类的时候,应该尽量把逻辑处理、数据和显示分离,实现类功能的单一性。
    示例:
    数据类不能包含数据处理的逻辑。
    通信类不能包含显示处理的逻辑。

  • 数据库操作、IO操作等需要使用结束close()的对象必须在try -catch-finally 的finally中close()。
    示例:

try
{
    // ... ...
}
catch(IOException ioe)
{
    //... ...
}
finally
{
    try
    {
        out.close();
    }
    catch (IOException ioe)
    {
        //... ...
    }
}
  • 异常捕获后,应该纪录日志或者ex.printStackTrace() 。
    说明:若有特殊原因必须用注释加以说明。
    示例:
try
{
    //.... ...
}
catch (IOException ioe)
{
    ioe.printStackTrace ();
}
  • 采用错误返回码代替抛出异常。一个函数必须在其内部对所有异常进行处理, 禁止主动抛出的异常(禁止使用throw new IOException()),采用错误返回码代替抛出异常。
  • 注意运算符的优先级,并用括号明确表达式的操作顺序,避免使用默认优先级。
    说明:防止阅读程序时产生误解,防止因默认的优先级与设计思想不符而导致程序出错。
    示例:
    下列语句中的表达式
word = (high << 8) | low     (1)
if ((a | b) && (a & c))      (2)
if ((a | b) < (c & d))       (3)

如果书写为

high << 8 | low
a | b && a & c
a | b < c & d

虽然不会出错,但语句不易理解;造成了判断条件出错。

  • 避免使用不易理解的数字,用有意义的标识来替代。涉及物理状态或者含有物理意义的常量,不应直接使用数字,必须用有意义的静态变量来代替。
    示例:如下的程序可读性差。
if (state == 0)
{
    state = 1;
    ...  // program code
}

应改为如下形式:

private final static int TRUNK_IDLE = 0;
private final static int TRUNK_BUSY = 1;
private final static int TRUNK_UNKNOWN = -1;    
if (state == TRUNK_IDLE)
{
    state = TRUNK_BUSY;
    ...  // program code
}
  • 数组声明的时候使用int index[],而不要使用int[] index。但函数返回值为数值类型时应使用int[] getIndex(),而不要使用int getIndex()[]。
    示例:
    如下程序可读性差:
public int getIndex()[]
{
    ....
}
如下程序可读性好:
public int[] getIndex()
{
    ....
}
  • 调试代码的时候,禁止使用 System.out 和 System.err 进行打印,应该使用log4j统一打印。
    说明:代码发布的时候可以统一关闭调试代码,定位问题的时候又可以打开开关。

  • 禁止在版本正式代码中写测试代码。测试代码应使用junit单独编写。

  • 一个函数除注释、空行外的代码行数不得超过50行。如果一个函数的代码行数超过50行,必须把它拆分为多个函数,直到每个函数的代码行数小于等于50行为止。
  • 禁止使用难懂的技巧性很高的语句。禁止使用不常用的语法。
    说明:高技巧语句不等于高效率的程序,实际上程序的效率关键在于算法。

####建议
- 记录异常不要保存exception.getMessage(),而要记录exception.toString()。
示例:NullPointException抛出时常常描述为空,这样往往看不出是出了什么错。

  • 异常捕获尽量不要直接 catch (Exception ex),应该把异常细分处理。

  • 如果多段代码重复做同一件事情,那么在方法的划分上可能存在问题。
    说明:若此段代码各语句之间有实质性关联并且是完成同一件功能的,那么可考虑把此段代码构造成一个新的方法。

  • 源程序中关系较为紧密的代码应尽可能相邻。
    说明:便于程序阅读和查找。
    示例:矩形的长与宽关系较密切,放在一起。
rect.length = 10;
rect.width = 5;

本文作者:Rance935本文出处:Java语言编程规范——编码规范转载请在开头注明作者详细信息和本文出处
欢迎关注我的微信公众号和QQ群,分享Android 开发和互联网内容
Android技术分享:群号534813930
微信号:androidparks
公众号:AndroidParks

评论

  • José Wilame Araújo Rodrigues回复

    Useful info. Fortunate me I found your web site accidentally, and I am surprised why this twist of fate didn't came about in advance! I bookmarked it.|

  • justbingosites.co.uk回复

    We appreciate, result in I ran across what exactly I had been seeking. You could have wrapped up my own Some evening extended quest! Our god Bless you man. Use a fantastic time. Ok bye

  • acne treatment quinoderm回复

    Hello! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche.

    Your blog provided us useful information to work on. You have done a outstanding job!

  • root canal treatment reddit回复

    My brother suggested I might like this web site.
    He was entirely right. This post actually made my day. You can not imagine simply how much time I had spent for this info!
    Thanks!

  • aids 86回复

    Interesting blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog jump out.
    Please let me know where you got your theme.
    Thanks

  • CheplanovaGergo回复

    le viagra rembourse

    http://viagralms.com/ - cheap viagra

    <a href="http://viagralms.com/">buy viagra</a>

    pharmacy viagra silagra

  • cervical cancer japan回复

    We're a bunch of volunteers and starting a new scheme
    in our community. Your web site provided us with useful information to
    work on. You have performed an impressive job and our whole neighborhood might be thankful to you.

  • dengue journal回复

    Hurrah! In the end I got a website from where I
    be able to actually take useful information regarding my study and knowledge.

  • adidas nmd回复

    My wife and i were quite more than happy that John managed to do his basic research with the ideas he was given through your web page. It is now and again perplexing to just always be releasing facts which usually many others may have been making money from. We know we have the writer to give thanks to because of that. Most of the illustrations you made, the easy site navigation, the friendships you will help engender - it is most astonishing, and it's assisting our son in addition to our family do think the topic is cool, and that's wonderfully essential. Many thanks for the whole thing!

  • thread lift near me回复

    This is very interesting, You're a very skilled blogger.
    I have joined your feed and look forward to seeking more of your magnificent
    post. Also, I have shared your site in my social networks!

  • กลิ่นปาก回复

    Hiya! I know this is kinda off topic nevertheless I'd figured I'd ask.
    Would you be interested in exchanging links or maybe
    guest writing a blog article or vice-versa? My website discusses a
    lot of the same subjects as yours and I feel we could greatly benefit from
    each other. If you happen to be interested feel free to shoot me an e-mail.

    I look forward to hearing from you! Terrific blog by the way!

  • Berita Jepang回复

    You got a very good website, Gladiola I detected it through yahoo.

  • ดัดฟัน回复

    I think that is one of the so much vital information for me.
    And i'm satisfied reading your article. But should remark
    on few normal issues, The site style is ideal, the articles is in reality excellent :
    D. Good process, cheers

  • tuberculosis o'hare回复

    Hi, i read your blog occasionally and i own a similar one
    and i was just wondering if you get a lot of spam responses?
    If so how do you protect against it, any plugin or
    anything you can suggest? I get so much lately it's driving me mad so
    any support is very much appreciated.

  • รับทุบตึก回复

    After looking over a number of the articles on your web
    page, I really appreciate your technique of blogging. I book
    marked it to my bookmark site list and will be checking back soon. Please visit my web site too and tell me
    how you feel.

  • virus mers 2015回复

    Hello There. I discovered your blog using msn. That is a very well
    written article. I will be sure to bookmark it and return to
    learn extra of your helpful info. Thanks for the post.
    I will certainly return.

  • bored piling philippines回复

    These are actually wonderful ideas in regarding blogging.
    You have touched some pleasant factors here. Any way keep up wrinting.

  • กลูต้า回复

    I will immediately grasp your rss as I can not in finding your email subscription hyperlink or e-newsletter service.
    Do you've any? Kindly allow me recognize so that I may subscribe.
    Thanks.

  • visit website回复

    There as noticeably a bundle to find out about this. I assume you made sure good points in features also.

  • seo回复

    You ave made some decent points there. I looked on the web for more information about the issue and found most individuals will go along with your views on this web site.

  • bored piling works回复

    Fine way of telling, and fastidious paragraph to take data concerning my presentation topic, which i am
    going to convey in university.

  • stomach acid ph回复

    It's going to be finish of mine day, except before
    ending I am reading this wonderful article to increase my experience.