Home  >  Article  >  Java  >  Java Coding Standards (Commonly Used Key Points)

Java Coding Standards (Commonly Used Key Points)

亚连
亚连Original
2018-05-10 10:15:561862browse

The following is the Java coding specification I compiled. When writing code, you must follow the coding specification to facilitate daily maintenance.

Naming convention

##Class naming convention

The first letter of each word in the class needs to be capitalized, such as UserService, wrong naming method userService, userservice

The test case ends with Test, such as UserServiceTest

If it is abbreviated with terms At the beginning, term abbreviations should be all capitalized, such as HTMLEditor. Wrong writing...

Class names should use English letters or numbers, and no special characters should appear

Interfaces do not start with I

Method naming convention

The first letter of the first word is lowercase, and the first letter of other words is capitalized

It should be visible from the method name The role of the method

Coding specifications

##Code indentationCode indentation is a tab (length of 4 spaces). Eclipse defaults to a length of 4 spaces.

ScopeThe properties in the class should be set to private, and external classes can modify private properties by providing get and set methods.

If the method in the class is only for internal use of the class, it should be set to private; if it can be used by subclasses, it should be set to protected; if it is a public method, it should be set to public.

Comment specification

Copyright information commentCopyright information comment At the beginning of the file, used to declare the copyright of the code. Use comments like /**/.

/* 
 * Copyright ©  2015 TIAMAES Inc. All rights reserved.
 */
package com.tiamaes.gjds.das.controller;

The comment template is as follows, Window->Preferences->Java->Code Style->Comments->Files

/*
* Copyright ©  ${year} TIAMAES Inc. All rights reserved.
*/

Copyright © 2015 TIAMAES Inc. All rights reserved. The description is as follows

Copyright 2015 Tianmai Technology Co., Ltd. All rights reserved.

- Inc. A joint stock limited company formed in accordance with the Company Law
- Co. Ltd Limited liability company

Class annotation specifications

Class annotation information should be Contains class description information, author information and version information.

/**  
 * 类描述
 * @author 王成委
 * @since 1.0
 * @see xxx
 */
public class TypeName

The comment template is as follows, Window->Preferences->Java->Code Style->Comments->Types

/**  
 * ${todo}
 * @author 王成委
 * @since 1.0
 */

类描述:描述类的功能,单行直接写,如果多行需要使用e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3

@author:多个作者使用多个@author

@since:说明此类是从那个版本开始

@see:与类相关的其他类或方法 
可参考org.springframework.stereotype.Controller的类注释信息。

/**
 * Indicates that an annotated class is a "Controller" (e.g. a web controller).
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 * It is typically used in combination with annotated handler methods based on the
 * {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
 *
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see org.springframework.web.bind.annotation.RequestMapping
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
 */

方法注释

使用下面的模版

/**
 * ${todo}
 * ${tags}
 */

${tags}:自动生成参数、异常、返回值等注解

/**
 * TODO
 * @param request
 * @throws IOException
 */
@RequestMapping("/ctx")public void test(HttpServletRequest request) throws IOException

如果类中的方法实现了抽象方法或重写了父类的方法,应在方法上加上@Override注解。如果要覆盖父类方法的注释可以使用/** */注释来覆盖父类的注释。

org.springframework.core.io.Resource
/**
 * Return a File handle for this resource.
 * @throws IOException if the resource cannot be resolved as absolute
 * file path, i.e. if the resource is not available in a file system
 */
File getFile() throws IOException;

在实现的方法上使用/**...*/可以覆盖父类方法的注释。

org.springframework.core.io.AbstractResource
/**
 * This implementation throws a FileNotFoundException, assuming
 * that the resource cannot be resolved to an absolute file path.
 */
@Overridepublic File getFile() throws IOException {    
throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
}

属性和变量及方法内代码的注释

使用//来对变量和属性注释,方法内的代码也使用//注释

如非必要变量和属性可以不加注释,如果代码内有负责逻辑应使用//注释加以说明

public ServletContextResource(ServletContext servletContext, String path) {    // check ServletContext
    Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");    this.servletContext = servletContext;    // check path
    Assert.notNull(path, "Path is required");
    String pathToUse = StringUtils.cleanPath(path);    
    if (!pathToUse.startsWith("/")) 
    {
      pathToUse = "/" + pathToUse;
    }    
    this.path = pathToUse;
}

以上是我所整理的一部分JAVA编码规范,希望今后大家做开发时能够严格按照编码规则来开发,这样有便于日常维护。


The above is the detailed content of Java Coding Standards (Commonly Used Key Points). For more information, please follow other related articles on the PHP Chinese website!

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