понеділок, 3 червня 2013 р.

О пользе рефакторинга

Вот простой пример, как можно рефакторить код и сделать его короче и читаемым:
Сначала получилась как обычно ужасная куча нечитаемого кода:

NSString *name = [[filename pathComponents] lastObject];
NSString *ext = [name pathExtension];
NSString *iconImg = [NSString stringWithFormat:@"icon%@",[ext uppercaseString]];
if (!iconImg) iconImg = @"iconUnknown";
if (([ext caseInsensitiveCompare:@"html"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"htm"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"xml"] == NSOrderedSame))
    iconImg = @"iconWeb";
else if (([ext caseInsensitiveCompare:@"mp3"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"m4a"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"wav"] == NSOrderedSame))
        iconImg = @"iconAudio";
else if (([ext caseInsensitiveCompare:@"mp4"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"mpeg"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"qt"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"avi"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"m4v"] == NSOrderedSame))
    iconImg = @"iconVideo";
else if (([ext caseInsensitiveCompare:@"mp3"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"m4a"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"wav"] == NSOrderedSame))
    iconImg = @"iconAudio";
else if (([ext caseInsensitiveCompare:@"jpg"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"jpeg"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"gif"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"tiff"] == NSOrderedSame)||([ext caseInsensitiveCompare:@"tiff"] == NSOrderedSame))
    iconImg = @"iconGraphic";
else if (([ext caseInsensitiveCompare:@"xlsx"] == NSOrderedSame))
    iconImg = @"iconXLS";
else if (([ext caseInsensitiveCompare:@"docx"] == NSOrderedSame))
    iconImg = @"iconDOC";
else if (([ext caseInsensitiveCompare:@"pptx"] == NSOrderedSame))
    iconImg = @"iconPPT";
 
_icon = [UIImage imageNamed:iconImg];

Пришлось написать довесок к классу NSString:

@interface NSString (Adds)
- (BOOL)caseInsensitiveExistsInArray:(NSArray *)a;
@end
 
@implementation NSString (Adds)
 
- (BOOL)caseInsensitiveExistsInArray:(NSArray *)a {
    BOOL exist = NO;
    for (NSString *s in a) {
        if ([self caseInsensitiveCompare:s] == NSOrderedSame) {
            exist = YES;
            break;
        }
    }
    return exist;
}
 
@end

На выходе теперь стало вот так:

NSString *name = [[filename pathComponents] lastObject];
NSString *ext = [name pathExtension];
NSString *iconImg = [NSString stringWithFormat:@"icon%@",[ext uppercaseString]];
if (!iconImg) iconImg = @"iconUnknown";
if ([ext caseInsensitiveExistsInArray:@[@"html",@"htm",@"xml"]])
    iconImg = @"iconWeb";
else if ([ext caseInsensitiveExistsInArray:@[@"mp3",@"m4a",@"wav"]])
    iconImg = @"iconAudio";
else if ([ext caseInsensitiveExistsInArray:@[@"mp4",@"mpeg",@"mpg",@"qt"]])
    iconImg = @"iconVideo";
else if ([ext caseInsensitiveExistsInArray:@[@"jpg",@"jpeg",@"gif",@"tiff"]])
    iconImg = @"iconGraphic";
else if ([ext caseInsensitiveExistsInArray:@[@"xlsx"]])
    iconImg = @"iconXLS";
else if ([ext caseInsensitiveExistsInArray:@[@"docx"]])
    iconImg = @"iconDOC";
else if ([ext caseInsensitiveExistsInArray:@[@"pptx"]])
    iconImg = @"iconPPT";

_icon = [UIImage imageNamed:iconImg];

По моему, результат на лицо — код стал намного понятнее и удобнее наращивать…

Немає коментарів:

Дописати коментар

HyperComments for Blogger

comments powered by HyperComments