0

I'm not really sure what this is called so it's hard to look it up and it is best if I show you what I'm trying to do.

I want to create a condional variable of sorts

String fileName = (if (this.filename != null) { return this.filename; } 
                   else { return "default value"; }); 

This should be pretty clear on what I'm trying to do. I want to use some sort of condition to set this variable based on another variables input, in this case whether or not it equals null or not.

DerekE
  • 235
  • 4
  • 15
  • 3
    Look up ternary operator. – Hovercraft Full Of Eels Mar 12 '13 at 03:35
  • possible duplicate of [How does the ternary operator work?](http://stackoverflow.com/questions/463155/how-does-the-ternary-operator-work) – Hovercraft Full Of Eels Mar 12 '13 at 03:36
  • what are you seeking after you find the term? Default value might be the term I use. – donfede Mar 12 '13 at 03:37
  • 1
    The Ternary operator is what I was after, I didnt know what it was called. Now I have something to look up as this seems to be exactly what I needed. – DerekE Mar 12 '13 at 03:39
  • Interestingly, some claim that "ternary operator" is a misnomer. See [this Stack Overflow discussion]. (http://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do) So I stand by my "question mark thingie" comment below... – user949300 Mar 12 '13 at 04:01

4 Answers4

7

Use the ternary operator. In my opinion, this is one of strategy in defensive programming.

String fileName = (this.filename != null? this.filename : "default value");
Glenn
  • 12,741
  • 6
  • 47
  • 48
5
String fileName = this.filename != null ? this.filename : "default value";
Careal Manic
  • 212
  • 2
  • 4
1

Or, more verbose but (perhaps) easier to understand

String aFilename;
if (this.filename != null)
  aFilename = this.filename;
else
  aFilename = "Default Value";
return aFilename;

I prefer Careal's code but YMMV. Some find the ? operator complicated (especially in messy cases)

Also, when stepping though with the debugger this code will be way easier to see what happened.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • 1
    I wanted to save lines, the ternary operator is what I was looking for, thanks for the answer though :D – DerekE Mar 12 '13 at 03:38
  • 1
    @DerekE: how much do you have to pay for each line that you use? How much are you really saving when you save lines? If you sacrifice readability and have a difficult debug later, you really aren't saving anything. 1+ – Hovercraft Full Of Eels Mar 12 '13 at 03:39
  • Thanks for the upvote. One minor advantage of this technique is that when you step through with the debugger it's usually much easier to see what is happening. Very useful in complex cases. – user949300 Mar 12 '13 at 03:43
  • Well it doesn't cost anything, but if I can do it on 1 line, maybe two vs six than I'll try that route. Plus the ternary operator seems pretty straight forward to read. – DerekE Mar 12 '13 at 03:43
  • @user949300, I use this same time of method when I need more complex stuff, but for a simple default value of a filename for this instance, the ternary seems sufficient. – DerekE Mar 12 '13 at 03:45
  • @DerekE: I've seen misuse of this operator quite a bit. The goal of your code shouldn't be brevity but rather you should write code that others, and your 6 months from now *future* self will understand easily and quickly. – Hovercraft Full Of Eels Mar 12 '13 at 03:45
  • @DerekE In this fairly "simple" case, I think the ternary is fine. But Hovercraft makes a good point that, in _complicated_ code, 6 months later when the original programmer is long gone and nobody remembers the requirements, it _will_ come back to bite you. – user949300 Mar 12 '13 at 03:48
  • @HovercraftFullOfEels, Oh yea I agree for sure, that is also why comments exist. Although I will admit that I'm pretty bad about placing comments. – DerekE Mar 12 '13 at 03:50
0

You can use ternary operator: boolean expression ? value1 : value2

String fileName = fileName == null ? "Default value" : this.filename;
Iswanto San
  • 18,263
  • 13
  • 58
  • 79