6

This foo that is returned by lookup could be null.

That's why I'm trying to avoid calling foo.getFooStr() on a null value by first returning null if foo is null.

But is there a better (more concise) way to write this?

public static String getFooStr(String input)
{
    Foo foo = lookup(input);
    if(foo==null)
    {
        return null;
    }
    return foo.getFooStr();
}
3
  • This question is similar enough (but not identical) to hopefully give you some insight: stackoverflow.com/questions/271526/… Sep 11, 2011 at 22:51
  • 4
    There is nothing wrong with what you've written. Writing it so that it looks shorter doesn't necessarily make it better.
    – hookenz
    Sep 11, 2011 at 22:54
  • 2
    Plus, you can have breakpoints in different lines for the case of the string being null or nonnull. That helps with debugging. Sep 12, 2011 at 6:26

7 Answers 7

34

You've two questions: is there a better way to write the code, and is there a more concise way to write the code.

Regarding more concise, this could work:

public static String getFooStr(String input) {
    Foo foo = lookup(input);          
    return foo == null ? null : foo.getFooStr();
}

Regarding better: I value readability over conciseness any day, and by a wide margin. Your original code looks fine to me. What matters is what looks good to you, and which is easier for you to understand and debug 3 months from now. I've heard someone say it best -- write your code so that is easily understandable by others, and even more importantly, by your future self.

4
  • 6
    +1 IMHO less code is good as long as it's still clear, and this is. btw, I removed the bracket noise (ie foo == null instead of (foo == null)) for better readability - hope you don't mind.
    – Bohemian
    Sep 11, 2011 at 22:54
  • 5
    @Bohem: HFOE said it well, what matters is what ... is easier for you to understand and debug ... and so that it is easily understandable by others. The "bracket noise" you removed may be only noise for those of us who know the grammar of C-like operators as almost a mother tongue; for less experienced people (which is far more people, but may not be your audience), the parentheses make it more readable, because they don't have to know whether ?: has higher precedence than ==. I'm not saying you're wrong to remove the parens, but they're not noise, and I would more likely leave them in.
    – LarsH
    Mar 14, 2012 at 11:11
  • 1
    +1. I often advise my minions (students) that come to me with suggestions about shaving off a few milliseconds, that they should first optimise for readability. Whatever time they save may well be wiped out by the next coder wasting an hour trying to understand their optimisation six months later :-)
    – paxdiablo
    Jan 30, 2013 at 4:51
  • @paxdiablo like backups they need to have tried it personally to understand why :) better they do it while it is not important yet. Sep 4, 2013 at 3:28
6

Why isn't there a lookup that returns the appropriate foo string?

0
3

I'm not into java, but I do like clean code... Source code should be easy to read and understand for humans - the machine doesn't care how it looks but your colleagues do. More concise code usually takes a moment or two longer to grasp (sometimes much longeer depending on the quantity and complexity). Keep code understandable and it will be maintainable (even if it is a bit more verbose)!

3

Groovy does it nicer.

return lookup(input)?.fooStr

or even just:

lookup(input)?.fooStr

0
2

For Java 7, at some point it was planned that you could just write this:

public static String getFooStr(String input)
{
    Foo foo = lookup(input);
    return foo?.getFooStr();
}

But until this feature is widely known, you will have to stick to the ?: operator.

4
  • Is this currently availble in Java7 any doco please link if possible.I would be interested in reading up on this. Sep 11, 2011 at 23:52
  • That feature didn't get into Java7 though and I doubt it'll be added any time soon. Though C# obviously has it ;-)
    – Voo
    Sep 11, 2011 at 23:58
  • 4
    The Elvis operator was dropped from Java 7 in 2009. The Project COIN site says this: "While the Elvis operator and related operators are helpful in Groovy, differences between Groovy and Java, such as the presence of primitive types and interactions with boxing/unboxing render the operators less useful in Java. JDK 7 will support other ways to ease the pain of null-handling, such as the null checking enabled by JSR 308." (JSR 308 is type annotations, and that got deffered to JDK 8)
    – Stephen C
    Sep 12, 2011 at 5:00
  • 1
    Just to be technically correct the elvis operator is ?:, not ?.. The ?. operator is the null safe dereferencing operator. Elvis is/was not one eyed.
    – Brett Ryan
    Oct 31, 2013 at 4:07
1

I do not like multiple returns any where in any code. I will just change it to

public static String getFooStr(String input)
{
    Foo foo = lookup(input);
    String fooString;
    if(foo!=null)
    {
        fooString = foo.getFooStr();
    }
    return fooString;
}

Also the version from @Hovercraft Full Of Eels is good in my opinion but less readable but still a common way of doing.

3
  • 5
    If you don't like multiple returns, then please don't look at Throwable.
    – A.H.
    Oct 2, 2011 at 23:02
  • if the code above is not clear enough you should not be coding @EricLindauer Oct 10, 2011 at 8:28
  • @EricLindauer did you down vate me ? Oct 10, 2011 at 10:47
0

In Java8:

Optional.ofNullable(lookup(input)).map(f->f.getFooStr()).orElse(null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.