Tuesday, September 11, 2012

Worse Development Anti-Pattern I've Ever Seen: Using Goto in Objective-C

This is actual code I was reviewing a few months ago. I've anonymized the code and did a bit of renaming... 
- (BOOL)aTerribleMethod:(NSString *)iDontKnowWhatImDoing
{
    if(iDontKnowWhatImDoing isEqualToString:@"YES") {
        [self callAnotherTerribleMethod];
        goto bail;
    }

    if(iDontKnowWhatImDoing isEqualToString:@"SOMETIMES") {
        [self callYetAnotherTerribleMethod];
        goto bail;
    }

    if(iDontKnowWhatImDoing isEqualToString:@"NO") {
        goto bail;
    }

    //Do a whole bunch of other stuff

    bail:
}
If you've ever written something like this, please quit programming.

Monday, September 10, 2012

LLDB Custom Object Summaries Implemented Using Python, not Objective-C

In the seemingly benignly titled WWDC 2012 session Debugging in Xcode, the speaker team shows you how to implement custom summaries on your own objects for LLDB. This is pretty important since LLDB is now the recommended default debugger as of Xcode 4.3 (Xcode 4.4.1 is the latest at the time of this writing, with Xcode 4.5 just days away).

What a second, what's wrong with implementing [ClassInstance description]? The speaker sternly says it's an action fraught with danger since you might run code and change the state of the object inside description. I admit, I never really thought about it much. But one of the side affects calling description caused was seeing the dreaded Summary Unavailable next to your variable where its description should be. When implementing LLDB, Apple choose another way to get custom summaries which really surprised me.

For LLDB, you create a Python script that directly inspects member variables of your class, then you use Python code to do any summarization, formatting, or transformation, and finally return a Python string. The video shows exactly how this works, and of course you need to register a script and function in the script to call when LLDB sees your Objective-C class.

I'd never seen Python before. It doesn't look radically hard to pick up, and seeing it used for LLDB sure made me wonder if it will have a more prominent role at Apple in the future. Things that make you go hmmmm…

Friday, September 07, 2012

My Favorite Unadvertised OS X 10.8 Mountain Lion Feature

Notification Center is without a doubt my favorite Mountain Lion feature, particularly the way Calendar reminders go from moderately disruptive to not disruptive at all.

My favorite unadvertised feature is that you can now add files to the Trash while it's emptying! I know, it's the kind of thing that seems like a no-brainer/should have been there for years, but it wasn't.

This is great for me since emptying build folders, or large batches of icons takes a few seconds, and I frequently find something else to trash after I've already started emptying the trash.