Java Programming Specification

Source: Internet
Author: User
Tags finally block

Good software:

Maintainability
Testability
Reliability
Performance
Simple
Portability

Huawei Program SMaRT Definition

Efficient
Portable
Simple
can be maintained
Reliable
Can be tested

Programming Specification Typography: Principles

Team consistency

Rules:
    1. Add a blank line between different concepts
    2. Put logically tightly related code together
    3. Control the width of a line, not more than 120 characters
    4. Controls the width of a row, adding spaces between different concepts
    5. Control uses indentation to differentiate between different levels of concept (4 spaces)
Suggestions:
    1. Minimizing the scope of local variables
    2. If,for,do,while,case,swich,default such as a line, and if,for,do,while must be parentheses
    3. Control the length of the file, preferably not more than 500 lines
Comments:

Try to explain yourself in code

1. 注释解释代码的意图2. 保证注释与代码一致3. 注释与代码相邻,上下方4. 不要用注释保留废弃代码5. 不要用注释记录修改日志
Naming principles:

The team takes a number name for the package, class, method, variable

Rules:
    1. Prohibit the use of the devil number
    2. Constant naming, consisting of all uppercase words, separated by underscores, and using the static final modifier
    3. Names of variables, attributes, nouns, and the use of the first lowercase hump name method
    4. Name of the method, using the verb and the dynamic-object structure, using the first-letter lowercase hump Name method
      The format is as follows:
      get+ Non-Boolean property name ()
      is+ Boolean property name ()
      set+ Property name ()
      has+ noun/adjective ()
      Verb ()
      Verb + object ()
    5. Naming of classes and interfaces, using first-letter hump nomenclature
    6. The name of the package, consisting of one or more words, all letters lowercase
Suggestions:

array declaration with int [] index instead of int index []

Variable and type principle:

Use static member variables sparingly

Rules:
1. 避免随意进行类型强制转换,应改善设计,或在转换前用instanceof进行判断2. 需要精确计算时不要使用float和double,建议用long,BigDecimal等3. 不能用浮点数作为循环变量,精度问题会导致错误4. 浮点型数据判断相等不能直接使用==,做减法,取绝对值和极小值对比,double用1E-6对比5. 避免同一个局部变量在前后表达不同的含义6. 不要在单个的表达式中对相同的变量赋值超过一次
Method principle:
    1. Short
    2. Single, a way to do only one thing
    3. Single Abstract Hierarchy principle
    4. Separation of command and query responsibilities
Rules:
    1. Do not use the method's arguments as work variables/temporary variables unless specifically required (define a temporary variable operation in the method)
    2. Call a static method using the class name instead of using an instance or expression to invoke the
Suggestions
    1. It should be specified whether the legality check of interface method parameters is the responsibility of the caller or the interface method itself.
    2. Methods of using variable quantity parameters with caution
    3. The number of parameters of docking method should not be excessive
Package, class, interface principles:
    1. The design of classes and interfaces should follow the principles of object-oriented solid design
    2. The design of the class should follow the Dimitri law
    3. Class should be designed to follow the Tell,don ' t ask principle
Rules
    1. In addition to the global variables that are provided for external use, you should try to avoid directly accessing the class member variables externally
    2. Avoid reusing names between unrelated variables or unrelated concepts to avoid hiding (hide), masking (shadow), and shadowing (obscure)
    3. Do not invoke methods that might be overridden by a quilt class in the parent class's construction method
    4. When you overwrite the Equals method, you should also overwrite the Hashcode method
      1. Same run, same object if the information in the Equals method does not change, multiple calls to the Hashcode return value must be the same
      2. Both objects call the Equals method, and their Hashcode method must return the same value
      3. The two objects call the Equals method differently, they call the Hashcode method and do not require a different return value
Exception principle:

Use the exception mechanism only for cases where the real exception is true

Rules
    1. Using a checked exception for recoverable cases, using runtime exceptions for programming errors (Runtime exception)
    2. Do not ignore exceptions
    3. Method comments and instructions in the document to include the thrown exception
    4. The exception that the method throws should correspond to its own level of abstraction
    5. Do not use return, break, or continue in the finally block to cause the finally block to end abnormally
    6. Do not directly capture the base class of the exception being inspected exception
Suggestions
    1. Encapsulation of a large number of types of exceptions thrown by third-party APIs
    2. A method should not throw too many types of exceptions
Language characteristics Rules
    1. Operations and expressions do not write complex expressions, encapsulate hard-to-understand statements into methods, and annotate them with method names
    2. Operation should avoid overflow BigInteger
Suggestions

The operation uses parentheses to define the precedence of the operation

Control statements
    1. Place a break statement in each case, and default in the switch statement
    2. Replace traditional for loop with For-each
Generic type

Using Generics in collections

Other

The new code does not use a method that has been labeled @deprecated

Testing Basic Concepts

Definition: A test is a process that contains a plan, preparation, and measurement activity that is intended to confirm
The characteristics of the system under test, and indicates the difference between requirements and implementation.

Objective

Defects found
Increase confidence in quality
Providing information to decision makers
Prevention of defects

Classification:
    • Do you need to do: static test, dynamic test
    • Test different stages: unit test, System test, integration test, acceptance test
    • Need to know the internal structure: black box test, white box test
Static test methods:
1. 代码检查工具检查2. 走读3. 检视
Dynamic test method: Constructs the test instance, executes the program, analyzes the output result
1. 等价类划分2. 边界值分析3. 决策表分析4. 判定条件覆盖5. 条件组合覆盖
Unit Test

Test the module, call the unit test

Software Development Life cycle

Requirement Analysis--Architecture design--specification--coding--unit test--integration test--operating system test

Junit
Unit Test Framework

Test interface
TestCase Test Set multiple tests
TestSuite test Cases, suites have only one

Java Programming Specification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.