Skip to content

Commit 2960a46

Browse files
ordepdevshekhargulati
authored andcommitted
Remove Optional<T> from class properties (shekhargulati#21)
Optional<T> should *never* be used as class property type. If the property is nullable, then a getter that returns Optional<T> should be used.
1 parent 57551c7 commit 2960a46

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

05-optionals.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,18 @@ import java.util.Optional;
145145

146146
public class Task {
147147
private final String title;
148-
private final Optional<User> assignedTo;
148+
private final User assignedTo;
149149
private final String id;
150150

151151
public Task(String id, String title) {
152152
this.id = id;
153153
this.title = title;
154-
assignedTo = Optional.empty();
155154
}
156155

157156
public Task(String id, String title, User assignedTo) {
158157
this.id = id;
159158
this.title = title;
160-
this.assignedTo = Optional.ofNullable(assignedTo);
159+
this.assignedTo = assignedTo;
161160
}
162161

163162
public String getId() {
@@ -178,16 +177,15 @@ import java.util.Optional;
178177
public class User {
179178

180179
private final String username;
181-
private final Optional<String> address;
180+
private final String address;
182181

183182
public User(String username) {
184183
this.username = username;
185-
this.address = Optional.empty();
186184
}
187185

188186
public User(String username, String address) {
189187
this.username = username;
190-
this.address = Optional.ofNullable(address);
188+
this.address = address;
191189
}
192190

193191
public String getUsername() {
@@ -200,7 +198,7 @@ public class User {
200198
}
201199
```
202200

203-
Use of the `Optional` data type in the data model makes it explicit that `Task` refers to an `Optional<User>` and `User` has an `Optional<String>*` username. Now whoever tries to work with `assignedTo` User would know that it might not be present and they can handle it in a declarative way. We will talk about `Optional.empty` and `Optional.of` methods in the next section.
201+
Since `Task.assignedTo` and `User.username` are nullable fields, each getter should return `Optional<User>` and `Optional<String>`. Now whoever tries to work with `assignedTo` User would know that it might not be present and they can handle it in a declarative way. We will talk about `Optional.empty` and `Optional.of` methods in the next section.
204202

205203
## Working with creational methods in the java.util.Optional API
206204

@@ -231,7 +229,7 @@ public class TaskRepository {
231229

232230
## Using Optional values
233231

234-
Optional can be thought of as a Stream with one element. It has methods similar to Stream API like `map`, `ilter`, and `flatMap`, which we can use to work with values contained in the `Optional`.
232+
Optional can be thought of as a Stream with one element. It has methods similar to Stream API like `map`, `filter`, and `flatMap`, which we can use to work with values contained in the `Optional`.
235233

236234
### Getting title for a Task
237235

0 commit comments

Comments
 (0)