• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Constants in Cocoa [Was Re: Magic Numbers]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Constants in Cocoa [Was Re: Magic Numbers] (From: Esteban Uribe <email@hidden>)

  • Prev by Date: Re: cocoa-dev digest, Vol 2 #1502 - 13 msgs
  • Next by Date: Path and new folders
  • Previous by thread: Constants in Cocoa [Was Re: Magic Numbers]
  • Next by thread: Problem with dynamic reconfiguration of a TableView?
  • Index(es):
    • Date
    • Thread