Skip to content

Conversation

@arialdomartini
Copy link
Contributor

The quiz

Here is a snippet of code I took (and adapted) from our production code.
Yes, it is real code, hence your Pull Requests could be truly accepted 🍺 🍺 🍺

public String Execute(final String file) {

        final String rewrittenUrl = fileHandler.getXmlFileFromFileName(file);
        final String executionId = fileHandler.getExecutionIdFromFileName(file);

        if ((executionId == "") || (rewrittenUrl == "")) {
            return "";
        }

        Foo knownFoo = fooRepository.getFooByXmlFileName(rewrittenUrl);

        if (knownFoo == null) {
            return "";
        }

        return knownFoo.DoThat(file);
    }

There are two conditional sections.

        if ((executionId == "") || (rewrittenUrl == "")) {
            return "";
        }

Now, Guard Clauses are not necessarily bad.
It would be very interesting to deliberately remove it, only in order to judge if the resulting code is better or worse

  • The second one is
  if (knownFoo == null) {
            return "";
        }

        return knownFoo.DoThat(file);

To me, it smells of missing Null Object, doesn't it?

arialdomartini added a commit that referenced this pull request Jul 25, 2014
Quiz #3 - Smell of Null Objects
@arialdomartini arialdomartini merged commit aa0224d into code4nothing:master Jul 25, 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