0

Is the following code the best way to check for null for two times in a single line for initializaing a final variable?

final String textValue = text != null ? text.getText() != null ? text.getText() : "" : "";
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90

4 Answers4

8

Well I'd probably use a single conditional with an && condition instead:

final String textValue = text != null && text.getText() != null ? text.getText()
                                                                : "";

If you find you need to do this in more than one place, you may well want to wrap it in a method:

// We don't know what the type of text is here... adjust appropriately.
public static String getTextOrDefault(TextBox text, String defaultValue)
{
    return text != null && text.getText() != null ? text.getText()
                                                  : defaultValue;
}

Or adjusted to avoid calling getText() more than once:

// We don't know what the type of text is here... adjust appropriately.
public static String getTextOrDefault(TextBox text, String defaultValue)
{
    if (text == null)
    {
        return defaultValue;
    }
    String textValue = text.getText();
    return textValue != null ? text.getText() : defaultValue;
}

Then you can simplify your variable declaration:

final String textValue = SomeHelper.getTextOrDefault(text, "");

Note that whether calling text.getText() more than once is a problem depends on your scenario - in some cases it would be a bad idea, and you should restructure your code to avoid it. We can't tell for sure, but it's worth considering.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Aubin Why not? For this case it's presumably fine - if it was a "long operation" or had side-effects, then no. – user2246674 Jun 17 '13 at 06:01
  • @Aubin: Maybe. Maybe not. We don't really know without knowing the class involved. – Jon Skeet Jun 17 '13 at 06:02
  • For performance reason, I don't knwon (and I don't want to know) how complex the method getText() is. In the "method" version the ternary should be replaced by two lines. – Aubin Jun 17 '13 at 06:03
  • @Aubin let the JIT handle this. – Luiggi Mendoza Jun 17 '13 at 06:03
  • @Aubin: I've edited the answer to give that option, but quite often simplicity triumphs *potential* (but unrealistic) performance concerns. For example, I'm happy enough calling `ArrayList.size()` in a loop. – Jon Skeet Jun 17 '13 at 06:04
  • @JonSkeet, you eat the SO Scores, and others must be very very fast to beat you :-). Nice answer. Thanks. – Mohsen Afshin Jun 17 '13 at 06:30
3

You can do this :

final String textValue = (text != null && text.getText() != null) ? text.getText() : "" ;
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

I am not sure what you mean by "best" (most programmer friendly? most efficient execution?)

But a programmer friendly alternative could be:

final String textValue = (text != null && text.getText() != null) ? text.getText() : "";
tbsalling
  • 4,477
  • 4
  • 30
  • 51
0

I assume this line is somewhere in a method. So this one will be more readable:

String value = "";
if (text != null && text.getText() != null) {
  value = text.getText();
}
final String textValue = value;
Obenland
  • 856
  • 16
  • 28