Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for local constants in objective-c

I see a lot of objective-c code that just #defines local constants it needs, and then proceeds on its merry way. The problem is that, as far as I know, #defines aren't scoped. Many of this is in Apple's own example code. For example, in the TableViewSuite example 5, the drawRect function in TimeZoneView.m contains the following block:

#define LEFT_COLUMN_OFFSET 10
#define LEFT_COLUMN_WIDTH 130

#define MIDDLE_COLUMN_OFFSET 140
#define MIDDLE_COLUMN_WIDTH 110

#define RIGHT_COLUMN_OFFSET 270

#define UPPER_ROW_TOP 8
#define LOWER_ROW_TOP 34

#define MAIN_FONT_SIZE 18
#define MIN_MAIN_FONT_SIZE 16
#define SECONDARY_FONT_SIZE 12
#define MIN_SECONDARY_FONT_SIZE 10

Is there some reason I don't understand that this is not absurdly dangerous? At a very minimum, shouldn't we #undef these constants at the end of the function?

That's my question I suppose:

Is it a better practice to define what you need in the file you need it, and un-define it at the end? Or do you think it's better to just use static consts for this type of thing? Is there any performance penalty to using static consts, or is the compiler able to handle them just as efficiently as #define?

like image 843
DougW Avatar asked Feb 26 '10 22:02

DougW


People also ask

How do you declare a constant in Objective C?

Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well. The constants are treated just like regular variables except that their values cannot be modified after their definition.

What does #define do in Objective C?

Many Objective-C apps use the #define preprocessor macro. These preprocessor macros tells the compiler to replace the definition of the macro before compilation. My team is using these macros in our Objective-C code to define constants. Example: #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width.

Where do constants go in a project?

You define Project Constants on the 'Generate Documentation' dialog; they are then available to be inserted into all custom document report templates in the text, section headers, end-notes, page headers or page footers, using the 'Document Template Designer' context menu.

Where do you put constants in a class?

Constants that are specific to a class should go in that class's interface.


1 Answers

#defines in implementation files (.m) are by definition scoped to the file they're in, since no one else #includes a .m file. (You do want to think carefully about this in common header files, where the scoping issue you mention is real, and SO_QUESTION_2345197_NAMESPACE_YOUR_CONSTANTS_APPROPRIATELY.)

For local constants in implementation files which is what you seem to be asking about, #define is more efficient to compile, but you don't get the symbols when you debug. Local consts have that benefit, and in some cases (string constants? maybe? depends) prevent duplication of constant data in the binary, although at this point in the world, size and compile efficiency (and runtime efficiency to look them up) is basically noise unless you profile some tight loop and find an issue with it.

like image 98
Ben Zotto Avatar answered Oct 19 '22 03:10

Ben Zotto