Difference between revisions of "Naming convention"
From DarkWiki
(→Getters and setters) |
(→Finders & filters) |
||
| Line 39: | Line 39: | ||
** Can be called multiple times, and in any order | ** Can be called multiple times, and in any order | ||
** Returns a consistent, single, non-null collection result (or an empty Optional) - ''never'' null | ** Returns a consistent, single, non-null collection result (or an empty Optional) - ''never'' null | ||
| + | |||
| + | <syntaxhighlight lang="java"> | ||
| + | public List<String> findNames() { | ||
| + | return Collections.emptyList(); | ||
| + | } | ||
| + | |||
| + | public Optional<Person> findById(String personId) { | ||
| + | return Optional.empty(); | ||
| + | } | ||
| + | </syntaxhighlight> | ||
===Factories=== | ===Factories=== | ||
Revision as of 13:59, 8 February 2020
Contents
Java naming convention
Getters and setters
- Getters (get, is)
- Takes no parameters
- Does not alter the object in any way; it is constant
- Can be called multiple times, and in any order
- Returns a consistent single result
public String getName() {
return this.name;
}
public boolean isAdult() {
return true;
}
- Setters (set)
- Takes one parameter
- May or may not alter the object
- Only affects the object that has the setter; it does not persist anywhere else
- Can be called multiple times, and in any order
public void setName(String name) {
this.name = name;
}
Finders & filters
- Finders (find)
- Takes zero or more arguments (which act as filter or sorting controls)
- Does not alter the object in any way; it is constant
- Can be called multiple times, and in any order
- Returns a consistent, single, non-null collection result (or an empty Optional) - never null
public List<String> findNames() {
return Collections.emptyList();
}
public Optional<Person> findById(String personId) {
return Optional.empty();
}
Factories
- Factories
- Responsible for creating objects - not persisting them!
- Are stateless
- Factory classes
- Conform to [ObjectType]Factory naming convention
- Factory methods
- Conform to create[ObjectType] naming convention
Other
- Populator methods (imbue)
- Only modify the object(s) passed as arguments
- Does not affect the object's state
Persistance
- Repositories
- Conform to [ObjectType]Repository naming convention
- Methods such as save*, load*, find*, read*, write*, delete*