【发布时间】:2015-09-23 08:58:25
【问题描述】:
你认为下面的代码片段(for-loop + continue)
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)?为什么?
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++;
}
}
}
}
对我来说 continue 的解决方案看起来更难理解,但另一方面我们有一个反模式:(
【问题讨论】:
-
antipattern,恕我直言,是一个更难阅读、维护、支持的工作人员> 等等。在你的情况下,它是第一个块。
ifs 的数量(及其级别)没有任何意义。
标签: java design-patterns anti-patterns