几个流行的Java代码规范

什么是最佳的Java代码编程规范?这可能是一个众口难调的问题。那么,不妨换个问题,什么代码规范最流行?sideeffect.kr通过分析GitHub上托管的开源代码,得出了一些有趣的结果。一起来看看吧。

空格对Tab缩进

空格,74.282 %; Tab,25.718%;基于1,879,062提交。

块语句空格

花括号前空一格(73.432 %)

if  {
 
}while  {
 
}

switch  {
 
}

花括号另起一行(20.33 %)

if {
 
}while {
 
}

switch
{
 
}

花括号后无空格(6.239 %)

if {
 
}while {
 
}

switch {
 
}

基于1,145,733次提交统计

常量大写

常量大写(51.829 %)

final static String FOO_BAR = "baz";
static final String FOO_BAR = "baz";

常量不大写(48.171 %)

final static String foobar = "baz";
static final String foobar = "baz";

条件语句

有空格(83.925 %)

if (true) {
 
}while (true) {
 
}switch (v) {
 
}

无空格(16.075 %)

if(true) {
 
}while(true) {
 
}switch(v) {
 
}

基于1,331,612次提交统计

参数定义

无空格(97.191 %)

public void setName(String name) {
 
}if(isTrue) {}while(isTrue) {}

有空格(2.809 %)

public void setName( String name ) {
 
}if( isTrue ) {}while( isTrue ) {}

最长行

80,91.35 %;120,7.267 %;150,1.383 %:基于1,917,477次提交统计。

staticvar

不使用特殊前缀(99.638 %)

static String name

使用特殊前缀(0.362 %)

static String _name;

final和static的顺序

access modifier - static - final|volatile (94.868 %)

public static final String t1 = "";
public static transient final String t2 = "";
transient public static final String t3 = "";

access modifier - final|volatile - static (4.802 %)

public final static String t1 = "";
public final static transient String t2 = "";
transient public final static String t3 = "";

static - access modifier - final|volatile(0.179 %)

static public final String t1 = "";
static public transient final String t2 = "";
static transient public final String t3 = "";

final|volatile - access modifier - static (0.151 %)

final public static String t1 = "";
final public static transient String t2 = "";
final transient public static String t3 = "";

基于 371,647 次提交统计

流行的不一定是好的(比如流行性感冒),但是从交流的角度来说,按照流行的风格编写代码,可以让你的代码在大多数人看起来更习惯。

编撰 SegmentFault

原文地址:https://segmentfault.com/a/1190000000411648

推荐阅读

我是一个Java class

11个简单的Java性能调优技巧

Java虚拟机性能监控与调优实战


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值