Hi, Dave

On 2 Apr 2010, at 22:48, Dave wrote:

Hi All,

I came across this code and was wondering if this is normal practice and/or considered good coding style.

Given two classes ClassX and ClassY:

@interface ClassX
{
ClassY  mClassY;
}

@property (nonatomic,retain,readonly)           ClassY*         mClassY
@end

@interface ClassY
{
int             mClassYValue;
}

@property (nonatomic,retain,readonly)           ClassY*         mClassY

@end

@implementation ClassX

-(ClassY*) mClassY
{
if (mClassY == nil)
        {
        mClassY [[ClassY alloc] initWithData:someData]:
        }
return mClassY;
}


Then later in another method of ClassX, I found the following code:

self.mClassY. mClassYValue = someValue;

Before this statement is executed, self. mClassY is nil. When it is executed it causes the "mClassY" method to be called which allocates the Class and sets the Variable.

There are some typing errors in this code, I think, but anyway, this might be a case of lazy creation of an object inside a getter of another object. You will find the same e.g. in Apple's code examples about OpenGL, where the OpenGL context is an instance variable of the OpenGL view. Accessing the context is managed by a getter method or property, respectively, of the view, where, in the case of the instance variable being nil, the context is created, stored in the instance variable, and eventually returned. I call the creation "lazy", because it is done at the latest point during execution, immediately before it is needed. For me this practice is normal.

But, may be, I don't understand your question correctly.

regards
Klaus

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to