Best Practices for Java Backend Development: Configuration, Lombok, Modular Code, Exception Handling, and More
This article shares a collection of practical Java backend development habits, covering configuration with @ConfigurationProperties, Lombok's @RequiredArgsConstructor, code modularization, exception throwing, reducing DB queries, avoiding null returns, simplifying if‑else, keeping controllers thin, IDE tips, source code reading, design patterns, and fundamental coding techniques.
Sharing personal coding habits for Java backend development, the article starts with defining configuration file information by moving variables to a YAML file and using @ConfigurationProperties instead of @Value. An example entity class and a controller that injects the bean are provided.
It then recommends replacing @Autowired with Lombok's @RequiredArgsConstructor for constructor injection, showing the compiled result of the earlier code.
Code modularization is emphasized: methods should stay under 50 lines, each handling a single responsibility, making them reusable across features.
For error handling, the article advises throwing exceptions rather than returning error codes, illustrating a bad example and a good example with screenshots.
To improve performance, it suggests minimizing unnecessary database queries, giving a before‑and‑after example of checking a record before deletion.
Returning null is discouraged; a proper example demonstrates returning an empty collection or using Optional instead.
Excessive if‑else chains should be avoided; the strategy pattern is proposed as an alternative.
Business logic should be moved from controllers to service layers for better maintainability, with a bad and good controller example.
The article highlights IntelliJ IDEA’s code inspection and quick‑fix suggestions, such as converting code to lambda expressions.
Reading source code of high‑star open‑source projects on GitHub is encouraged to learn design ideas and advanced APIs, which also helps in interviews.
Applying the 23 design patterns is recommended to produce clean, elegant code.
Continuous learning beyond routine CRUD work is urged, especially for junior developers, by experimenting with more challenging demos after work.
Fundamental topics are covered: proper HashMap iteration, using Optional for null‑checks, and recursion tips (avoid object allocation inside deep recursion).
Commenting guidelines stress clear documentation for classes, interfaces, methods, and complex logic.
When checking element existence, using a HashSet is preferred over a List due to O(1) lookup versus O(n); code snippets and complexity analysis are provided.
Overall, the article compiles a checklist of best practices to write cleaner, more maintainable, and efficient Java backend code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
