title: Best practices for local constants in objective-c
body: <p>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 <a href="http://developer.apple.com/iphone/library/samplecode/TableViewSuite/index.html" rel="nofollow noreferrer" title="TableViewSuite">TableViewSuite</a> example 5, the drawRect function in TimeZoneView.m contains the following block:</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>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?</p>
  
  <p>That's my question I suppose:</p>
  
  <p><strong>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?</strong></p>