Comment on coding style


#1

Page 186 in the hard copy 1st edition

It is telling the developer to create a static called ‘sharedImageCache’.

This also happens to be the exact same name of static method in the class.

In my C++ days I remember doing m_ or _ as a prefix for ‘this’ or ‘class instance’ data. It was also suggested good style to make any static class variable all upper case. I’ve also seen some shops use s_ to mean static.

If the code leverages pointers to functions it could get challenging to read ‘who’ is on first and ‘what’ is second. :slight_smile:

Below is what I did and hopefully nobody will be offended. At least they can’t tell by this post if I’m using TABS for indenting. I’m not telling anyone what I’m doing for that either. :wink:

[code]//
// ImageCache.m
// Homepwner
//
// Created by Ed on 10/14/10.
// Copyright 2010 Sincerus Technologies LLC. All rights reserved.
//

#import “ImageCache.h”

static ImageCache *SHARED_IMAGE_CACHE;

@implementation ImageCache

-(id)init {

[super init];

dictionary = [[NSMutableDictionary alloc] init];
return self;

}

#pragma mark -
#pragma mark Singleton methods

+(ImageCache *)sharedImageCache {

if (!SHARED_IMAGE_CACHE) {
    SHARED_IMAGE_CACHE = [[ImageCache alloc] init];
}
return SHARED_IMAGE_CACHE;

}

+(id)allocWithZone:(NSZone *)zone {

if (!SHARED_IMAGE_CACHE) {
    SHARED_IMAGE_CACHE = [super allocWithZone:zone];
    return SHARED_IMAGE_CACHE;
} else {
    return nil;
}

}[/code]

This is a great book. Thank you for your efforts.

Ed