Java语言编程规范——排版规范

对于排版的规范,可能大家很多都知道用ctrl+shift+f一键格式化,那么这个一键格式化遵循了什么样的规则呢?当然很多公司也有自己的一个formatting.xml 格式化规范文件。今天我们就来具体了解下这个一键格式化到底包括哪些内容。
- 程序块要采用缩进风格编写,缩进1个TAB键。
- 较长的语句、表达式或参数(>120字符)要分成多行书写,长表达式要在低优先级操作符处划分新行,操作符放在新行之首,划分出的新行要进行适当的缩进,使排版整齐,语句可读。
示例:

if (filename != null 
     && new File(logPath + filename).length() < LogConfig.getFileSize())
{
    ... // program code
}   
public static LogIterator read(String logType, Date startTime, Date endTime,
                                 int logLevel, String userName, int bufferNum)
  • 不允许把多个短语句写在一行中,即一行只写一条语句
    示例:如下例子不符合规范。
LogFilename now = null;        LogFilename that = null;

应如下书写:

LogFilename now = null;
LogFilename that = null;
  • if, for, do, while, case, switch, default 等语句自占一行,且if, for, do, while等语句的执行语句无论多少都要加括号{}。
    示例:如下例子不符合规范。
if(writeToFile)           writeFileThread.interrupt();

应如下书写:

if(writeToFile)
{
     writeFileThread.interrupt();
}
  • 相对独立的程序块之间、变量说明之后必须加空行。
    示例:如下例子不符合规范。
if(log.getLevel() < LogConfig.getRecordLevel())
{
    return;
}
LogWriter writer;

应如下书写:

if(log.getLevel() < LogConfig.getRecordLevel())
{
     return;
}
//空一行
LogWriter writer;
int index;
  • 对齐只使用TAB键,不使用空格键。
  • 在两个以上的关键字、变量、常量进行对等操作时,它们之间的操作符之前、之后或者前后要加空格。
    说明:采用这种松散方式编写代码的目的是使代码更加清晰。
    示例:
    (1)逗号、分号只在后面加空格。
int a, b, c; 

(2)比较操作符, 赋值操作符"="、 "+=",算术操作符"+"、"%",逻辑操作符"&&"、"&",位域操作符"<<"、"^"等双目操作符的前后加空格。

if (current_time >= MAX_TIME_VALUE) 
a = b + c;
a *= 2;
a = b ^ 2;

(3)"!"、"~"、"++"、"--"、"&"(地址运算符)等单目操作符前后不加空格。

flag =! isEmpty; // 非操作"!"与内容之间
i++;             // "++","--"与内容之间

(4)"."前后不加空格。

p.id = pid;     // "."前后不加空格

(5)if、for、while、switch等与后面的括号间应加空格,使if等关键字更为突出、明显。

if (a >= b && c > d)
  • 类属性和类方法不要交叉放置,不同存取范围的属性或者方法也尽量不要交叉放置。
    格式:
类定义
{
     类的公有属性定义
     类的保护属性定义
     类的私有属性定义
     类的公有方法定义
     类的保护方法定义
     类的私有方法定义
}

##最后
以上规范并不完整,欢迎大家补充。

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

评论

  • AlenaGergo回复

    buy cialis egypt

    http://buycialiskj.com/ - generic cialis online

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

    tadapox sale cialis

  • health education回复

    I don’t even know how I ended up here, but I thought this post was great. I don't know who you are but certainly you are going to a famous blogger if you are not already 😉 Cheers!

  • single parents回复

    whoah this weblog is magnificent i love studying your articles. Keep up the great paintings! You already know, a lot of individuals are looking round for this information, you could aid them greatly.

  • single relationship回复

    I like what you guys are up too. Such intelligent work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it'll improve the value of my site 😊

  • health insurance回复

    Whats Going down i am new to this, I stumbled upon this I have discovered It absolutely useful and it has helped me out loads. I'm hoping to give a contribution & help other customers like its helped me. Great job.

  • application回复

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

  • AlenaGergo回复

    buy cialis pills shop online

    http://buycialiskj.com/ - generic cialis online

    <a href="http://buycialiskj.com/">cialis coupon</a>

    generio cialis 10mg

  • meatball recipe回复

    Thanks , I have recently been looking for information approximately this topic for a long time and yours is the best I have came upon so far. However, what in regards to the conclusion? Are you positive concerning the source?

  • dessert recipes回复

    Thanks a lot for sharing this with all of us you actually realize what you're talking about! Bookmarked. Please additionally visit my web site =). We can have a link alternate contract among us!

  • MoisesGergo回复

    buy viagra soft tab

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

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

    how to buy real viagra online

  • home improvement回复

    I¡¦ve learn some good stuff here. Certainly worth bookmarking for revisiting. I surprise how so much attempt you place to make this kind of wonderful informative web site.

  • GoloaGergo回复

    cialis online secure

    <a href=http://canadianfdcialis.com/>generic cialis online</a>

    <a href="http://canadianfdcialis.com/">cialis canada</a>

    the best site cialis purchase

  • AlenaGergo回复

    we like it online cialis sale

    http://buycialiskj.com/ - generic cialis

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

    look here order cialis pill

  • AlenaGergo回复

    cialis con dapoxetina

    http://buycialiskj.com/ - buy cialis

    <a href="http://buycialiskj.com/">buy cialis</a>

    india generic cialis cipla

  • MoisesGergo回复

    buy online viagra

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

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

    osu acheter viagra pfizer

  • GoloaGergo回复

    look there cialis price

    <a href=http://canadianfdcialis.com/>cialis canada</a>

    <a href="http://canadianfdcialis.com/">generic cialis</a>

    cialis inde de tadalafil

  • AlenaGergo回复

    buying cialis in mexico,

    http://buycialiskj.com/ - generic cialis

    <a href="http://buycialiskj.com/">cialis coupon</a>

    cialis generic women

  • MoisesGergo回复

    low price viagra soft uk

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

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

    viagra suisse pharmacie

  • MoisesGergo回复

    viagra prices prescription

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

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

    viagra pour cycliste

  • universalsitesupplies.co.uk回复

    I have been browsing on-line more than 3 hours today, but I by no means found any fascinating article like yours. It is pretty value enough for me. In my opinion, if all web owners and bloggers made good content material as you probably did, the web will likely be a lot more helpful than ever before.

  • AlenaGergo回复

    cialis 10mg generico

    http://buycialiskj.com/ - cialis online

    <a href="http://buycialiskj.com/">buy cialis</a>

    cialis rx kosten