Skip to main content
deleted 45 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Optimize IF condition block Cleaning a text field

this is my first post here!
HowHow can iI optimize the ifif condition onin this snippet?

  The only one difference is && [self isCurrentPosition:i].
How How can madeI make it on a single ifif, 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:@""];
    }
    // -----------

  }
}

Note: self is a category of NSArray.

thanks.

Optimize IF condition block

this is my first post here!
How can i optimize the if condition on this snippet?

  The only one difference is && [self isCurrentPosition:i].
How can made it on a single if, including the condition val?

- (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:@""];
    }
    // -----------

  }
}

Note: self is a category of NSArray.

thanks.

Cleaning a text field

How can I optimize the 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:@""];
    }
    // -----------

  }
}
Tweeted twitter.com/#!/StackCodeReview/status/273785133683847168
Source Link
elp
  • 193
  • 5

Optimize IF condition block

this is my first post here!
How can i optimize the if condition on this snippet?

The only one difference is && [self isCurrentPosition:i].
How can made it on a single if, including the condition val?

- (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:@""];
    }
    // -----------

  }
}

Note: self is a category of NSArray.

thanks.