Backend Development 10 min read

14 Java Backend Code‑Optimization Tips from a Senior Architect

This article presents fourteen practical Java backend optimization techniques—including configuration‑file management, Lombok’s @RequiredArgsConstructor, modular code design, exception handling, reducing DB queries, avoiding null returns, minimizing if‑else, slimming controllers, IDE shortcuts, source‑code reading, design patterns, and efficient collections—each illustrated with clear code examples.

Top Architect
Top Architect
Top Architect
14 Java Backend Code‑Optimization Tips from a Senior Architect

In this guide, a senior architect shares fourteen concise tips to improve Java backend code quality and maintainability.

1. Define configuration file information

Store common variables in a yml file instead of hard‑coding magic numbers, then bind them using @ConfigurationProperties rather than @Value .

@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
    private String name;
    private String website;
    private String qq;
    private String phoneNumber;
}

@RestController
@RequiredArgsConstructor
public class PropertyController {
    private final DeveloperProperty developerProperty;
    @GetMapping("/property")
    public Object index() {
        return developerProperty.getName();
    }
}

2. Use @RequiredArgsConstructor instead of @Autowired

Lombok’s @RequiredArgsConstructor generates a constructor for final fields, enabling constructor injection which is preferred by Spring.

3. Code modularization

Keep each method under 50 lines, split responsibilities, and reuse modules across projects.

4. Throw exceptions instead of returning error codes

Prefer throwing meaningful exceptions to signal failure rather than returning ambiguous status values.

5. Reduce unnecessary database queries

Avoid extra SELECT statements; perform checks using existing data when possible.

6. Do not return null

Returning null leads to NPEs; use Optional or empty collections instead.

7. Minimize if‑else chains

Replace long if‑else blocks with strategy patterns or polymorphism.

8. Reduce controller business logic

Move core logic to service layers, keeping controllers thin and readable.

9. Leverage IDE (IntelliJ IDEA) suggestions

Use IDEA’s quick‑fixes to replace boilerplate code with lambda expressions or other modern constructs.

10. Read source code

Study high‑quality open‑source projects (stars > 1000) to learn design ideas and advanced APIs.

11. Apply design patterns

Incorporate the 23 classic GoF patterns to make code more robust and extensible.

12. Embrace new knowledge

Continuously explore emerging technologies and apply them in side projects.

13. Master basic problems

Examples include efficient Map traversal and safe Optional usage:

HashMap
map = new HashMap<>();
map.put("name", "du");
for (Map.Entry
entry : map.entrySet()) {
    // process entry
}

public List
getChild(String pid) {
    if (V.isEmpty(pid)) {
        pid = BasicDic.TEMPORARY_DIRECTORY_ROOT;
    }
    CatalogueTreeNode node = treeNodeMap.get(pid);
    return Optional.ofNullable(node)
                   .map(CatalogueTreeNode::getChild)
                   .orElse(Collections.emptyList());
}

14. Use appropriate collections for existence checks

Prefer HashSet over ArrayList when checking element existence to achieve O(1) lookup.

HashSet
set = new HashSet<>();
int index = hash(a);
return getNode(index) != null; // O(1) check

Adopting these habits gradually transforms a “code swamp” into clean, maintainable, and robust backend systems.

design patternsJavaBackend DevelopmentSpringcode optimizationBest PracticesLombok
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.