The benefits of the infamous tertiary...
Results 1 to 8 of 8

Thread: The benefits of the infamous tertiary...

  1. #1
    Member
    Join Date
    Oct 2004
    Posts
    40

    The benefits of the infamous tertiary...

    I get the feeling that a lot of the people on this forum are somewhat new to programming, so I decided to give all of you a tip that no one gave me when I was first learning to program.

    Most beginning programmers are unaware of this technique.


    Consider the following construct:

    if (foo == null) {
    bar = 2;
    } else {
    bar == foo;
    }

    what you have is a five line construct. There has to be a simpler way to declare this little variable. It's simple, use a tertiary.

    those lines above become one line in this way:

    bar = foo == null ? 2 : foo;

    what happens here, you may ask...
    Well, your compiler sees the boolean statement foo == null and does the same thing you would have initially done and turns it into an if (foo == null) and then builds the rest of the if construct for you. Which is my point, why write the 5 lines of code when the compiler will do it for you.
    -BigDick
    p.s. I know this may insult some of your intelligence, if it does I apologize, but also think that it may provide help to someone as well.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    I never liked that...lol, it is a statement which is hard to read.
    Anway, might be useful if one wants to use this per se...
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255
    The problem is too few people are familiar with its use. Also, I prefer to write it as the former to start with should I need to ever down the road take additional action if foo is 2. It's nice, but in today's world of compilers, it's unnecessary. It makes code harder to read, and it doesn't actually gain you any processing time in compiled languages. It might make processing a wee bit faster in interpreted languages like Perl or PHP, but its obfuscational side effect seems to me to not be worth it even in those languages.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  4. #4
    Member
    Join Date
    Oct 2004
    Posts
    40
    I agree, but it's extremely prolific in industrial development and wanted any young programmer who might be going into that field one day to be made aware of the concept.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

  5. #5
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    It isn't really prolific in industrial development, not where I live. I have yet to see real world code using the ternary operator.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  6. #6
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,255
    I've seen it used in open source projects before, but very infrequently. That aside, my actual point BigDick was rather that your presentation in your original post was of the "this is a cool thing you can do writing code", not as in "this is done, watch out for it". I disagree that it should be encouraged, but agree you should watch out for it and know what it is.
    Chris Shepherd
    The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
    \"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
    Is your whole family retarded, or did they just catch it from you?

  7. #7
    Junior Member
    Join Date
    Apr 2002
    Posts
    5
    I think the main reason it isn't used in programming much is readability. Back in the "dark ages" (for which I am not a part of unforutnately) you had to cram as much code into your 64K memory space as possible, and even multiple lines of text would waste valuable compiling/programming space.

    Nowadays, RAM, Processors, and Hard Disks are about a dime a dozen, and on top of that, there is SO MUCH code out there, that the key issue to focus on is not performance, but maintenance (at least in my shop it is). By having your standard If/Else statement instead of the ternary operator, it becomes much easier to manage, should somebody else pick up your code.

    Oops...that may have been too much soapboxing, but hey you noobs out there, keep your code manageable and readable, it still save VALUABLE maintenance time in the long run. 8^D

  8. #8
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Erm, I don't believe it has much to do with performance.

    Less people use it due to readability and maintenance issues. The tertiary operator is generally seen as a bit more obtuse than the simple if statement. Your own if statement wouldn't be quite as bad if there was better formatting of the code.

    In the end the compiler will generate the same code either way, optimized as far as you tell it to.

    Also, this doesn't really belong in code review. This would fit far better in the noob programming forum.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •