search
HomeJavajavaTutorialjava code coding specifications
java code coding specificationsNov 30, 2016 am 09:51 AM
javaCoding Standards

Standards need to be paid attention to during the daily coding process, and it is a good habit to develop slowly

1. Basic principles

Mandatory principles:
1. StringBuilder must be used for string addition operations;
2.try…catch Usage

try{
}catch{Exception e
    e.printStackTrace();
}finally{
}//在最外层的Action中可以使用,其它地方一律禁止使用;
try{ //程序代码 }catch(Exception e){ //为空,什么都不写 }//在任何场景中都禁止使用
try{
}catch{Exception e throw new runtimeException(e);//最优先采用的写法 }finally{
}

1. For situations where you don’t know what to do or how to handle it after catching it, don’t catch the exception and leave it to the outgoing layer to catch and handle it;
2. If the return type is a collection, in the method declaration Generics must be used, and the javadoc must indicate under what circumstances null is returned and under what circumstances an empty collection is returned.
3. For methods and variable declaration scopes, the following priorities should be adopted: private, protected, public. For variables, the following priorities should be adopted: local variables, instance variables, and class variables. If instance variables or class variables must be used, Next, to ensure thread safety, try to use ThreadLocal to save instance variables or class variables if possible;
4. If it is not necessary, do not define variables or new objects in the loop; try to use new objects at the last moment when needed;
5. If it is not necessary, do not use try...catch in the loop;
6. For more complex logic in the class, line comments should be used for comments. Block comments are absolutely not allowed in java code (/**/ ) to comment;
7. The first letter of the name of the Java class must be capitalized. It consists of multiple words, and the first letter of each word is capitalized
8. The file name of jsp must be all lowercase;
9. Spring’s bean The configuration file name must be lowercase, in the format of xxx.bean.xml, 10. The configuration file name of xwork must be lowercase and written in the format of xwork_xxx.xml, where XXX is the abbreviation of the business name;
11. Log processing;

if (log.isDebugEnabled())
ex.printStackTrace(); else log.error("从数据库删除: [" + entity.getClass().getName() + "] 实例失败", daex); 
throw new PersistenceException("从数据库删除: [" + entity.getClass().getName()+ "] 实例失败", daex);

  12.代码中严禁使用System.out.println()进行调试输出,如果要使用调试信息,必须使用log.debug()。对于必要的信息使用log.info()进行输出;
    13.类中不要出现无用import,可以采用IDE工具进行优化,类提交前进行代码的格式化;
    14.有业务逻辑处理的类必须写junit单元测试类;
    15.国际化的支持:ftl模板中不允许出现中文字符,要全部放到相应的properties文件中,properties文件要放到和Action类同样的目录中;ftl的编码要全部采用UTF-8的格式;properties文件的命名:中文:Action名称+“_zh”+“_CN”.properties,英文:Action名称+“_en”+“_US”.properties
    16.一个程序文件最好不要超过2000行
    17.尽可能缩小对象的作用域,这样对象的可见范围和生存期也都会尽可能地小,尽所有可能优先采用局部变量,实在没有办法用全局变量的,优先采用ThreadLocal来处理。
    18.一个方法所完成的功能要单一,不同的功能封装为不同的方法.
    19.尽可能的处理异常或转换异常,不要一味的包装异常
    20.如果对象在某个特定范围内必须被清理(而不是作为垃圾被回收),请使用带有finally子句的try块,在finally子句中进行清理。
    21.对于把一些逻辑相关的类组织在一起,可以考虑把一个类的定义放在另一个类的定义中,这种情况推荐使用内部类(比如界面层中的事件响应等)。内部类拥有所有外围类所有成员的访问权。
    22.对成员变量的访问最好通过getter/setter方法,这样能够保证访问的合法性,以及代码调整
    23.优先选择接口而不是抽象类或具体类。如果你知道某些东西将成为基类,你应当优先把它们设计成接口;只有在必须放进方法定义或成员变量时,才把它修改为具体或抽象类。接口只和客户希望的动作有关(协议),而类则倾向于关注实现细节。
    24.使用java标准库提供的容器。精通他们的用法,将极大地提高工作效率。优先选择ArrayList来处理顺序结构,选择HashSet来处理集合,选择HashMap来处理关联数组,选择linkedList来处理堆栈和队列,它对顺序访问进行了优化,向List中间插入与删除的开销小,但随机访问则较慢。当使用前三个的时候,应该把他们向上转型为List、Set和Map,这样就可以在必要的时候以其它方式实现
    25.数组是一种效率最高的存储和随机访问对象引用序列的方式,但是当创建了一个数组对象,数组的大小就被固定了,如果在空间不足时再创建新的数组进行复制,这样效率就比ArrayList开销大了。所以必须明确使用场景。
    26.尽量使用”private”、”protected”关键字。一旦你把库的特征(包括类、方法、字段)标记为public,你就再也不可能去掉他们。在这种方式下,实现的变动对派生类造成的影响最小,在处理多线程问题的时候,保持私有性尤其重要,因为只有Private的字段才会受到保护,而不用担心被未受同步控制的使用所破坏。
    27.禁止后台业务代码使用如下代码

try{
    something()
}catch(Exception ex)
{} new Exception()

2.类编写规范

   类的结构组织,一般按照如下的顺序:
     1.常量声明
     2.静态变量声明
     3.成员变量声明
     4.构造函数部分
     5.Finalize部分
     6.成员方法部分
     7.静态方法部分
     8.这种顺序是推荐的,在实际开发中可以按照一定的尺度修改,原则是程序更易读。如对方法的排序按照重要性,或按照字母顺序排列或按照方法之间的关系排列。
     9.每个方法(包括构造与finalize)都是一个段。多个变量声明按照逻辑共同组成一个段,段与段之间以空行分隔。
     10.类声明时,要指出其访问控制,一般为没有修饰符,public,和private。
     11.方法与方法之间,大的部分之间都需要以空行隔离。
     12.编写通用性的类时,请遵守标准形式。包括定义equals()、hasCode()、toString()、Clone(实现Cloneable接口),并实现Comparable和Serialiable接口
     13.对于设计期间不需要继承的类,尽量使用final

3.变量编写规范

   1.对成员变量, 尽量采用private 
   2.每一个变量声明/定义占一行(参数变量除外),如

int a; int b;

        比int a,b; 更容易读, 更容易查找bug

   3.局部变量在使用前必须初始化,一般在声明时初始化
   4.变量的声明要放在程序块的开始位置

     如

public void myMethod() {  int int1 = 0; // beginning of method block  if (condition) {  int int2 = 0; // beginning of "if" block  ...
  }
}

    一种例外情况是在for语句中,定义声明不仅不占一行,还在表达式内部,完全采用Eclips生成,如:

for(int i = 0; i<100; i++)

    5.数组的申明采用 方式如

char[] buffer;

而不是

char buffer[];

4.方法编写规范

    1.对成员方法,不要轻易的采用public的成员变量。主要的修饰符有public, private, protected, 无
    2.空方法中方法声明和函数体可都在一行。如: void func(){}
    3.方法和方法之间空一行
    4.方法的文档注释放在方法的紧前面,不能空一行。
    5.避免过多的参数列表,尽量控制在5个以内,若需要传递多个参数时,当使用一个容纳这些参数的对象进行传递,以提高程序的可读性和可扩展性
    6.方法中的循环潜套不能超过2层
    7.对于设计期间不需要子类来重载的类,尽量使用final
    8.每个方法尽量代码行数尽量不要超过100行(有效代码行,不包括注释),但必须保证逻辑的完整性
    9.接口中的方法默认级别为protected,只有很确认其它子系统的包会调用自己子系统的接口中的方法时,才将方法暴露为public.

5.语言使用及书写规范

   1.避免变量的定义与上一层作用域的变量同名。
   2.方法与方法之间用需要用一空行隔开
   3.局部变量在使用时刻声明,局部变量/静态变量在声明时同时初始化
   4.在与常数作比较时常数放在比较表达式的前面如:

if(“simpleCase”.equals(obj))… if(null == obj)….

   5.return语句中,不要有复杂的运算。
   6.switch语句,需要一个缺省的分支


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.