Fundamental Java Backend Development Practices and Tips
This article presents a collection of basic Java programming methods and best‑practice recommendations—ranging from using @ConfigurationProperties and @RequiredArgsConstructor to modularizing code, handling exceptions, reducing unnecessary database queries, avoiding null returns, and applying design patterns—to help developers write cleaner, more maintainable backend code.
1. Define configuration file information
Sometimes we put variables in yml files for unified management.
Use @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 this bean when using it.
@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 for beans. The following diagram shows the compiled result of the previous code.
RequiredArgsConstructoris provided by Lombok.
3. Code modularization
Alibaba Java Development Handbook suggests each method should not exceed 50 lines. Split interfaces and methods so each handles a single piece of logic, making reuse easier.
4. Throw exceptions instead of returning error codes
Avoid returning multiple error messages; use exceptions to keep code clean.
Bad example
Good example
5. Reduce unnecessary database queries
Avoid excessive DB access; for example, when deleting a service, first check if the record exists before performing extra queries.
Bad example
Good example
6. Do not return null
Returning null leads to unnecessary NullPointerExceptions; prefer Optional or proper defaults.
Bad example
Good example
7. Reduce excessive if‑else chains
Too many if‑else if statements can be replaced with the Strategy pattern.
8. Keep controller logic minimal
Business logic should reside in the service layer for better maintainability and readability.
Bad example
Good example
9. Leverage IDE features
IntelliJ IDEA provides code analysis and suggestions, such as converting anonymous classes to lambda expressions.
10. Read source code
Regularly read high‑quality open‑source projects (stars > 1000) to learn design ideas, advanced APIs, and improve interview performance.
11. Apply design patterns
There are 23 design patterns; using them makes code more standardized and elegant.
12. Embrace new knowledge
Junior developers should continuously learn beyond daily CRUD tasks, experimenting with more challenging concepts in personal projects.
13. Fundamental topics
Map iteration
HashMap<String, String> map = new HashMap<>();
map.put("name", "du");
for (String key : map.keySet()) {
String value = map.get(key);
}
map.forEach((k, v) -> {
});
// Recommended
for (Map.Entry<String, String> entry : map.entrySet()) {
}Optional null‑check
//获取子目录列表
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
When dealing with large data sets, avoid creating new objects inside recursive methods; pass objects as parameters instead.
Comments
Classes, interfaces, methods, annotations, and complex methods should have clear comments, often for the future self.
14. Use hash‑based collections for existence checks
Prefer HashSet over List for O(1) containment checks.
ArrayList<String> list = new ArrayList<>();
// Check if "a" is in list
for (int i = 0; i < list.size(); i++)
if ("a".equals(elementData[i]))
return i; HashSet<String> set = new HashSet<>();
// Check if "a" is in set
int index = hash(a);
return getNode(index) != null;Time complexity is O(1).
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.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.
