More and more when I write code, I’m looking for optimizations, short cuts and less typing. This can mean a careful balance between maintainable code and author convenience. Here’s an example of something I use in every program I write.
I use the preprocessor to define debug modes for myself to produce debug output under certain conditions. Here I define DEBUG_ON to produce a boolean value that can be tested throughout my program.
ANSI C Version
Objective C Version
This can save a lot of redundant typing/pasting and reduces code eyestrain.
e.g. This…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
static NSString *cellIdentifier = @"accessCell";
DEBUG_F_L
Accounts *account = [self.arrAccounts :indexPath.row];
LocationCollectionViewCell3 *cell
= (LocationCollectionViewCell3 *)[collectionView :cellIdentifier :indexPath];
if (Nil == cell) {
DEBUG_F_L
cell = [[LocationCollectionViewCell3 ] ];
}
cell.orgNameLabel.text = account.org_display_name;
cell.locationNameLabel.text = account.location_name;
cell.locationCityLabel.text = account.location_city;
cell.actionQtyLabel.text = @"";
[cell.backgroundImage :[NSURL :account.org_login_logo_url]];
DEBUG_F_L
vs. this…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
static NSString *cellIdentifier = @"accessCell";
// NSLog(@"%s \t\tline: %d", __FUNCTION__, __LINE__);
Accounts *account = [self.arrAccounts :indexPath.row];
LocationCollectionViewCell3 *cell
= (LocationCollectionViewCell3 *)[collectionView :cellIdentifier :indexPath];
if (Nil == cell) {
// NSLog(@"%s \t\tline: %d", __FUNCTION__, __LINE__);
cell = [[LocationCollectionViewCell3 ] ];
}
cell.orgNameLabel.text = account.org_display_name;
cell.locationNameLabel.text = account.location_name;
cell.locationCityLabel.text = account.location_city;
cell.actionQtyLabel.text = @"";
[cell.backgroundImage :[NSURL :account.org_login_logo_url]];
// NSLog(@"%s \t\tline: %d", __FUNCTION__, __LINE__);
Discussion
No comments for “C Preprocessor Life Saver(s) – Part 1”