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:
-(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:
-(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.