I have some code that I've included in a project I want to release. The problem is, I've only had time to code it late at night, when my brain is working at a less-than-optimal state. Therefore, I've ended up with some blocks of code that are themselves, less-than-optimal. Here is one such block:
NSString *path = nil;
if (delegate && [delegate respondsToSelector:@selector(toolbarAssociatedXibName:)]) {
path = [delegate toolbarAssociatedXibName:_toolbar];
if (![[NSBundle mainBundle] pathForResource:path ofType:@"nib"]) {
IFLog(@"Delegate responded with an invalid value (%@) for `toolbarAssociatedXibName:`. Attempting default value (%@).", path, _identifier);
path = _identifier;
}
} else {
path = _identifier;
}
Rewritten in pseudocode, for those who don't write in Objective-C:
Variable declaration (set to null value)
If there is an option to set the variable to a custom value
Set it to that custom value
If the value is not valid
Log the error
Set the value to a specified default
Otherwise
Set the value to a specified default
Is it possible to shorten/compact this block of code? Normally, I would go for something like a ternary operator and some clever hacking, but I haven't been able to come up with something smarter than this (perhaps I should get some sleep). Any suggestions?
Edit: I have also tried combining this block into a single line of code, but that hasn't done any good either. I end up with something looking like this:
(delegate && [delegate respondsToSelector:@selector(toolbarAssociatedXibName:)] ? !![[NSBundle mainBundle] pathForResource:[delegate toolbarAssociatedXibName:_toolbar] ofType:@"nib"] ? path = [delegate toolbarAssociatedXibName:_toolbar] : IFLog(@"Delegate responded with an invalid value (%@) for `toolbarAssociatedXibName:`. Attempting default value (%@).", [delegate toolbarAssociatedXibName:_toolbar], _identifier), path = _identifier : path = _identifier);
That does absolutely nothing for readability. I guess there isn't much that can be done here...