你认为下面的代码片段(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++;
}
}
}
}对我来说,继续的解决方案看起来更难理解,但另一方面,我们有一个反模式:(
发布于 2015-09-23 17:21:47
这两种解决方案都是不可读的。不要使用continue,也不要在一个方法中创建太多嵌套的in。
我建议:
试着使用有意义的
当然,我不理解你的方法和变量是做什么的,但我试着创建一个虚构的例子,说明重构的、有文档记录的代码可能是什么样子的:
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;
}
}发布于 2015-09-23 17:08:03
我投票支持继续。但也许您可以将状态映射到枚举,然后使用switch/case?
https://stackoverflow.com/questions/32735137
复制相似问题