Archive for September, 2007

BOOLing for Dollars

Sunday, September 30th, 2007

While we’re all aiting-way or-fay eopard-Lay, I’d like to share a pointer that I picked up while mugging a C library. (I have no idea what that means. It seemed witty when I wrote it.) As you know, I’m always ahead of the curve, setting the trends, framing the public discourse. Thus, I should add my 1.2 cents — the dollar is weak, and I’m a little short this month — on a hot topic discussed on the Cocoa-dev mailing list recently (in geological time, anyway): the use of the Objective-C BOOL type.

If you look in the header file /usr/include/objc/objc.h, you can see how BOOL is defined:

	typedef signed char		BOOL;
	// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
	// even if -funsigned-char is used.

	#define YES             (BOOL)1
	#define NO              (BOOL)0

A char type — e.g., char, signed char, unsigned char — is always one byte, i.e., sizeof(signed char) == 1, whereas in most implementations an int type is more than one byte. A byte standardly consists of 8 bits, or 12 nibbles. What happens to the extra bits if you convert an int into a BOOL? According to the wacky rules of C type conversion, the result is implementation-dependent. Many implementations simply throw away the highest bits. (Other implementations recycle them into information superhighway speed bumps.) As a consequence, it’s possible that myIntVar != 0 && (BOOL)myIntVar == NO.

Usually we don’t have to worry about this, because ‘boolean’ operators in C, such as == and !, always return 1 or 0. When we use bitwise operators, on the other hand, the problem does come into play. Suppose, for example, that we’re testing whether the option key is down. The method -[NSEvent modifierFlags] returns a bit field indicating the modifier keys that are pressed, and bit masks can be used to test for specific keys. Consider the following code; there are situations where doSomethingAfterEvent: does something, yet doSomethingElseAfterEvent: does nothing.

	-(void) doSomethingAfterEvent:(NSEvent *)anEvent
		if (anEvent)
			if ([anEvent modifierFlags] & NSAlternateKeyMask)
				[self doSomething];

	-(void) doSomethingElseAfterEvent:(NSEvent *)anEvent
		if (anEvent)
			BOOL shouldDoSomethingElse = [anEvent modifierFlags] & NSAlternateKeyMask;
			if (shouldDoSomethingElse)
				[self doSomethingElse];

It has been suggested on the mailing list that the type conversion could be handled by

	BOOL shouldDoSomethingElse = !!([anEvent modifierFlags] & NSAlternateKeyMask);


	BOOL shouldDoSomethingElse = ([anEvent modifierFlags] & NSAlternateKeyMask) != 0;

However, these approaches would only work for single-bit masks. What if we wanted to test both the option key and the shift key?

The point I wish to make here actually has little to do with the BOOL type. (Say what?!?) Bitwise operators are not boolean operators. A boolean operator only returns 1 or 0. A bitwise operator, in contrast, can return any bit field. The proper way to handle a bitmask is to test whether the resulting bit field has the desired value:

	unsigned int myMask = NSAlternateKeyMask | NSShiftKeyMask;
	BOOL isMyKeyComboPressed = ([anEvent modifierFlags] & myMask) == myMask;

Yes, I know Robot Chicken already covered this subject a ha-while ago. I didn’t really care for it.

WordPress Bug Fix Near Saturday: props lapcat

Saturday, September 22nd, 2007

The network has decided to renew my series for at least one episode. WordPress 2.3 is approaching release, and I’m pleased with the progress that it has made in supporting syndicated feeds. The patch I submitted to fix the Atom feed modification date bug has been committed to the trunk. RSS 2.0 feeds still don’t give modification dates, but that could be considered a personal preference. (As can RSS 2.0 itself. Of course, no one would prefer the worse to the better, as Socrates would say, if he were 2500 years old and spoke English.)

Other syndication bugs that have been fixed in the trunk include checking the last modified date of posts rather than comments for comments feeds, an obsolete Atom feed template, and checking the last modified date of unapproved comments for comments feeds. WordPress 2.3 should be a good update for feed readers such as you and feed readers such as Vienna.

Time to go. Joan Cusack is calling again.

Vienna 2.2: Twice as good

Sunday, September 16th, 2007

Sorry if this is old news for those of you who haven’t spent the last week like me, shopping for the perfect ring-tone: Vienna 2.2 has been officially released.

I admit that twice as good may be an exaggeration, because Vienna 2.1 was already pretty good, but Vienna 2.2 is even better. It incorporates the work of two new main developers — Evan Schoenberg of Adium fame and Michael Ströck of The Unofficial Apple Weblog fame — which makes twice as many! Therefore, we’ve definitively proved that throwing more programmers at a project is a smart idea, vindicating all management everywhere.

Take a look at the release notes to see what’s changed, and go to the downloads page to get a copy. I recommend that all Vienna users (as well as all non-Vienna users) start using Vienna 2.2. Our development efforts — including bug fixes — are now focused entirely on Vienna 2.2 and the newly branched Vienna 2.3. Vienna 2.1 is dead to me. (Unless Vienna 2.1 wins an award, of course. I’m already preparing my acceptance speech. Ed Asner, you really deserve this.)