5 Replies - 633 Views - Last Post: 27 April 2010 - 06:46 PM Rate Topic: -----

#1 werner7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 werner7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • 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
Was This Post Helpful? 2
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 werner7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1