Wednesday, June 4, 2008

Unearthed! System Preferences Keyboard shortcuts

Last night, I was trying to shutdown my computer with an Apple Wireless keyboard. It has no power key so I was trying different shortcuts. Option-F12 brought up something interesting (F12 is not really F12, but the "volume up" command). Holding option with it brought up System Preferences and switched directly to the "Sound" preference. This works with most of the "special keys". Here's the list...

Sound -> option + <volume-up/down/mute>
Spaces -> option + <Dashboard/Spaces>
Display -> option + <Brightness Up/Down>

Friday, February 29, 2008

Overcome indentation in your init methods.

I really like the ideas in the Seven Pillars of Pretty Code paper at Perforce Software, especially the "Overcome Indentation" section. I try to keep my code as far to the left as is reasonable.

For instance:
if ( thing != NULL  )
{
thing->doSomething();
thing->doSomethingElse();
thing->doSomethingMore();
}

... can become ...
if ( !thing  )
return;

thing->doSomething();
thing->doSomethingElse();
thing->doSomethingMore();

... and ...
if (value == otherValue)
{
thing->doSomething();
}
else
{
thing->doSomethingElse();
}

... can become ...
if (value == otherValue)
{
thing->doSomething();
return;
}

thing->doSomethingElse();


This is why I cringe when I see this in Apple's tutorial documentation:
-(id)init
{
self = [super init];
if (self != nil) {
// initialization code
} else {
[self release];
return nil;
}
return self;
}

With this, I get to write my entire init method in two levels of indentation just so I can happily execute one return statement after the end paren.

This is also in some of XCode's class templates, and in many cocoa books that I see. I wish they would change it to this. It looks far calmer to me.

-(id)init
{
if ((self = [super init]) == nil )
{
[self release];
return nil;
}

// initialization code

return self;
}


I realize this is a small thing, but after looking over page after page of highly indented code, I get a little fatigued. The init method will likely be the first method beginning Objective-C coders see, and they will likely copy that style, never re-thinking how they indent code.