首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >for-loop + Arrow Anti Pattern VS for-loop + continue

for-loop + Arrow Anti Pattern VS for-loop + continue
EN

Stack Overflow用户
提问于 2015-09-23 16:58:26
回答 2查看 246关注 0票数 2

你认为下面的代码片段(for-loop + continue)

代码语言:javascript
复制
for (Identity fileIdentity : fileIdentities) {
      totalFileCount++;
      if (!forceTheStatus(params, fileIdentity))
        continue;
      updatedFileCount++;
      if (!params.shouldUpdateLinkedEntity())
        continue;
      Optional<Identity> batchIdentity = getLinkedBatchIdentity(fileIdentity);
      if (!batchIdentity.isPresent())
        continue;
      totalBatchCount++;
      if (!getRemittanceProcessor().forceTheStatus(params, batchIdentity.get()))
        continue;
      updatedBatchCount++;
}

比其他(for-loop + Arrow Anti Pattern)更好吗?为什么?

代码语言:javascript
复制
for (Identity fileIdentity : fileIdentities) {
      totalFileCount++;
      if (forceTheStatus(params, fileIdentity)) {
        updatedFileCount++;
        if (params.shouldUpdateLinkedEntity()) {
          Optional<Identity> batchIdentity = getLinkedBatchIdentity(fileIdentity);
          if (batchIdentity.isPresent()) {
            totalBatchCount++;
            if (getRemittanceProcessor().forceTheStatus(params, batchIdentity.get()))
              updatedBatchCount++;
          }
        }
      }
}

对我来说,继续的解决方案看起来更难理解,但另一方面,我们有一个反模式:(

EN

回答 2

Stack Overflow用户

发布于 2015-09-23 17:21:47

这两种解决方案都是不可读的。不要使用continue,也不要在一个方法中创建太多嵌套的in。

我建议:

试着使用有意义的

  1. Split pieces
  2. Document names

  • smaller pieces

  • Document你的代码

当然,我不理解你的方法和变量是做什么的,但我试着创建一个虚构的例子,说明重构的、有文档记录的代码可能是什么样子的:

代码语言:javascript
复制
import java.util.Arrays;
import java.util.Optional;

public class Snippet {

    /** The total number of all files, that were inspected */
    private int totalFileCount;

    /** The number of files that have been modified in any kind of way */
    private int updatedFileCount;

    /** The number of files, that were put into batch processing */
    private int totalBatchCount;

    /** The number of files, that have successfully been processed by the batch */
    private int updatedBatchCount;

    /** inspects all files, processes them, if required */
    private void processFiles(Parameters params, Iterable<Identity> fileIdentities) {
        for (Identity fileIdentity : fileIdentities)
            processFile(params, fileIdentity);
    }

    /** inspects a single file, processes it, if required */
    private void processFile(Parameters params, Identity fileIdentity) {
        totalFileCount++;
        if (forceTheStatus(params, fileIdentity))
            updateFile(params, fileIdentity);
    }

    /** updates the file, and - if necessary - updates the linked entity */
    private void updateFile(Parameters params, Identity fileIdentity) {
        updatedFileCount++;
        if (params.shouldUpdateLinkedEntity())
            updateLinkedEntity(params, fileIdentity);
    }

    /** puts the linked entity into the batch processing queue (if any exists) */
    private void updateLinkedEntity(Parameters params, Identity fileIdentity) {
        Optional<Identity> batchIdentity = getLinkedBatchIdentity(fileIdentity);
        if (batchIdentity.isPresent())
            batchUpdateLinkedEntity(params, batchIdentity);
    }

    /**
      * puts the linked entity into the batch processing queue
      * and uses the remittance processor
      */
    private void batchUpdateLinkedEntity(Parameters params, Optional<Identity> batchIdentity) {
        totalBatchCount++;
        if (getRemittanceProcessor().forceTheStatus(params, batchIdentity.get()))
            updatedBatchCount++;
    }

    // Dummy implementations to make the code compilable

    public static class Parameters {
        public boolean shouldUpdateLinkedEntity() {
            return false;
        }
    }

    public static class Identity {
    }

    public static void main(String[] args) {
        Iterable<Identity> fileIdentities = Arrays.asList(new Identity());
        new Snippet().processFiles(new Parameters(), fileIdentities);
    }

    private Snippet getRemittanceProcessor() {
        return null;
    }

    private Optional<Identity> getLinkedBatchIdentity(Identity fileIdentity) {
        return null;
    }

    private boolean forceTheStatus(Object params, Identity fileIdentity) {
        return false;
    }
}
票数 4
EN

Stack Overflow用户

发布于 2015-09-23 17:08:03

我投票支持继续。但也许您可以将状态映射到枚举,然后使用switch/case?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32735137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档