Asked • 06/19/19

How can I simplify this set of if statements? (Or, what's making it feel so awkward?)?

My colleague showed me this piece of code, and we both wondered why we couldn't seem to remove duplicated code. private List<Foo> parseResponse(Response<ByteString> response) { if (response.status().code() != Status.OK.code() || !response.payload().isPresent()) { if (response.status().code() != Status.NOT_FOUND.code() || !response.payload().isPresent()) { LOG.error("Cannot fetch recently played, got status code {}", response.status()); } return Lists.newArrayList(); } // ... // ... // ... return someOtherList; } Here's an alternate representation, to make it a little less wordy: private void f() { if (S != 200 || !P) { if (S != 404 || !P) { Log(); } return; } // ... // ... // ... return; } Is there a simpler way to write this, without duplicating the `!P`? If not, is there some unique property about the situation or conditions that makes it impossible to factor out the `!P`?

1 Expert Answer

By:

Tim C. answered • 06/22/19

Former math teacher - math, CS, and science tutor

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.