patternMinor
Cleaning a text field
Viewed 0 times
textcleaningfield
Problem
How can I optimize the
Note:
if condition in this snippet? The only one difference is && [self isCurrentPosition:i]. How can I make it a single if, including the condition val?Note:
self is a category of NSArray.- (void) cleanTextfieldExcluding:(int)current checkPosition:(BOOL)val {
for ( int i=0; i<[self count]; i++ ) {
// -----------
if ( val ) { // this IF block is very bad
if ( i != current && [self isCurrentPosition:i] )
[self replaceObjectAtIndex:i withObject:@""];
} else {
if ( i != current )
[self replaceObjectAtIndex:i withObject:@""];
}
// -----------
}
}Solution
the
You just must practise factoring out the common subexpressions and subprograms and learn how to use the Carnot maps for boolean minimization.
(val · ¬cur · pos) | (¬val · ¬cur) = ¬cur (val·pos | ¬val) = ¬cur (pos | ¬val). So, the condition is if ( (i != current) && (!val || [self isCurrentPosition:i]) ) {
[self replaceObjectAtIndex:i withObject:@""];
}You just must practise factoring out the common subexpressions and subprograms and learn how to use the Carnot maps for boolean minimization.
Code Snippets
if ( (i != current) && (!val || [self isCurrentPosition:i]) ) {
[self replaceObjectAtIndex:i withObject:@""];
}Context
StackExchange Code Review Q#19101, answer score: 5
Revisions (0)
No revisions yet.