2
Jul
2008

Objective C, readability, and language design

For the last several months, I’ve been teaching myself Objective C and Cocoa. On the plus side, the framework is well designed. It’s the Objective C syntax that still has me tearing my hair out.

Square brackets

The biggest difference between Objective C and C++/Java/C#/ActionScript is the syntax for sending messages to objects. Instead of using dots or arrows, you use square brackets:

ActionScript / JavaScript
foo=object.method()

Objective C
foo=[object method]

Looking at it like this, the syntax doesn’t look half bad. The problem comes when you start chaining these methods together.

Let’s say you want to get the screen bounds of the window that holds a view object:

NSRect screenRect = [[[myView window] screen] frame]

Reading this syntax isn’t bad, but writing it is a pain. Every time you want to add a new method call at the end, you have to go all the way to the beginning of the line to add an open square bracket.

Long names

To compound matters, most of the method names are much longer than above:

sortedDocuments = [[[documents allKeys] filteredArrayUsingPredicate:predicate] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

By shortening some names (e.g., filteredArrayUsingPredicate -> filter) and using dots and parentheses instead of square brackets, you might end up with something like this:

sortedDocuments = documents.keys.filter(predicate).sort(caseInsensitive);

Strings and collections

Some languages, such as C# and ActionScript define string classes as an integral part of the language. This allows those languages to add common operations like string concatenation directly into the language. Others, like C++, do not define strings directly in the language, but allow class designers to override operators to, in effect, extend the syntax of the language.

In Objective C, string objects are only barely integrated into the language. The only concession to usability is that there is a syntax to define string literals (@”xxx”).

In order to concatenate strings, you end up doing something like this:

ActionScript / JavaScript
"The file " + filename + " could not be found."

Objective C
[[@"The file " stringByAppendingString:filename] stringByAppendingString: @” could not be found.”]

The same is true of collection classes, such as arrays and hash tables.

Objective C
[wordMap setObject:[words objectAtIndex:i] forKey:key]

ActionScript / JavaScript
wordMap[key] = words[i]

Frankenstein - Not 100% object-oriented

Like all of the C-based languages, there are weird cracks in the language where the object-oriented and non-OO worlds meet. In Objective C, you can define structs and objects, which work totally differently. You can define both functions (denoted by parentheses) and methods (denoted by square brackets). To confuse matters further, Objective C 2.0 defines getters and setters, which are accessed using dot notation, even though the implementation is done through methods, which normally don’t use dots.

One of the classic ways in which the OO and non-OO worlds meet is in dealing with primitive numeric types. Should an int be represented in the most efficient way possible (e.g., 4 bytes of raw data), or should it be a full-fledged object, so it can be used in places that expect objects (e.g., collection classes).

Many languages provide both primitive and object versions of things like integers, and Objective C is no exception. Modern languages like Java 5 and C# simplify this through automatic boxing/unboxing, which means that these languages automatically convert between primitive types (e.g., int) and the object types (e.g., Integer).

Here is a line of actual code that I wrote the other day:

Objective C
[dict setObject:[NSNumber numberWithInt:prio++] forKey:[NSNumber numberWithInt:matcher.type]]

Now you might look at that line of code and say that it is too complicated to put on a single line. Well, if the language (a) had built-in hashes, and (b) did auto-boxing/unboxing, the above line would look like this:

Hypothetical Objective C with hashes and boxing/unboxing
dict[matcher.type] = prio++

Syntax is important

From a mathematical point of view, the specific syntactic choices are not all that important in defining what a language can do. But from a human point of view, the purpose of a language is to make it easy to express and communicate ideas. Computer programs are hard enough to read and write as it is. To the extent that a syntax makes it hard to express ideas clearly, it makes the task of programming harder than it needs to be.

For those of you who like Objective C, I know… I know… every language has its warts. I spent a good chunk of my life working in C++, and I know that a badly written C++ program is as hard to read as the worst Perl program.

To be fair to the other side, maybe the issue is that I know the warts of C++, whereas I am only now learning new warts in Objective C. So maybe I’m just overreacting when I tear my hair out and throw things across the room.

Yeah, right.

14
Jun
2008

Aquavit deathwatch?

Last night, jean and I went to Aquavit in Manhattan. The food was phenomenal. The service was great. The decor was good. So why was the place more than half empty at 9pm on a friday night?

There is a sadness to eating delicious food in a half empty restaurant, which is preferable to the indignation I feel when eating lousy food in a crowded restaurant. (Actually, let me correct that. Half empty restaurants don’t have to be sad. The most joyful experience is to eat great food at a place that is half empty because it hasn’t been discovered yet. Why is that? Is it the snobbish joy of being “in”?)

The whole experience left us shaking our heads wondering what went wrong. Are new yorkers just over the herring and lingonberries? Is itthe lack of foam and xanthan gum? To be fair, the place has been arpund for 20 years and maybe people just want something new.

Was it worth it? I think so. For us San Francisco hicks, a chance to eat upscale Swedish like this is a treat. All of the dishes were great, and some were mind-blowing. One of the standout dishes was a combination of foie gras, duck confit covered in crisped rice, arugula sorbet, and apple puree.

Tonight, we eat at Degustation, which, being a 16 seat restaurant, had better not be half empty!

10
Jun
2008

Ack! I’ve been blocked by Google! (and what to do about it)

A few weeks ago, I got an email from Google saying that my site was full of spam, and that my site was being removed from the indexes. The email contains a sample of the spam words (in my case, it was viagra, cialis, etc) Sure enough, it turned out that my blog had been hacked to include lots of words/links that were made invisible via CSS. Pretty distressing stuff.

For those of you in the same boat, here’s what I did to remedy the situation:

1) Most of the damage was in the form of obfuscated code that made use of base64_decode(xxxx). To find this code, go to the root of your site and do a grep base64_decode -Rl ./* It should be pretty easy to use your judgment about what code to remove. This code was inserted into my theme files (found in wp-content/themes/ThemeName).

2) To be extra sure, do a Google search for the offending words on your site. (e.g., viagra site:kuwamoto.org). For me, this turned up another problem with the site, which was that URLs of the form http://kuwamoto.org/?aff=1234 were being redirected to a different site (selling pharmaceuticals, natch). This code wasn’t obfuscated with base64, so I didn’t catch it in step 1. In my case, it was an extra file, so I just blew it away.

3) Look through your posts, pages, comments, etc. In my case, one of the links on my blogroll had been compromised.

4) Upgrade your WordPress installation. As recommended in the WordPress documentation, I used the automated upgrade plugin which worked like a charm.

5) Follow the recommendations at http://www.noupe.com/how-tos/wordpress-security-tips-and-hacks.html and http://sitening.com/blog/2008/04/08/wordpress-security-vulnerabilities/.

6) If you are using a hand-rolled theme (as I was), make a copy of it somewhere so it is easier to restore if it ever gets hacked again.

7) Change your passwords, and make a promise to yourself to be good from now on (keep WP and other software updated to the latest version, use SFTP instead of FTP, etc).

23
May
2008

Long overdue update

Finally updated my bio. It has been… what… eight months since I left Adobe? Man.. time flies!

23
May
2008

The San Francisco Chicken Emergency

I know I haven’t posted in a long time, but something kind of funny happened recently and I couldn’t resist.

Ever since leaving Adobe, I’ve been tinkering around with various projects at home. In the mornings, I usually sit on my downstairs deck, drink my coffee, and work on my laptop. The other day, I went down to my usual hangout to find…

Hmm…. is this animal dung? A cat? No.. A bird? Maybe a big bird…

Well, it turns out our neighbors have bought chickens and have not had their wings clipped yet. I snapped a photo of one of the chickens hanging out at my favorite morning spot. (The broom and hose are for cleaning up the chicken poop).

I kind of like the thought of having chickens in our neighborhood, but I was worried our neighbor was going to get in some kind of trouble. After doing a bit of research, it turns out that it is legal to own chickens in San Francisco as long as you have fewer than four.

Who knew?