简体   繁体   English

如何使用字符串生成器

[英]How to use stringBuilder

I have a function and Technical Leader review code and said : Why this if statement?我有一个功能和技术负责人审查代码并说:为什么这个if语句? It's basically the same message.它基本上是相同的消息。 If you want customized, use a string builder with the type.如果要自定义,请使用具有类型的字符串生成器。 How to change it , can someone help me?怎么改,谁能帮帮我?

private Optional<String> validatePrimaryPath(SalesChannelType salesChannelCode, List<String> primaryPathList) {

  if (CollectionUtils.isEmpty(primaryPathList)) {
    if (salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)) {
      return Optional.of("Customer Hierarchy is mandatory field for HebToYou.");
    } else {
      return Optional.of("Customer Hierarchy is mandatory field.");
    }
  }

  return Optional.empty();
}

Specifying two string literals implies that there is no runtime overhead.指定两个字符串文字意味着没有运行时开销。 You can achieve the same using string concatenation, when all arguments are compile-time constants.当所有参数都是编译时常量时,您可以使用字符串连接来实现相同的效果。 In contrast, using a StringBuilder always implies a runtime operation.相反,使用StringBuilder总是意味着运行时操作。 You can read more about the “ StringBuilder is better than + myth” in this answer .您可以在此答案中阅读有关“ StringBuilder+神话更好”的更多信息。

You can generally reduce the syntactic complexity of you code:您通常可以降低代码的语法复杂度:

private Optional<String> validatePrimaryPath(
    SalesChannelType salesChannelCode, List<String> primaryPathList) {

    final String prefix = "Customer Hierarchy is mandatory field";
    final String general = prefix + ".", forHebToYou = prefix + " for HebToYou.";

    return Optional.of(
            salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)? forHebToYou: general)
        .filter(s -> CollectionUtils.isEmpty(primaryPathList));
}

This emphasizes that you are doing the same, just with slightly different data and uses the semantic of Optional instead of an if statement.这强调您正在做同样的事情,只是数据略有不同,并使用Optional的语义而不是if语句。 And the actual code doesn't need changes, if you decide the externalize the actual strings.如果您决定外部化实际字符串,则实际代码不需要更改。

Note that if SalesChannelType is an enum , there is no need for equals , you can just use salesChannelCode == SalesChannelType.HEB_TO_YOU then.请注意,如果SalesChannelTypeenum ,则不需要equals ,您可以使用salesChannelCode == SalesChannelType.HEB_TO_YOU然后。

To prevent writing the same (partial) string literal multiple times, you can:为了防止多次写入相同的(部分)字符串文字,您可以:

  • Use a constant for the common part:对公共部分使用常量:

     if (CollectionUtils.isEmpty(primaryPathList)) { final String COMMON = "Customer Hierarchy is mandatory field"; if (salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)) { return Optional.of(COMMON + " for HebToYou."); } else { return Optional.of(COMMON + "."); } }
  • Build the string using StringBuilder :使用StringBuilder构建字符串:

     if (CollectionUtils.isEmpty(primaryPathList)) { StringBuilder buf = new StringBuilder("Customer Hierarchy is mandatory field"); if (salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)) { buf.append(" for HebToYou"); } return Optional.of(buf.append('.').toString()); }

Personally, I would keep the code in the question, especially if you ever might need support non-English versions of the text, because in other languages the extra text might not go there.就我个人而言,我会将代码保留在问题中,特别是如果您可能需要支持非英语版本的文本,因为在其他语言中,额外的文本可能不会出现在那里。

First, please don't use raw types.首先,不要使用原始类型。 Second, I do not agree that using a StringBuilder to build the message is an improvement;其次,我不同意使用StringBuilder来构建消息是一种改进; however, since that is what you want I will show you what was likely intended.但是,既然这是您想要的,我将向您展示可能的意图。 Something like,就像是,

private Optional<String> validatePrimaryPath(SalesChannelType salesChannelCode, 
                List<String> primaryPathList) {
    if (CollectionUtils.isEmpty(primaryPathList)) {
        StringBuilder sb = new StringBuilder(
                    "Customer Hierarchy is mandatory field");
        if (salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)) {
            sb.append(" for HebToYou");
        }
        sb.append(".");
        return Optional.of(sb.toString());
    }

    return Optional.empty();
}

Note that I have specified the method returns an Optional<String> and takes a List<String> when you write Optional and List without specifying the type then you are using raw types.请注意,当您编写OptionalList而不指定类型时,我已指定该方法返回一个Optional<String>并采用List<String> ,然后您使用的是原始类型。

Personally, I think your code is OK but I have a suggestion for this if you still want to use StringBuilder就个人而言,我认为您的代码没问题,但如果您仍想使用StringBuilder ,我对此有一个建议

private Optional validatePrimaryPath(SalesChannelType salesChannelCode, List primaryPathList) {
    if (CollectionUtils.isEmpty(primaryPathList)) {
        StringBuilder sb = new StringBuilder("Customer Hierarchy is mandatory field.");
        if (salesChannelCode.equals(SalesChannelType.HEB_TO_YOU)) {
            sb.insert(sb.length()-1, " for HebToYou");
        }
        return Optional.of(sb.toString());
    }
    return Optional.empty();
}

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

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