Constants in Cocoa [Was Re: Magic Numbers]
Constants in Cocoa [Was Re: Magic Numbers]
- Subject: Constants in Cocoa [Was Re: Magic Numbers]
- From: Esteban Uribe <email@hidden>
- Date: Fri, 8 Nov 2002 08:57:30 -0800
On Thursday, November 7, 2002, at 09:55 AM, James Hober wrote:
What is the best way to handle magic numbers in Objective-C?
By the way, from reading the headline I thought you were talking about
Magic numbers in Magic Squares.
So I was confused for a second, but after closely reading your e-mail
made me realize something else.
I'm not familiar with the term "Magic Numbers" in the way you are using
it, but I am with "Constants" :)
With this in mind :)
Using #define seems common. However, its lack of type checking and
its use of the
entire app's namespace seem to be drawbacks.
Well as I understand it #define is a compiler directive, in which the
compiler will replace #define label
with the code or value you set, at compilation time. Also if I
remember correctly it is only available in the source
or header file you define it in unless you #include or #import it into
another header or source file.
Using an enum is common and useful for ints. You can stick it between
@interface
and @end and its scope is limited to the .h and .m file. But its use
is limited to
ints. Also it is public, so enum changes may break client code that
depends on a
particular enum value.
I usually use #define or enumeration to label some numeric constants
that I will use in my apps.
Another way is:
static inline int MY_CONSTANT {return 4;}
which goes outside of the @interface and @end, and allows the compiler
check the
type.
Use of the keyword "const" is possible but doesn't seem nearly as
useful in
Objective-C as it is in C++.
You can't really use the const keyword in Objective-C but you do have
mutable and Immutable types
For string identifiers, I've seen Cocoa use something like the following
NSString *MYConstString = @"MyConstString";
You'll note Attribute names follow this convention.
Particularly difficult are constants that you would like to define at
the top of an
implementation file but that depend on the dynamic nature of Cocoa,
for example
defining a particular NSFont. Code like:
NSFont *resultFont = [NSFont fontWithName: @"Helvetica-Bold" size: 20];
seems to always want to go inside of a method body, rather than to be
somehow
broken out and put at the top of the file so that it can be changed
easily.
Well if you want that to be a "constant" of sorts I would suggest
choosing a more explicit name
and capitalizing the variable name. Also if you do capitalize the
variable name don't append it with NS
as that prefix is for Cocoa only stuff.
So you could do something like:
#define kDefaultFontSize 20
NSString *EUHelveticaBold = @"Helvetica-Bold";
NSFont *EUHelveticaBold20Font = [NSFont fontWithName:EUHelveticaBold
size: kDefaultFontSize];
(EU being the initials of my name...though they could be initials
specific to your app)
And yes I'm using the old Mac style convention for numeric constants
(prefix of small k)
Though making the numeric constant all CAPS will probably be better,
especially if you plan to hand your code to some other programmers in
the future.
Finally, I'm sure this has been discussed before, you may try doing a
search through
http://cocoa.mamasam.com
-Esteban
_______________________________________________
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.