inessential by Brent Simmons

Cocoa style and category methods used just once

I used to create categories too often, and I’ve cut way back.

One of the rules I’ve given myself: if something is used just once, don’t make a category for it, just make it a static C function. Unless it’s big.

Here’s a case of big: I use base64 decoding in just one place, but I still have my base64 code in an NSData category. Just because it’s big, it’s not some small function.

Here’s a case of small: I had a category for NSAppleEventDescriptor with one method, to get a target descriptor for a running app, given the path to the app. This was used in exactly one place (where the send-to-weblog command is implemented).

So what I did was turn that method into a static C function, and I put it in the same file where it’s used. (Which allowed me to get rid of my NSAppleEventDescriptor category file.)

The benefits I get from that:

1. Less code to maintain. (And, in this case, two fewer files: one deleted .h and one deleted .m.)

2. The code appears where it’s used: no need to jump to it if I need to edit it.

My instincts are always to lower the number of lines of code (and number of files) I have to maintain, so I consider this a good rule. But I wonder what other folks do in this case.