Naming conventions in Java

Good naming can reflect the character, meaning, or purpose of the code, allowing the reader to quickly clarify the context of the program based on the meaning of the name. In Java, there are three common naming forms: UpperCamelCase (uppercase), lowerCamelCase (lower case), and UPPERCAMELUNSER_SCORE (all uppercases and underscores). By convention, classes are generally named with a big camel, methods and local variables are named with a small camel, and uppercase underline is usually used in constants and enumerations.

Package name

The package names are all lowercase, and there is only one or more natural semantic English words between the dot delimiters, or more words are naturally connected together (for example, springframework and deepspace do not need to use any division). The package name must be singular. If the class name has a plural, it can be plural.

Package name can be divided into the following four parts [prefix] [initiator name] [project name] [module name]. Common prefixes can be classified as follows:

Class name

In addition to nouns and noun phrases, interface names can also use adjectives or adjective phrases, such as Cloneable, Callable, etc., to indicate that the class that implements the interface has certain functions or capabilities. For a Test class, it starts with the class it is testing and ends with Test, such as HashMapTest.

It is also possible to use all caps for abbreviations of specific nouns, such as XMLHttpRequest, but I believe that abbreviations should be capitalized up to three letters, or more. There is no standard such as Fastjson in Alibaba, which uses JSONObject as the name of the class, and Google uses JsonObjectRequest. For this special abbreviation, the principle is unified.

methods

Method names are small humps, with the first letter in lower case and each subsequent word capitalized. Unlike class names, method names are usually verbs or phrasal verbs, which together with parameters or parameter names form verb-object phrases, i.e., verbs + nouns. A good function name usually tells you directly what the function does.

A method that returns true or false values

Note: pre-prefix, suf-suffix, alo-alone are used alone

A method used to check

An on-demand approach

Asynchronous correlation method

The callback method

Methods to manipulate the life cycle of an object

Methods related to collection operations

Methods related to data

Verbs that come in pairs

Variable & constant name

Variable naming

Variables are variables whose values can be changed during program execution, including member variables and local variables. When a variable name consists of multiple words, the first letter of the first word is lowercase and the first letter of the following words is uppercase, commonly known as camel naming (also known as camel naming), such as computedValues, index, variable naming, as short as possible to clearly express the function of the variable, the name reflects the specific business meaning.

Variable names should not begin with an underscore or dollar sign, although this is syntactically permissible. Variable names should be short and descriptive. Variable names should be chosen to be easy to remember, that is, to indicate their purpose. Avoid single-character variable names unless they are one-time temporary variables. Pojo Boolean variables should not be prefixed with is(all Boolean fields in the database should be prefixed with IS_).

Constant named

Constants are named CONSTANT_CASE, usually in all capitals (except when used as method parameters), and words are separated by underscores. So what is a constant?

Constants are values that remain constant in scope and are typically modified with final. There are three types of constants: global (public static final), internal (private static final), and local (constant within a method or parameter). Local constants are special, and are usually named with a small hump.

General naming rules

Try not to use pinyin; Avoid mixing pinyin and English. For some general expressions or difficult to describe in English pinyin can be used, once adopted pinyin must not be mixed with English. Example: BeiJing, HangZhou Example: validateCanShu

  • Do not use special characters in the naming process, except constants.
  • Try not tojdkOr existing class names in the framework can not be named using Java keywords.
  • Prepositions such as for(can be replaced by homophone 4), to(can be replaced by homophone 2), from, with, of, etc. For example, the class name is User4RedisDO, and the method name is getUserInfoFromRedis and convertJson2Map.

Code annotations

Principles of annotation

Good naming makes code readable, and code naming is often strictly restricted. Unlike annotations, programmers are often free to do whatever they want, but that doesn’t mean they can do whatever they want. Elegant annotations usually meet three elements.

Nothing is strange unannotated code is very unfriendly to the reader, even if the code is written clean, the reader will be at least psychologically resistant, and there is often a lot of complex logic in the code, so be sure to write annotations, not only to record the logic of the code, but also to clarify the logic of the change.

From a code maintenance perspective, comments in code must be the best of the best. Reasonable and clear naming makes code easy to understand. Code with simple logic and naming conventions that clearly express what the code does does not need annotations. Overuse of annotations adds extra burden, not to mention that most of them are nonsense.

GetMessageById (id);

Advance with the time annotations should change as the code changes, and the information expressed in the annotations should be exactly the same as in the code. It is common to modify code without modifying annotations.

Annotation format

Annotations can be broadly divided into two types, javadoc annotations and simple annotations. Javadoc annotations can be generated to provide effective support for external users through the JavaAPI. Javadoc annotations are automatically generated when using development tools such as IDEA or Eclipse. Custom annotation templates are also supported, requiring only the interpretation of corresponding fields. Students involved in the same project should try to set up the same annotation template.

Package annotations

Package annotations are usually special in work. They can be used to quickly know what functions the code under the current package is used to implement. It is strongly recommended to use package annotations in work, especially for some complicated packages.

/** * Landing also quality testing * 1. What problems are used to solve * Perform performance testing on the landing page of advertisers, simulating different systems, such as Android, IOS, etc.; Simulate different networks: 2G, 3G, 4G, wifi, etc. * 2. How to achieve * Based on Chrome browser, use Chrome driver to drive the browser, set the corresponding network, OS parameters, and obtain the browser return results. * note: Network environment configuration information {@ link cn.mycookies.landingpagecheck.meta.Net WorkSpeedEnum} is currently using conventional speed, Can be adjusted according to actual condition * @ author cruder * * @ the time 2019/12/7 thus afternoon/package cn mycookies. Landingpagecheck;Copy the code

Class notes

In Javadoc annotations, each class must have an annotation.

Attribute annotations

Each attribute must be preceded by an attribute comment, usually in one of two forms, whichever you like, but be consistent throughout the project.

Methods the annotation

Each method must be preceded by a method comment that describes each parameter in the method, as well as the return value.

Constructor comment

Each constructor must be preceded by a comment. The comment template looks like this:

Matters needing attention

While simple annotations often require engineer byte definitions, there are a few things to note when using annotations:

Each attribute value of an enumerated class is annotated. Enumerations can be thought of as constants, usually unchanged, and often referred to in multiple places. Modifying and adding attributes to an enumeration often has a big impact.

Keep typography clean and don’t use end-of-line comments. Double slashes and asterisks are separated by 1 space.


Writing is not easy, if the article is helpful to you, can you leave footprints to leave a praise ~