Skip to content

Quiz #1 - Returning different values depending on permission #2

@arialdomartini

Description

@arialdomartini

This is the code

https://github.com/code4nothing/singlepath/blob/2c4cf0990f3969762c5d6a176f2e6003c688188c/java/src/main/java/it/tug/Main/Formatter.java

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 terrible. Not only is the Service API awful (askForPermission could return a boolean rather than a String), but the Formatter.doTheJob() code itself is a huge if-then-else snippet.

This is one of the 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions