I just glanced at a fragment of code
and thought, "well, I could re-write that loop in one line
AFAICT, these two forms compile to essentially the exact same object code. For me, "break" is usually a rather unattractive statement, yet at the same time, an empty loop is really no more appealing. So it got me to wondering, at what point does one choose one form over the other? I realize businesses typically have coding guidelines that leave the programmer little discretion, but from a personal standpoint, what is preferable?
When writing method calls, do you personally prefer to nest them
or break them up
and how strict do company coding guidelines tend to be in such cases?
Not being a professional, I tend to decide method nesting based on need. If I will be using an object reference again, I will grab a copy of it so that the accessor method does not get called repeatedly, but sometimes reliable nested methods are just quicker to write.
(Edit) fixed the second loop example
Code:
setScanner = [self objectEnumerator];
while ( thing = [setScanner nextObject] )
{
if ( NSOrderedSame == [(NSString *)aValue caseInsensitiveCompare:thing] )
break;
}
Code:
while ( ( thing = [setScanner nextObject] )&&( NSOrderedSame != [(NSString *)aValue caseInsensitiveCompare:thing] ) ) ;
When writing method calls, do you personally prefer to nest them
Code:
[[[aThing aProperty] itsProperty] aMethod];
Code:
thingProperty = [aThing aProperty];
otherObject = [thingProperty itsProperty];
[otherObject aMethod];
Not being a professional, I tend to decide method nesting based on need. If I will be using an object reference again, I will grab a copy of it so that the accessor method does not get called repeatedly, but sometimes reliable nested methods are just quicker to write.
(Edit) fixed the second loop example