Andy's Laboratory

Friday, December 29, 2006

 

Syntactic Sugars

I have a sweet tooth for syntactic sugar. I love things like mystring[-1] in Python. Just like real world sugar, too much syntactic sugar can ruin the health of a programmer, but in moderation, it's a wonderful thing.

Naturally, I was overjoyed to learn that Objective-C 2.0 in Leopard will bring property accessors and foreach style loops. This is all good except that in order to take advantage of these 1999 technologies you have to target Leopard. While not a problem for people developing new applications, it's a huge tease for the rest of us.

So what's a programmer to do? Well, at Potion Factory we actually have had accessors and foreach loops for a while now. The solution involves lots of macros and it's of course nothing like what you get with Obj-C 2.0, but it still saves us an enormous amount of typing.

Foreach style loops


After having programmed in Python for a while, I found the following enumeration idiom in Objective-C to be rather painful, to put it mildly.

NSEnumerator *e = [collection objectEnumerator];
id obj;
while (obj = [e nextObject]) {
// do stuff with obj
}

I can't believe it's not butter... or Java...

Luckily I found the following foreach macro somewhere in the cocoa newsgroup.
#define foreach(element, collection) \
for (id _##element##_enumerator = [collection objectEnumerator], element; \
element = [_##element##_enumerator nextObject]; )

It simplifies the enumeration down to the following bit of code
foreach (obj, collection) {
// do stuff with obj
}

More than just taking out two lines of code, this drastically improves the readability., It also saves you from typing the same thing mindlessly over and over throughout the day.

Later on I found out that the foreach macro has been discussed by Jonathan Rentzsch, Michael Tsai, and Wincent Colaiuta. Jonathan has a type-safe version where you specify the object type in the loop to get compiler warnings and Michael's implementation does IMP-caching to speed up calls to -nextObject.

Accessors


// For interface

#define GETTER_H(type, attr, capattr) \
- (type)attr;

#define SETTER_H(type, attr, capattr) \
- (void)set##capattr:(type)value;

#define ACCESSORS_H(type, attr, capattr) \
GETTER_H(type, attr, capattr) \
SETTER_H(type, attr, capattr)

// For implementation

#define MYASSIGN(old, new) \
if (old != new) { \
[new retain]; \
[old release]; \
old = new; }

#define GETTER(type, attr, capattr) \
- (type)attr { return m##capattr; }

#define SETTER(type, attr, capattr) \
- (void)set##capattr:(type)value { MYASSIGN(m##capattr,value); }

#define ACCESSORS(type, attr, capattr) \
GETTER(type, attr, capattr) \
SETTER(type, attr, capattr)

The macros above allows us to define and implement our accessors the following way:
@interface Person : NSObject {
NSString *mName;
}
ACCESSORS_H(NSString*, name, Name);
@end

@implementation Person
ACCESSORS(NSString*, name, Name);
@end

We use various flavors of the above macro and it allows us to develop our applications much faster. Again, it improves readability and eliminates much typing. The (f)ugly part is having to type the variable name in both lowercase and capitalized forms. If you know how that can be avoided, please let me know.

The time savings become quite significant when you apply the idea to Core Data's managed object subclasses.

ManagedObjectAccessors.h

Labels: ,

Tuesday, December 26, 2006

 

Hello, World!

Ah, it's the end of the year. It's time for the festivities, wonderful gifts, getting drunk, and making resolutions (while drunk?) that usually last a month at best. My new year's resolution this year is becoming the most famous Andy Kim in the world. How the heck do you measure famousness? With Google pagerank of course. There are a lot of Andy Kims in the world, with 99.999% of them being of Korean descent, but right now the top position is held by the Canadian singer and song-writer famous for the 70's hits such as Baby I Love You and Sugar, Sugar. With this blog I intend to beat the singer to become the most famous me there is. I figure that if this doesn't work, I'll start uploading videos to YouTube as lonelykim15, but I am hoping that it doesn't have to come to that.

By the way, I do realize that this is juvenile and narcissistic, but before you pass judgement, just take a look at this photo and tell me that you would want that to be the first hit on your name search. In this day and age where people—say, a blind-date, for example—google your name, these things become important.

All jokes aside, here I will rant about stuff that interest me—such as, the Mac, programming on the Mac (with Cocoa mostly), running a Mac ISV called Potion Factory, stuff relating to design, and maybe even boring personal stuff from time to time.

Welcome to my blog and I hope to see you around.

Archives

December 2006   January 2007   June 2007  

Subscribe to posts RSS Feed

About Me

I am the co-founder of Potion Factory, a small Macintosh software company. I like things that are well designed.

See my complete profile

This page is powered by Blogger. Isn't yours?