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

评论

  • lose weight 900 calories a day回复

    Its like you read my mind! You appear to know so much about
    this, like you wrote the book in it or something. I think that
    you can do with a few pics to drive the message home a
    bit, but instead of that, this is wonderful blog. A fantastic read.
    I will certainly be back.

  • VladilenGergo回复

    tga and viagra

    <a href=http://viagraomz.com/>viagra cheap</a>

    <a href="http://viagraomz.com/">cheap viagra</a>

    viagra celebrity endorsements

  • มะเร็งปากมดลูก回复

    I've been exploring for a bit for any high-quality
    articles or weblog posts on this sort of house . Exploring in Yahoo I ultimately stumbled upon this website.

    Reading this info So i'm satisfied to convey that
    I have an incredibly good uncanny feeling I came upon just what I needed.
    I most surely will make sure to don?t disregard this
    site and provides it a glance on a relentless basis.

  • michael kors purses回复

    There are certainly plenty of particulars like that to take into consideration. That could be a nice point to carry up. I supply the ideas above as general inspiration but clearly there are questions just like the one you convey up where the most important factor might be working in honest good faith. I don?t know if greatest practices have emerged round things like that, however I am certain that your job is clearly identified as a good game. Both girls and boys really feel the impact of only a moment抯 pleasure, for the remainder of their lives.

  • for more information回复

    You completed several fine points there. I did a search on the theme and found mainly persons will have the same opinion with your blog.

  • วัณโรค การติดต่อ回复

    Greetings! I know this is kind of off topic but I was wondering which blog platform are you using
    for this site? I'm getting fed up of WordPress because I've had issues with
    hackers and I'm looking at alternatives for another platform.

    I would be great if you could point me in the direction of a
    good platform.

  • ครีมหน้าใส回复

    Excellent goods from you, man. I have understand your stuff previous to
    and you're just extremely fantastic. I actually like what you've acquired
    here, certainly like what you are stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it smart.
    I can't wait to read far more from you. This is really a tremendous website.

  • for details回复

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is excellent, as well as the content!

  • กำจัดปลวก回复

    Thanks , I've recently been searching for information approximately this topic for a long time and yours is the greatest I've found out so
    far. But, what concerning the conclusion? Are you certain in regards to the supply?

  • CheplanovaGergo回复

    can i wank then take viagra

    http://viagralms.com/ - canadian viagra

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

    what happened if take viagra

  • Continuing Education回复

    Wow, fantastic blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, let alone the content!

  • rent a car回复

    You made a number of fine points there. I did a search on the issue and found most people will go along with with your blog.

  • Law Dictionary回复

    I'm extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one nowadays..

  • จีคลับ回复

    In fact, a systematic graphical representation is done by professional designers who have creative bent

  • termites what do they look like回复

    No matter if some one searches for his essential thing, thus he/she needs to be available that in detail, therefore that thing is maintained over here.

  • José Wilame Araújo Rodrigues回复

    We are a group of volunteers and starting a brand new scheme in our community. Your site provided us with useful information to work on. You have performed an impressive activity and our entire community will be thankful to you.|

  • MakritskayaGergo回复

    where do you buy your viagra

    <a href=http://viagraoail.com/>online viagra</a>

    <a href="http://viagraoail.com/">viagra cheap</a>

    beat way to order viagra

  • รับผลิตอาหารเสริม回复

    This design is incredible! You certainly know how
    to keep a reader entertained. Between your wit and your videos,
    I was almost moved to start my own blog (well, almost...HaHa!) Great job.
    I really loved what you had to say, and more than that, how you presented it.
    Too cool!

  • online resume writing service reviews回复

    Hey! Remarkable post! I really enjoy the method that you explained Java语言编程规范——编码规范.

    Minimum I’ve proven great brief article with regards to
    Java语言编程规范——编码规范! Times scour are over these days.
    Say thanks a ton, person responsible! You’ve done an excellent responsibility.

    May you blog a couple of e book in addition to tips for folks really have posses writing ability?

    It is normally superb significant, you will the youngsters,
    just because present day day university students only have to craft
    a sizable magnitude of various reports for college then educational facilities .

    Through the you can find be around e-commerce sites such as this one online resume writing service reviews which always making testimonies on your
    many paper publishing services

  • Jose Wilame Araujo Rodrigues回复

    Excellent post. I was checking constantly this blog and I'm impressed! Extremely helpful info particularly the last part 😊 I care for such information much. I was seeking this certain information for a very long time. Thank you and best of luck.|

  • AdamGergo回复

    acquistare cialis soft

    http://canadianiucialis.com/ - canadian cialis

    <a href="http://canadianiucialis.com/">cialis online</a>

    cialis 20 mg look like

  • yeezys回复

    My husband and i got fortunate Chris could finish up his studies from the precious recommendations he discovered in your web page. It's not at all simplistic just to choose to be freely giving information and facts that many the others may have been trying to sell. And we all discover we have the blog owner to thank because of that. The main illustrations you have made, the straightforward website menu, the friendships you will aid to engender - it is many spectacular, and it's really making our son in addition to our family consider that this concept is pleasurable, and that's extraordinarily pressing. Thanks for all!

  • โรคหัวใจ回复

    Hi, all is going sound here and ofcourse every one
    is sharing facts, that's genuinely good, keep up writing.

  • i-sunglasses limited回复

    Hi there! I know this is kinda off topic however , I'd figured
    I'd ask. Would you be interested in trading links
    or maybe guest authoring a blog article or vice-versa? My
    website covers a lot of the same subjects as yours and I think we could greatly benefit from each other.
    If you happen to be interested feel free to send me an email.
    I look forward to hearing from you! Great blog by the way!

  • โรคพิษสุนัขบ้า回复

    My partner and I stumbled over here from a different web page
    and thought I might as well check things out. I like what I see
    so i am just following you. Look forward to finding
    out about your web page yet again.

  • Outerwear回复

    Wow! Thank you! I constantly wanted to write on my blog something like that. Can I implement a fragment of your post to my website?

  • เบอร์สวย回复

    Hey! Do you know if they make any plugins to
    assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains.
    If you know of any please share. Many thanks!

  • Baby nursery home design回复

    Hi there, I found your website by the use of Google at the same time as looking for a similar subject, your website got here up, it seems to be great. I have bookmarked it in my google bookmarks.

  • Hair Loss回复

    I must show my affection for your generosity for folks that really need help with this particular study. Your special commitment to getting the message around became surprisingly important and have really permitted many people just like me to get to their pursuits. Your new insightful guide signifies so much a person like me and additionally to my mates. Thank you; from all of us.

  • tory burch shoes回复

    you have an awesome weblog here! would you like to make some invite posts on my blog?

  • VladilenGergo回复

    look here viagra low price

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

    <a href="http://viagraomz.com/">online viagra</a>

    liquide suisse viagra

  • Egli Diana Pinto回复

    It's remarkable to pay a quick visit this web page and reading the views of all friends on the topic of this paragraph, while I am also keen of getting experience.|

  • American Parenting回复

    I want to express some appreciation to this writer for bailing me out of this type of challenge. Right after surfing through the online world and seeing ways which are not helpful, I assumed my entire life was well over. Living devoid of the approaches to the difficulties you have fixed by means of your good posting is a critical case, as well as the kind which could have adversely affected my entire career if I had not noticed your blog. Your own talents and kindness in handling every part was invaluable. I don't know what I would've done if I hadn't come across such a step like this. I can now look ahead to my future. Thanks a lot so much for this expert and results-oriented guide. I won't hesitate to propose your web site to anyone who should have guidance on this subject matter.

  • cool shoes回复

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a little bit, but other than that, this is fantastic blog. A fantastic read. I will definitely be back.

  • Computer Store回复

    I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my difficulty. You're amazing! Thanks!

  • Nannik TV回复

    I really liked your blog.Much thanks again. Will read on

  • glutathione回复

    I absolutely love your site.. Excellent colors & theme.

    Did you create this site yourself? Please reply back
    as I'm wanting to create my own personal blog and would
    love to learn where you got this from or what the theme is named.

    Appreciate it!

  • white face cream recipe回复

    Exceptional post however , I was wanting to know if you could write a litte more
    on this topic? I'd be very grateful if you could elaborate a little bit more.
    Many thanks!

  • google display network 2015回复

    I am sure this paragraph has touched all the internet viewers, its really really fastidious post
    on building up new website.

  • mopeio回复

    Thanks again for the blog post.Really looking forward to read more. Keep writing.