Skip to content

Conversation

@arialdomartini
Copy link
Contributor

The quiz

This is an example of a (Java) snippet of code where an if-then-else seems pretty natural.

We have an imaginary Formatter class with a method (doTheJob(String)) performing a trivial feature (it takes a string and it outputs the same string repeated twice).

Now, the class performs differently based on the output of its only dependence, Service.askForPermission()

Service.askForPermission() Formatter.doTheJob("foo")
"OK" "foofoo"
"FAIL" "error"
any other result ""

Apparently, the code is awful. Not only is the Service API awful (askForPermission) could return a boolean rather than a String. What's worst, the Formatter.doTheJob() code is a huge if-then-else snippet.

This is one of the (extreme?) cases where I think a conditional is pretty hard to remove. Or, at least, where I think the removal could produce a code more awful than the original one.

Can you prove me wrong?

arialdomartini added a commit that referenced this pull request Jul 21, 2014
if-then-else necessary in order to return a different output depending on a service response
@arialdomartini arialdomartini merged commit 2c4cf09 into code4nothing:master Jul 21, 2014
@arialdomartini arialdomartini changed the title if-then-else necessary in order to return a different output depending on a service response Quiz #1 - if-then-else necessary in order to return a different output depending on a service response Jul 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant