简体   繁体   English

避免空值检查条件运算符样板的最佳实践

[英]Best practice to avoid null-check conditional operator boilerplate

When it is not possible to use null object, what is the best practice to replace conditional operator null-checks boilerplate like this: 当无法使用null对象时,更换条件运算符null-checks样板的最佳做法是:

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

Is there something like below in Java 8 or any utility library? 在Java 8或任何实用程序库中是否有类似下面的内容?

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);
}

I find return employee == null ? null : employee.getName(); 我发现return employee == null ? null : employee.getName(); 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. 通常在尝试决定使用哪种编程模式时,最好使用最易读且最容易让代码的维护人员理解的模式。

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; ofNullable从给定的雇员中创建一个Optional值:如果为null ,则返回空的Optional ; otherwise an Optional containing the employee is returned. 否则Optional返回包含雇员。 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. 然后,该Optional被映射到使用雇员的姓名map ,它返回一个新的Optional施加给定的映射器,如果Optional为非空并且一个空Optional如果Optional为空。 Finally, orElse returns the name of the employee or null is the Optional is empty. 最后, orElse返回雇员的名称或nullOptional是空的。

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. 话虽如此,我认为将此代码放在null -check和条件运算符上没有任何附加价值:它可能会有更好的性能,也可能更容易阅读。

Java 8 has the new Optional class, eg Java 8具有新的Optional类,例如

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". employee永远不会为null ,但可能是“空的”。

The getEmployeeName() method can then be implemented in two ways: 然后可以通过两种方式实现getEmployeeName()方法:

// 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM