by Cesare Rocchi

On Naming Macros in Objective-C

by Cesare Rocchi

Right after my latest post on naming methods with blocks my friend Murray asked for some feedback on the names for his collection of macros. Without even blinking I suggested ALL_CAPS, having in mind mostly the following macro by Apple

UI_USER_INTERFACE_IDIOM()

I think it’s pretty ugly to read but I guess it’s meant call out the fact it’s a macro. If you check UIDevice.h though, you’ll find the following:

#define UI_USER_INTERFACE_IDIOM() ...

#define UIDeviceOrientationIsPortrait(orientation) ... #define UIDeviceOrientationIsLandscape(orientation) ...

WTH?

The first is all caps and the subsequent two are named like classes. Again, not even Apple is consistent in naming. So far I have seen many styles in different projects:

  • UA_invalidateTimer
  • NSStringFromBool
  • IS_IPHONE

My take? The first looks like a method in a category and the second looks like a class or a C function. I’d stick with all caps, ugly to read (and type) but at least it clearly says “I am a macro”. And you? How do you name your macros?

Get in touch on Twitter or App.net if you like.