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

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

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

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

  • 数据库操作、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

评论

  • universalsitesupplies.co.uk回复

    Wonderful blog you have here but I was wanting to know if you knew of any discussion boards that cover the same topics discussed in this article? I'd really like to be a part of online community where I can get suggestions from other experienced individuals that share the same interest. If you have any recommendations, please let me know. Thanks!

  • adidas nmd回复

    There are actually lots of details like that to take into consideration. That could be a great point to carry up. I provide the thoughts above as normal inspiration however clearly there are questions like the one you bring up the place an important thing will be working in honest good faith. I don?t know if best practices have emerged around issues like that, but I'm sure that your job is clearly recognized as a good game. Both girls and boys feel the influence of only a moment抯 pleasure, for the remainder of their lives.

  • dog boarding回复

    I’m not sure where you are getting your information, but great topic. I needs to spend some time learning much more or understanding more. Thanks for wonderful information I was looking for this info for my mission.

  • Computer Software回复

    Hello, Neat post. There is a problem together with your site in web explorer, would check this¡K IE still is the market leader and a huge portion of people will leave out your excellent writing due to this problem.

  • XXX回复

    I'm very happy to read this. This is the type of manual that needs to be given and not the random misinformation that's at the other blogs. Appreciate your sharing this greatest doc.

  • Fake seller How to be penalized!回复

    Someone necessarily help to make significantly posts I would state. This is the very first time I frequented your website page and so far? I amazed with the analysis you made to create this particular post extraordinary. Excellent job!

  • yeezy boost 350 v2回复

    Needed to put you a tiny observation to give many thanks once again with your pretty strategies you have documented at this time. It has been simply generous with people like you to provide easily exactly what numerous people could possibly have made available as an ebook to get some cash for their own end, especially considering the fact that you could possibly have tried it in the event you considered necessary. The smart ideas as well served as the easy way to know that other individuals have a similar passion really like mine to find out lots more concerning this condition. I'm sure there are several more pleasant times up front for many who looked at your site.

  • porn movies回复

    Very interesting info !Perfect just what I was searching for! "I meant what I said, and I said what I meant. An elephant's faithful, one hundred percent." by Dr. Seuss.

  • justbedding.com.au回复

    Hello There. I found your blog using msn. This is an extremely well written article. I will make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll certainly comeback.

  • web design company回复

    Thanks for sharing excellent informations. Your website is very cool. I'm impressed by the details that you have on this blog. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my pal, ROCK! I found just the info I already searched all over the place and just couldn't come across. What a great web-site.

  • http://www.linux.org回复

    If you еnnd up excited about a brand new ρrofssion ɑs a paralegal,
    there are a selection of cһoices which youll be able tto consider.
    Yⲟu may dеtermine that being a freelance paralegаl is the
    best way that you wish to pursue this field. You may beɡin by wdighing
    tthe pros and cons off this exciting new means oof working within the paralegal subject; and its possible youll resolve that it is the best choice
    for you.

  • adidas nmd回复

    I wish to convey my gratitude for your kindness in support of those individuals that require help on in this theme. Your special dedication to passing the solution around was surprisingly informative and has usually permitted professionals just like me to reach their objectives. Your new invaluable report indicates a whole lot a person like me and further more to my office colleagues. Best wishes; from everyone of us.

  • adidas ultra boost回复

    Thanks for all of your labor on this web site. Kim really likes working on investigation and it's really obvious why. We hear all about the lively mode you deliver vital solutions via the web site and as well as inspire contribution from the others on this article while my simple princess is really being taught a lot of things. Enjoy the rest of the new year. You are always performing a great job.

  • health clinic回复

    Great blog here! Also your site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  • XXX回复

    I definitely wanted to send a brief note to be able to thank you for those fabulous points you are placing here. My time consuming internet investigation has at the end of the day been compensated with reasonable know-how to exchange with my partners. I 'd believe that we readers are very blessed to exist in a fabulous community with very many wonderful people with very beneficial plans. I feel really lucky to have encountered the webpage and look forward to really more brilliant times reading here. Thanks once more for a lot of things.

  • M88回复

    I think other web site proprietors should take this site as an model, very clean and excellent user genial style and design, let alone the content. You're an expert in this topic!

  • computer waste disposal回复

    I¡¦ll immediately grab your rss feed as I can not to find your e-mail subscription link or e-newsletter service. Do you have any? Please allow me know so that I may just subscribe. Thanks.

  • Porno回复

    I really like your writing style, great information, regards for posting : D.

  • burberry scarf回复

    This site can be a stroll-by way of for the entire data you wanted about this and didn抰 know who to ask. Glimpse right here, and also you抣l definitely uncover it.

  • MHR回复

    Hello my family member! I wish to say that this article is awesome, great written and include almost all vital infos. I would like to see more posts like this.

  • PNL回复

    Its such as you read my mind! You seem to understand so much approximately this, like you
    wrote the e-book in it or something. I think that you just can do with a few p.c.

    to drive the message house a little bit, however other than that,
    that is great blog. An excellent read. I'll certainly
    be back.