Java programming specifications worth having

A naming and annotation

1.1 Naming

  • The key to naming is to accurately express meaning. In the case of sufficient expression of its meaning, the name is of course as short as possible.
  • Temporary variables inside methods are recommended to be named short, and class names are recommended to be long. If you are familiar with them, use abbreviations.
  • The naming should be readable and searchable, and the naming process should conform to the unified specifications of the project. It is best to be able to know the meaning by name.
  • Interface naming method 1: The interface is prefixed with "I", such as IUserService, and the corresponding implementation class is named UserService.
  • Interface naming method 2: No prefix, such as UserService, and the corresponding implementation class with the suffix "Impl", such as UserServiceImpl.
  • There are also two ways to name abstract classes, one is with the prefix "Abstract", the other is without the prefix "Abstract", and the prefix "Abstract" is recommended.

1.2 Notes

  • The purpose of comments is to make the code easier to understand, as long as it meets this requirement, you can write it.
  • It’s better to explain: what, why, and how.
  • For some complex classes and interfaces, we may also need to specify "how to use".
  • Classes and functions must be commented, as detailed as possible, comments inside functions can be written less.
  • The comment itself has a certain maintenance cost. Generally, it relies on good naming and refined functions, explanatory variables, and summary comments to make the code easy to read.

Two code style

  • The number of lines of code of the function should not exceed the size of one screen, such as 50 lines.
  • It is best not to exceed the width of the IDE display for a line of code. Try to keep the code on one line and split it into two lines to affect reading.
  • Use blank lines to separate code blocks to make the boundaries between the codes of different modules clearer and clearer.
  • For clear code level and elegant code, it is recommended to use two-space indentation code.
  • The braces can start on a new line or on the same line as the previous statement. Get used to the same line as the previous statement.
  • In the Google Java programming specification, dependent classes are arranged in alphabetical order from smallest to largest. Write member variables first and then functions in the class.
  • Between member variables or functions, write static member variables or functions first, then write ordinary variables or functions, and arrange them in order according to the scope.

Three programming skills

  • Have modularity and abstract thinking, and be good at distilling large pieces of complex logic into classes or functions.
  • If the function has too many parameters, split it into multiple functions according to the principle of single responsibility.
  • If the function has too many parameters and cannot be split into multiple functions, encapsulate the parameters as objects.
  • Functions should not use parameters to control the code logic, and can be split into multiple functions in accordance with the principle of single responsibility and interface isolation.
  • By removing redundant if or else statements, part of the nested logic is abstracted into functions and the nesting level is reduced.
  • Use the continue, break, and return keywords to exit nesting early and adjust the execution order to reduce nesting.
  • Replace magic numbers with literal constants; use explanatory variables to explain complex expressions.

Guess you like

Origin blog.csdn.net/jack1liu/article/details/108293542