Reputation: 2356
First off, I'm sorry for the question title.
Now, the question: I'm writing an iPhone app that reads compass values and displays the name of direction the device is facing. As of writing, I do it like this:
switch (trueHeadingRounded) {
case 23 ... 68:
cardinalDirection = @"northeast";
break;
case 69 ... 114:
cardinalDirection = @"east";
break;
//ad infinitum
How can I shorten this part? There has to be a better way.
Upvotes: 2
Views: 87
Reputation: 47739
NSString* cardinalDirection = @[@"north", @"northeast", @"east", @"southeast", @"south", @"southwest", @"west", @"northwest", @"north"][(trueHeadingRounded + 23) / 45];
It might be I'm off on the 23 constant or some such -- I haven't double-checked my calcs. (But I kinda agree with Zaph.)
(And, of course, if you really did this it would probably be wise to make the array a relative constant -- inited class property or some such -- for efficiency's sake.)
Upvotes: 1
Reputation: 52237
You could increase readability by using named constants and not magic numbers
typedef NS_ENUM(NSInteger, Heading) {
HeadingNorth = -1,
HeadingNorthEast,
HeadingEast,
HeadingSouthEast,
HeadingSouth,
HeadingSouthWest,
HeadingWest,
HeadingNorthWest
};
You could than alter your switch statement
Heading heading = floor((trueHeadingRounded - 23) / 45);
NSString *cardinalDirection;
switch (heading) {
case HeadingNorth: cardinalDirection = @"north"; break;
case HeadingNorthEast: cardinalDirection = @"northeast"; break;
case HeadingEast: cardinalDirection = @"east"; break;
case HeadingSouthEast: cardinalDirection = @"southeast"; break;
case HeadingSouth: cardinalDirection = @"south"; break;
case HeadingSouthWest: cardinalDirection = @"southwest"; break;
case HeadingWest: cardinalDirection = @"west"; break;
case HeadingNorthWest: cardinalDirection = @"northwest"; break;
}
NSLog(@"%@", cardinalDirection);
Upvotes: 1
Reputation: 40
you can use something like:
cardinalDirection = ( trueHeadingRounded <= 22 ? @"north" :
trueHeadingRounded <= 68 ? @"northeast" :
treuHeadingRounded <= 114 ? @"east" :
etc.
However, I think I'd use the mapping array suggestion.
Upvotes: 0
Reputation: 2741
You could do the following:
(x - 23) / 45
or something like that.[0] => 'Northeast', [1] => 'East', etc.
return mappingArray[indexFromAngle(x)]
Upvotes: 5
Reputation: 112855
Don't write code to shorten it, write code to make it more understandable. Clarity is everything.
If there is a concern about performance write for clarity, then if there is a performance problem profile to find exactly what and where and then fix that.
Upvotes: 3