I've got this code that I found in a project and I'm wondering how can it be done better:- (void) setAlpha:(float)alpha {    if (self.superview.tag == noDisableVerticalScrollTag) {        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleLeftMargin) {            if (self.frame.size.width < 10 && self.frame.size.height > self.frame.size.width) {                UIScrollView *sc = (UIScrollView*)self.superview;                if (sc.frame.size.height < sc.contentSize.height) {                    return;                }            }        }    }    if (self.superview.tag == noDisableHorizontalScrollTag) {        if (alpha == 0 && self.autoresizingMask == UIViewAutoresizingFlexibleTopMargin) {            if (self.frame.size.height < 10 && self.frame.size.height < self.frame.size.width) {                UIScrollView *sc = (UIScrollView*)self.superview;                if (sc.frame.size.width < sc.contentSize.width) {                    return;                }            }        }    }    [super setAlpha:alpha];}
iOS/Objective-C if best practice
objective c;ios
First of all, using the tag to distinguish between views is almost everytime (there are exceptions) a bad idea. It's much cleaner to use your own variable (pointers to view, BOOL flags).10 is a magic number. What does it mean? Use a constant.Use methods. Four levels of if are terrible. Extract the code into methods.if ([self isVerticalScrollingDisabled] || [self isHorizontalScrollingDisabled]) {    return;}[super setAlpha:alpha];By the way, changing the behavior of a method in a subclass (the method sets alpha only if some conditions are valid) is smelly from architecture point of view. Declaring a special method, e.g. setAlphaIfScrollingEnabled: would be better.