# If statement String Boolean question

Page 1 of 1

## 5 Replies - 793 Views - Last Post: 27 April 2010 - 06:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=170657&amp;s=7b00b62ac23355ad4b024b87c43ec1dd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 werner7

Reputation: 0
• Posts: 8
• Joined: 13-April 10

# If statement String Boolean question

Posted 27 April 2010 - 03:44 PM

Hey,

I was told by a masters programming student that helps us in class that the following example code isn't good practice.
```public String toString()
{
String msg = "";
msg += "A ";
msg += (isFemale)?"female ":"male ";
msg += breed +" pig\n";
return msg;

```

The
```msg +=(booleanName)? "ture ":"fasle ";
```

was the concern as he said that I should rather write an if statement, instead of this line like that because it makes the code easier to read and understandable to others. Is this how everyone sees it? or is it just him?

Thanks.

Is This A Good Question/Topic? 0

## Replies To: If statement String Boolean question

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: If statement String Boolean question

Posted 27 April 2010 - 03:50 PM

well in this example, I disagree as its a simple conditional statement with no nested conditionals which could make your program less readable. In this example everything is clear so its ok.

### #3 werner7

Reputation: 0
• Posts: 8
• Joined: 13-April 10

## Re: If statement String Boolean question

Posted 27 April 2010 - 03:54 PM

Thanks a lot there. I saw it that way as well.

### #4 PsychoCoder

Reputation: 1653
• Posts: 19,853
• Joined: 26-July 07

## Re: If statement String Boolean question

Posted 27 April 2010 - 04:45 PM

I would have to disagree with his statement. I see no problem with someone using a ternary operator, in fact I prefer them over a bunch of if..else statements pesonally

### #5 Dogstopper

Reputation: 2955
• Posts: 11,220
• Joined: 15-July 08

## Re: If statement String Boolean question

Posted 27 April 2010 - 06:10 PM

It is all a matter of opinion. There are some purists in either direction, but actually, I agree with PsychoCoder here in that 1 short, quick line is better than 2-4 lines for the if statement. However, if you're graded on it, do what your professor wants.

### #6 werner7

Reputation: 0
• Posts: 8
• Joined: 13-April 10

## Re: If statement String Boolean question

Posted 27 April 2010 - 06:46 PM

Not only have I gotten my answer but I learned that its called a "Ternary Operation."

Thanks guys, and yes I will see if its accepted by my professor just in case I get marks taken down.