Re: Constants in Cocoa [Was Re: Magic Numbers]
Re: Constants in Cocoa [Was Re: Magic Numbers]
- Subject: Re: Constants in Cocoa [Was Re: Magic Numbers]
- From: Marco Scheurer <email@hidden>
- Date: Sat, 9 Nov 2002 17:06:32 +0100
From: Esteban Uribe <email@hidden>
So you could do something like:
#define kDefaultFontSize 20
NSString *EUHelveticaBold = @"Helvetica-Bold";
NSFont *EUHelveticaBold20Font = [NSFont fontWithName:EUHelveticaBold
size: kDefaultFontSize];
Frankly, I don't see the advantage of using constants named like this
instead of the literal. If you decide to define constants, use names
that will give the intent:
#define DefaultFontSize 20
const NSString *DefaultFontname = @"Helvetica-Bold";
NSFont *defaultFont = [NSFont fontWithName: DefaultFontname size:
DefaultFontSize];
What you suggested is not far from:
#define ONE 1
which is a dumb application of the "do not use literals in your code"
programming guideline. (And in addition to that, EUHelveticaBold20Font
is wrong if you change kDefaultFontSize to 14.)
Another way of defining constants is to use methods. The advantage of
methods is flexibility: you can change the implementation easily (for
instance in that case to use a NSUserDefaults value instead of a
literal), they can be overriden...
- (NSString *) defaultFontName
{
return @"Helvetica-Bold";
}
Marco Scheurer
Sen:te, Lausanne, Switzerland
http://www.sente.ch
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.