Java Naming Conventions, Code Comments, and Advanced Language Features
This article explores the trade‑offs between efficiency, readability and maintainability in software development, presents common naming conventions and best‑practice guidelines for Java, discusses proper use of comments, and introduces advanced language constructs such as records, sealed classes and the upcoming Valhalla project.
Software development constantly balances efficiency, readability, and maintainability, often described as an impossible triangle; low‑level languages like C provide performance but lack readability, while high‑level languages like Java excel in readability but may sacrifice speed.
Building a language ecosystem that mitigates each language's shortcomings is an ongoing evolution. Using Java as an example, the article enumerates essential knowledge points and practical skills that are frequently overlooked.
Basic Naming Conventions
Snake case (e.g., my_system )
Camel case – upper (Pascal) and lower (e.g., MySystem , mySystem )
Hungarian notation (e.g., nLength , g_cch )
Pascal case (identical to upper camel case, e.g., MySystem )
Spinal case (e.g., my-system )
Free/Studly caps (e.g., mySYSTEM , MYSystem )
Among these, camel case and snake case are most popular due to their balance of readability and ease of writing.
Naming Dictionaries
Management: Bootstrap , Processor , Manager , Factory , Service , Task
Propagation: Context , Propagator
Callback: Handler , Callback , Listener , Aware
Monitoring: Metric , Tracker
Memory management: Allocator , Pool
Filter/Detection: Pipeline , Filter , Interceptor
Structure: Cache , Buffer , Option , Param , Iterator
Design‑pattern names: Strategy , Adapter , Command , Proxy
Parsing: Converter , Parser , Formatter
Network: Packet , Encoder , Decoder
CRUD: Controller , Service , Repository
Utility: Util , Helper
Other: Mode , Invoker , Future , Promise , Constants
Practical Naming Rules for Java
Project and package names: all lower‑case.
Class names: UpperCamelCase.
Variable and method names: lowerCamelCase; constants: UPPER_SNAKE_CASE.
Common pitfalls include cryptic abbreviations, meaningless single‑letter variables, long pinyin names, mixed symbols, and inconsistent casing.
Variable Type Decisions
Prefer wrapper types for member fields to allow null as an extra state and avoid NPEs; use primitive types for local variables where performance matters.
Boolean Getter Naming
public boolean is
();Because different JSON libraries treat isXxx differently, it is recommended not to start boolean fields with is to avoid serialization inconsistencies.
Case Sensitivity and Jar Conflicts
File systems are case‑insensitive, which can cause Git to miss path changes; keep package names lower‑case. Jar version conflicts arise from multiple versions of the same library or duplicate classes across jars; use Maven dependency management or tools like jarjar to relocate packages.
Balancing Readability and Resource Consumption
Using @JsonProperty to rename fields during serialization improves readability without affecting internal logic:
public class SkuKey implements Serializable {
@JsonProperty(value = "sn")
private Long stationNo;
@JsonProperty(value = "si")
private Long skuId;
// getters/setters omitted
}Choosing Between Class Objects and Maps for API Parameters
Maps are flexible but obscure structure; class‑based DTOs provide clear contracts and better maintainability.
Comments
Good comments act as documentation, following a layered approach: system, package, class, method, block, and line. Over‑commenting, ambiguous comments, signature‑style comments, large blocks of text, non‑local comments, and commented‑out code are discouraged.
Annotations (e.g., Swagger) can replace many comments by generating API documentation automatically.
Class Definitions
Constant definitions can use enums or static final fields:
public enum Color { RED, GREEN, BLUE; } public class MyClass { public static final int MAX_VALUE = 100; }Utility classes should be abstract with static methods to avoid instantiation:
public abstract class ObjectHelper {
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}JavaBeans can be written manually or generated with Lombok:
public class Person {
private String name;
private int age;
// getters and setters
} import lombok.Data;
@NoArgsConstructor
@Data
@Accessors(chain = true)
public class Person {
private String name;
private int age;
}Immutable classes are declared final or as record (Java 17):
public final class String implements Serializable, Comparable
, CharSequence { } public record Person(String name, int age) {
public Person { if (name == null) throw new IllegalArgumentException(); }
public static Person of(String name) { return new Person(name, 18); }
}Sealed classes (Java 17) restrict subclassing:
sealed class SealedClass permits SubClass1, SubClass2 { }
class SubClass1 extends SealedClass { }
class SubClass2 extends SealedClass { }Method definitions include constructors, overrides, overloads, and lambda expressions. Examples of constructor, method overriding, overloading, and lambda usage are provided.
Object Definitions
Singletons can be implemented with enums:
public enum Singleton { INSTANCE; public void someMethod() { /* ... */ } }Immutable objects store state in final fields; mutable objects should be avoided when possible.
Tuples (pair, triplet, generic) are not part of the JDK but can be implemented manually or via libraries such as Guava or Vavr.
Valhalla Project
Valhalla aims to introduce value types to the JVM, offering a middle ground between primitive and reference types, reducing object header overhead and improving memory layout, though it is still under development.
The article concludes that mastering naming, commenting, and structural design improves code readability, maintainability, and scalability across Java projects.
JD Retail Technology
Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.