Java Backend Development Best Practices and Tips
This article presents a collection of practical Java backend development guidelines, covering configuration management with @ConfigurationProperties, constructor injection via @RequiredArgsConstructor, code modularization, exception handling, database query reduction, null avoidance, strategic use of design patterns, IDE assistance, source code reading, and fundamental coding techniques such as map traversal and collection choice.
1. Define configuration file information – Use @ConfigurationProperties instead of @Value to bind YAML properties to a POJO. Example:
@Data
// specify prefix
@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 {
private final DeveloperProperty developerProperty;
@GetMapping("/property")
public Object index() {
return developerProperty.getName();
}
}2. Replace @Autowired with @RequiredArgsConstructor – Prefer constructor injection for beans; Lombok generates the required constructor automatically.
3. Code modularization – Keep methods under 50 lines, split responsibilities, and apply the single‑responsibility principle.
4. Throw exceptions instead of returning error codes – Improves readability and centralizes error handling.
5. Reduce unnecessary database queries – Avoid extra look‑ups; perform checks in a single query when possible.
6. Do not return null – Use Optional or throw exceptions to prevent NullPointerExceptions.
7. Limit if‑else chains – Replace long conditional blocks with the Strategy pattern where appropriate.
8. Keep controller thin – Move business logic to the service layer for better maintainability.
9. Leverage IntelliJ IDEA – Use IDE inspections and quick‑fix suggestions, such as converting anonymous classes to lambda expressions.
10. Read source code – Study high‑quality open‑source projects (e.g., GitHub repos with >1000 stars) to learn design ideas and advanced APIs.
11. Apply design patterns – Familiarize yourself with the 23 classic patterns and incorporate them where they improve code structure.
12. Embrace new knowledge – Continuously explore unfamiliar technologies and practice with demos beyond routine CRUD tasks.
13. Basic issues
Map traversal examples:
HashMap<String, String> map = new HashMap<>();
map.put("name", "du");
for (String key : map.keySet()) {
String value = map.get(key);
}
// Recommended modern style
for (Map.Entry<String, String> entry : map.entrySet()) {
// use entry.getKey() and entry.getValue()
}Optional null‑check example:
public List<CatalogueTreeNode> 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());
}Recursion tip: for large data sets, pass reusable objects as method parameters instead of creating new ones inside the recursive call.
14. Check element existence efficiently – Prefer HashSet over List for O(1) look‑ups.
ArrayList<String> list = new ArrayList<>();
// O(n) check
for (int i = 0; i < list.size(); i++) {
if ("a".equals(list.get(i))) return i;
}
HashSet<String> set = new HashSet<>();
// O(1) check
int index = hash(a);
return getNode(index) != null;Overall, these practices aim to produce clean, maintainable, and high‑performance 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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
