digital-garden/dev/java/Возвращайте пустую коллекцию вместо null.md
Struchkov Mark 7b5734d1ac
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Возвращайте пустую коллекцию вместо null.md
2024-09-07 01:12:21 +03:00

21 lines
783 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
aliases:
tags:
- maturity/🌱
date:
- - 2024-09-07
zero-link:
- "[[../../meta/zero/00 Java разработка|00 Java разработка]]"
parents:
linked:
---
Если ваша программа может вернуть коллекцию, которая не содержит никаких значений, убедитесь, что возвращается пустая коллекция, а не `null`. Это сэкономит вам время на различные проверки и избавит от многих потенциальных ошибок.
```java
public List<Event> getAllEventByUserId(int userId) {
if (userId == 0) {
return Collections.emptyList();
}
return eventRepository.findAllByUserId(userId);
}
```