While working on project code left to me by a previous dev, I have encountered the following construct
-(NSString *)StringCheckWithString:(NSString *)string{
NSString *string2 = [NSString stringWithFormat:@"%@", string];
if([string2 length] == 0){
return @"none";
}
else {
return string2;
}
}
Can anyone explain why you would do this, it seems significantly overengineered to me and I don't understand why it has been done this way (for clarity, I don't understand why the string is formatted like this, I understand the length check)
stringis an unknown input. If it contains format characters and you use it raw, BAD THINGS can happen.stringcontains format characters,string2now also contains those characters?[NSString stringWithFormat:@"%@", string](if it's really needed) can be achieved much more cheaply with[string description]. Though you far too often see the stringWithFormat thing used where it's entirely unnecessary -- it's like bad DNA that gets copied form one generation to the next.NSLog(ieNSLog(string)vsNSLog(@"%@", string)) was bad and assumed this would fix it in the general case.