HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

NSAssert or NSLog in defaults switch case

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
casedefaultsnsassertnslogswitch

Problem

I am wondering if I should use NSLog(@"Switch out of range") or NSAssert(FALSE,@"Switch out of range"); in the following example:

typedef enum {
    kSectionFavoriteCarpark,
    kSectionPromotions,
    SectionsEnumCount
} SectionsEnum;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case kSectionFavoriteCarpark:
            return self.locationFavoritesKeyArr.count;
        case kSectionPromotions:
            return self.promotionCarparks.count;
        default:
            DLog(@"index out of switch range");
            return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.promotionCarparks.count > 0) {
        return SectionsEnumCount;//one for user defined positions one for favorite parkings
    }
    else {
        return SectionsEnumCount-1;
    }
}

Solution

Assertions are for declaring facts that you know to be true, not for conditions that you hope to be true. If this method is only ever called by your own code, then it makes sense to assert. On the other hand, if this is a public method, an assertion would not be an appropriate way to perform what is essentially parameter validation.

Context

StackExchange Code Review Q#69817, answer score: 5

Revisions (0)

No revisions yet.