I am processing, say, a list of Document objects. Before recording the processing of a document successfully, I first want to check a couple of things. Say a file referencing a document must be present and something in the document must be present. Just two simple checks for an example, but remember 8 more checks before I successfully process my document.
What will be your preference?
for (Document document : List<Document> documents) {
if (!fileIsPresent(document)) {
doSomethingWithThisResult("File is not present");
continue;
}
if (!isSomethingInTheDocumentPresent(document)) {
doSomethingWithThisResult("Something is not in the document");
continue;
}
doSomethingWithTheSucces();
}
or
for (Document document : List<Document> documents) {
try {
fileIsPresent(document);
isSomethingInTheDocumentPresent(document);
doSomethingWithTheSucces();
} catch (ProcessingException e) {
doSomethingWithTheExceptionalCase(e.getMessage());
}
}
public boolean fileIsPresent(Document document) throws ProcessingException {
... throw new ProcessingException("File is not present");
}
public boolean isSomethingInTheDocumentPresent(Document document) throws ProcessingException {
... throw new ProcessingException("Something is not in the document");
}
Which is more readable. What's better? Is there even a better way to do this (possibly using some kind of design pattern)?
As for readability, my preference is currently the Exception variant ...
What's yours
source
share