如何避免使用Java中的多个语句?

发布于 2025-01-21 10:57:50 字数 517 浏览 3 评论 0 原文

如何重构此方法?

public String getFirstOrLastNameOrBoth() {
    if (this.getFirstname() != null && this.getLastname() != null) {
        return this.getFirstname() + this.getLastname();
    } else if (this.getFirstname() != null && this.getLastname() == null){
        return this.getFirstname();
    } else if (this.getLastname() != null && this.getFirstname() == null){
        return this.getLastname();
    }
    return 0.0;
}

How to refactor this method?

public String getFirstOrLastNameOrBoth() {
    if (this.getFirstname() != null && this.getLastname() != null) {
        return this.getFirstname() + this.getLastname();
    } else if (this.getFirstname() != null && this.getLastname() == null){
        return this.getFirstname();
    } else if (this.getLastname() != null && this.getFirstname() == null){
        return this.getLastname();
    }
    return 0.0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

草莓酥 2025-01-28 10:57:50
    public String getFirstOrLastNameOrBoth() {
        return (getFirstname() == null ? "" : getFirstname()) 
             + (getLastname() == null ? "" : getLastname());
    }
    public String getFirstOrLastNameOrBoth() {
        return (getFirstname() == null ? "" : getFirstname()) 
             + (getLastname() == null ? "" : getLastname());
    }
晨曦÷微暖 2025-01-28 10:57:50

为了访问字段,无需在班级中调用Getter。改用字段名称。

您可以使用对象> objects 实用程序类的nearnonnnullse(t,t)“ rel =“ nofollow noreferrer”> noneNulloreLse() 静态方法。

return Objects.requireNonNullElse(firstName, "") +
       Objects.requireNonNullElse(lastName, "");

There's no need to invoke getter in the class in order to access a field. Use the field name instead.

Instead of null-checks, you can make use of noneNullOrElse() static method of the Objects utility class.

return Objects.requireNonNullElse(firstName, "") +
       Objects.requireNonNullElse(lastName, "");
倾城花音 2025-01-28 10:57:50
public String getFirstOrLastNameOrBoth() {
    if(this.getFirstname() == null && this.getLastname() == null) {
        return "0.0";
    }
    
    return (this.getFirstName() != null ? this.getFirstName() : "") +
            (this.getLastname() != null ? this.getLastname() : "");
}
public String getFirstOrLastNameOrBoth() {
    if(this.getFirstname() == null && this.getLastname() == null) {
        return "0.0";
    }
    
    return (this.getFirstName() != null ? this.getFirstName() : "") +
            (this.getLastname() != null ? this.getLastname() : "");
}
烈酒灼喉 2025-01-28 10:57:50
if (this.getFirstname() != null && this.getLastname() != null) {
   return this.getFirstname() + this.getLastname();
} else {
   return Optional.ofNullable(this.getFirstname()).orElseGet(() -> Optional.ofNullable(this.getLastname()).orElseGet(() -> "0.0"));
}
if (this.getFirstname() != null && this.getLastname() != null) {
   return this.getFirstname() + this.getLastname();
} else {
   return Optional.ofNullable(this.getFirstname()).orElseGet(() -> Optional.ofNullable(this.getLastname()).orElseGet(() -> "0.0"));
}
小梨窩很甜 2025-01-28 10:57:50
  1. 如果不需要,请勿使用此。
  2. 使用库方法与字符串一起使用。

strimutils.trimtoempty()来自Apache Commons org.apache.commons.lang3 可以在此处使用

    public String getFirstOrLastNameOrBoth() {
        return trimToEmpty(getFirstname()) + trimToEmpty(getLastname());
    }
  1. Don't use this if it is not necessary.
  2. Use library methods to work with strings.

StringUtils.trimToEmpty() from Apache Commons org.apache.commons.lang3 can be used here

    public String getFirstOrLastNameOrBoth() {
        return trimToEmpty(getFirstname()) + trimToEmpty(getLastname());
    }
浪荡不羁 2025-01-28 10:57:50

您可以做的另一件事是在获取方法中传输IF语句,因此您可以在此检查是否有空。更具体地说:

 public String getFirstname() {
    if (firstname != null){
    return firstname;}

    return "";
}

public String getLastname() {
    if (lastname!= null){
        return lastname;}

    return "";
}
public String getFirstOrLastNameOrBoth() {
   return (getFirstname() + " " + getLastname()).trim();
}

这种方式称为提取方法,在这种情况下,您不仅可以在最后一个方法中而且在Getters中检查NULL。因此,我认为这是安全的。我还使用了Trim方法来删除开头的空格,以防名字为null。

Another thing you can do is to transfer the if statements in the get methods, so you can check there whether something is null or not. More specifically:

 public String getFirstname() {
    if (firstname != null){
    return firstname;}

    return "";
}

public String getLastname() {
    if (lastname!= null){
        return lastname;}

    return "";
}
public String getFirstOrLastNameOrBoth() {
   return (getFirstname() + " " + getLastname()).trim();
}

This way is called the extract method and in this case, you'll be able to check for null not only in the last method but also in the getters. Therefore I think it is safe. I have also used the trim method in order to remove the spaces in the beginning in case the first name is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文