14 Practical Code Optimization Tips for Java Backend Development
This article presents fourteen actionable Java backend code‑optimization techniques—including configuration‑file management, Lombok’s @RequiredArgsConstructor, modularization, exception handling, database query reduction, null‑avoidance, strategic use of design patterns, IDE shortcuts, and efficient collection handling—to help developers write cleaner, more maintainable, and higher‑performance code.
When it comes to code optimization, many start with theory and architecture, but the most effective improvements often stem from solid coding habits; this article shares fourteen concise tips that turn optimization into a routine practice.
1. Define configuration file information
Store variables in a yml file instead of scattering magic numbers throughout the code, allowing a single change to affect the whole project.
Example using @ConfigurationProperties instead of @Value :
@Data
// 指定前缀
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
private String name;
private String website;
private String qq;
private String phoneNumber;
}Inject the bean where needed:
@RestController
@RequiredArgsConstructor
public class PropertyController {
final DeveloperProperty developerProperty;
@GetMapping("/property")
public Object index() {
return developerProperty.getName();
}
}2. Use @RequiredArgsConstructor instead of @Autowired
Spring recommends constructor injection; Lombok’s @RequiredArgsConstructor generates the required constructor automatically.
3. Code modularization
Follow the Alibaba Java Development Manual: keep methods under 50 lines, split responsibilities, and reuse modular code.
4. Throw exceptions instead of returning error codes
Reduce scattered return statements; use exceptions to handle error conditions uniformly.
5. Minimize unnecessary database queries
Avoid redundant SELECT operations; fetch only what is needed before performing business logic.
6. Avoid returning null
Prevent NullPointerExceptions by returning empty collections or Optional values.
7. Reduce excessive if‑else chains
Replace long if‑else blocks with strategy patterns or polymorphism.
8. Keep controller logic thin
Move business logic to the service layer for better maintainability and readability.
9. Leverage IDE (IntelliJ IDEA) suggestions
Use IDEA’s quick‑fix recommendations, such as replacing anonymous classes with lambda expressions.
10. Read source code
Study high‑quality open‑source projects (stars > 1000) to learn design ideas and advanced APIs.
11. Apply design patterns
Familiarize yourself with the 23 classic design patterns and incorporate them where appropriate.
12. Embrace new knowledge
Continuously explore unfamiliar technologies and build demo projects to deepen understanding.
13. Fundamental issues
Map iteration: prefer for (Map.Entry<K,V> e : map.entrySet()) over keySet() or forEach when both key and value are needed.
Optional null‑check: use Optional.ofNullable(...).map(...).orElse(...) to avoid explicit null checks.
Recursion: pass reusable objects as parameters to reduce object creation in deep recursion.
Comments: document classes, interfaces, methods, and complex logic clearly.
14. Use appropriate collections for existence checks
Prefer HashSet over List when checking element existence, achieving O(1) lookup instead of O(n).
ArrayList
list = new ArrayList<>();
// O(n) check
for (int i = 0; i < list.size(); i++) {
if ("a".equals(elementData[i])) {
return i;
}
} HashSet
set = new HashSet<>();
// O(1) check
int index = hash(a);
return getNode(index) != null;By adopting these practices, developers can write cleaner, more robust, and higher‑performance backend code.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.