Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / Objective C
Technical Blog

Ternary, Tertiary, Terinary …

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Jun 2013MIT2 min read 22.1K   1   2
What really is the correct spelling for this conditional operator?

Introduction

What really is the correct spelling for this conditional operator?

Regardless of spelling, I have never been that much a fan of its use. Most times that I have seen them used, it was done so (in my opinion) to inflate the ego of the original programmer. We all have seen that code that solved what was most likely an easy problem but whose solution was done with complicated code.

My all time favorite of course is in conditional returns that always seem to resemble:

-(Boolean)crazyFunction{
    Boolean response = YES;

    // do some code processing...

    return (YES == response) ? YES : NO;
}

Before you jump to the end and flame the comments section, please understand that I am not trying to discourage developers from ever using them. I believe at times they can come in rather handy when it comes to minimizing code so that its readability is maintained.

So now that I have given a background to my opinions, let me cover an example of how I used this in something that I am currently working on.

Problem – Variable String Building

I wanted to build a human readable string that actively demonstrates the difference between two different times.

Rules

  • It will display nothing when the difference is zero (0).
  • When the value is greater than 1, it will display a plural word.
  • When the value is equal to 1, it will display a singular version.

Fairly simple piece of code:

C++
-(NSString*) stringFromTimeInterval:(NSTimeInterval) interval
{
    int castedInterval = (int)interval;
    int minutes = (castedInterval / 60) % 60;

    NSString* result = [[NSString alloc] init];
    if (minutes > 0) {
        if (minutes > 1) {
            result = [result stringByAppendingString:
            [NSString stringWithFormat:@"%d Minutes", minutes]];
        }
        else {
            result = [result stringByAppendingString:
            [NSString stringWithFormat:@"%d Minute", minutes]];
        }
    }

    return result;
}

If I was just doing one element of time, this wouldn’t be that bad (my opinion). However, in my scenario, I actually want to solve for years, weeks, days, hours and minutes. Which grows this small bit of code into something that just becomes unreadable and unmanageable.

To simplify and clean things up a bit, I went with the following approach:

C++
-(NSString*) stringFromTimeInterval:(NSTimeInterval) interval
{
    int castedInterval = (int)interval;
    int minutes = (castedInterval / 60) % 60;

    NSString* result = [[NSString alloc] init];
    if (minutes > 0) {
        result = (minutes > 1) ? 
        [result stringByAppendingString:[NSString stringWithFormat:@"%d Minutes ", minutes]] : 
        [result stringByAppendingString:[NSString stringWithFormat:@"%d Minute ", minutes]];
    }

    return result;
}

For each time element, this saved me about 5 lines of code. I could have written the whole construction by embedding the current ternary operation inside of another however I felt by doing so I would have sacrificed readability of the code.

So when choosing to use a ternary operation, make sure you remember the following:

With great power comes great responsibility – Uncle Ben

P.S. – Sprinkle in some comments.

This article was originally posted at http://dniswhite.com?p=120

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United States United States
I am software developer with over 20 years of professional experience. I have been employed as a software developer since the early 90′s back when Microsoft’s Windows 3.1x was gaining popularity and IBM’s OS/2 was the predominant leader in 32-bit PC based Operating Systems.

Prior to choosing this as my profession I had studied architecture and then later Electrical and Mechanical engineering in college. As a young kid growing up I always played with computers, my first computer was a TRS-80 that I would spend countless hours writing programs for, I never really thought of programming as a profession. The story goes that in my final year of college I took a C/C++ programming class and had so much fun working on the various projects that my professor told me something that changed everything.

“You know they pay people to do stuff like this for a living?” – Professor Bolman

Check out my blog here.

My current and ever evolving projects:

jqAlert javascript alerts done right for those using jQueryUI.
DooScrib Doodle and scribble pad written in javascript for use with HTML5 Canvas.

Comments and Discussions

 
QuestionCleaner? Pin
ednrg26-Jun-13 4:17
ednrg26-Jun-13 4:17 
AnswerRe: Cleaner? Pin
Dennis E White26-Jun-13 4:54
professionalDennis E White26-Jun-13 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.