4

When it is not possible to use null object, what is the best practice to replace conditional operator null-checks boilerplate like this:

public String getEmployeeName() {
    return employee == null ? null : employee.getName();
}

Is there something like below in Java 8 or any utility library?

public String getEmployeeName() {
    return nullable(employee, employee -> employee.getName());
}

private <T, R> R nullable(T nullable, Function<T, R> doIfNotNull) {
    return nullable == null ? null : doIfNotNull.apply(nullable);
}
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Daniel Hári
  • 7,254
  • 5
  • 39
  • 54
  • 5
    I find `return employee == null ? null : employee.getName();` to be the most readable, so perhaps it is the better solution instead of just making your code overly complex. It gets the job done, and there's nothing wrong with it - so might as well use it. That's what the conditional operator is for. – Parker Hoyes Dec 05 '15 at 04:31
  • I also think this is the most clear and simple solution, please create an answer of this, and I will accept it. – Daniel Hári Dec 05 '15 at 16:42

3 Answers3

6

I find return employee == null ? null : employee.getName(); to be the most readable, so perhaps it is the better solution instead of just making your code overly complex. It gets the job done, and there's nothing wrong with it - so might as well use it. That's what the conditional operator is for.

Often when trying to decide which programming pattern to use, it is best to use the pattern that is the most readable and the easiest for future maintainers of your code to understand.

Parker Hoyes
  • 2,118
  • 1
  • 23
  • 38
4

You could refactor your code to this:

public String getEmployeeName() {
    return Optional.ofNullable(employee).map(Employee::getName).orElse(null);
}

ofNullable creates an Optional value out of the given employee: if it is null, the empty Optional is returned; otherwise an Optional containing the employee is returned. Then, this Optional is mapped to the name of the employee using map, which returns a new Optional with the given mapper applied if the Optional is non empty and an empty Optional if the Optional is empty. Finally, orElse returns the name of the employee or null is the Optional is empty.

Having said that, I don't see any added value in having this code over the null-check and the conditional operator: it will likely have a better performance and may also be easier to read.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • This one I looking for, thanks, but I think it is more readable and simpler to just leave the conditional operator as it was originally. – Daniel Hári Dec 05 '15 at 16:44
-1

Java 8 has the new Optional class, e.g.

private Optional<Employee> employee = Optional.empty();

public Optional<Employee> getEmployee() {
    return this.employee;
}
public void setEmployee(Employee employee) {
    this.employee = Optional.of(employee); // null not allowed
}
public void removeEmployee() {
    this.employee = Optional.empty();
}

The employee will never be null, but may be "empty".

The getEmployeeName() method can then be implemented in two ways:

// Returning Optional (never null)
public Optional<String> getEmployeeName() {
    return this.employee.map(Employee::getName);
}

// Standard getter (may return null)
public String getEmployeeName() {
    return this.employee.map(Employee::getName).orElse(null);
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Yes, thats rigth. But I mean situations when you are not able to refactor the source of the null value. – Daniel Hári Dec 05 '15 at 05:50
  • The problem with `Optional` is that it can't guarantee that its input is non-null. Here's an example, http://ideone.com/8b4w6A that shows that feeding the `Optional.of` function a null value will generate a null-pointer runtime error, just like without using `Optional`. – Lily Mara Dec 05 '15 at 11:50
  • 1
    @NateMara If the value is *allowed* to be null at that point, use [`Optional.ofNullable(x)`](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#ofNullable-T-). If value is *not* allowed to be null, then you're supposed to get the NPE. – Andreas Dec 05 '15 at 15:39
  • 2
    This is exactly the sort of "Optional abuse" that Stuart Marks discusses here: http://stackoverflow.com/questions/23454952/uses-for-java8-optional/27071576#27071576. – Brian Goetz Dec 05 '15 at 18:54