Home > Java > javaTutorial > Item Adopt generally accepted naming conventions

Item Adopt generally accepted naming conventions

Mary-Kate Olsen
Release: 2024-11-01 09:36:30
Original
1154 people have browsed it

Item  Adote as convenções de nomenclatura geralmente aceitas

The Java language has a well-established set of naming conventions that help maintain code readability and organization. These conventions are documented in detail in the Java Language Specification (JLS) and can be classified into typographic and grammatical.

Typographic Conventions
Typographic naming conventions cover packages, classes, interfaces, methods, fields, and type variables. Following them helps keep the code consistent and avoids confusion, making it easier to use and maintain. Among the best practices:

  • Packages and modules: They must be hierarchical, separated by periods and composed of lowercase letters. Packages used outside the organization must start with the domain backwards, such as com.google or org.apache.

  • Classes and interfaces: They must have PascalCase names, such as ArrayList or FutureTask, preferring full names and avoiding abbreviations, unless widely accepted (HttpUrl instead of HTTPURL).

  • Methods and fields: Named in camelCase with the first lowercase letter, such as remove or getCapacity. Constant fields (final static) must be written in capital letters with words separated by underscores, for example, MAX_VALUE.

Local variables: Can use abbreviations and individual characters, such as i, sum, or index, especially for loops and temporary variables.

Type parameters: These are usually single letters, such as T for a generic type, E for the element type of a collection, and K and V for keys and values ​​of a map.

Grammar Conventions
Grammatical conventions vary depending on the type of identifier, with a focus on providing clarity to the element's function:

  • Classes: Use nouns or noun phrases (Thread, ArrayList), while utility classes are usually pluralized (Collections, Collectors).

  • Interfaces: Interface names can use nouns or adjectives ending in “able” or “ible”, such as Runnable or Comparable.

  • Methods: Action functions have names in verbs (append, drawImage). Boolean methods often start with is or has (isEnabled, hasNext).

  • Getters and setters: Methods that return values ​​have a name with a noun or begin with get, while methods that modify values ​​begin with set. Example: getAge and setAge.

  • Conversion methods: For type conversions, conventions such as toString and asList are used. Static factories are usually named with from, of, or getInstance.

Importance of Conventions
Respecting these conventions improves code readability and consistency, making teamwork and maintenance easier. Although typographic conventions are more rigid, grammatical conventions allow greater flexibility, allowing the programmer to adapt to the needs of the context and conventional usage.

The above is the detailed content of Item Adopt generally accepted naming conventions. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template