Effective Objective C 2.0
by Matt Galloway
This book is organized and presented as an Objective C homage to Scott Meyers’ classic work, Effective C++. The new book is good and interesting and provides useful tips for working in Objective C, but it’s far less interesting than Meyers.
What made Meyers’ book both necessary and important was the subtlety and indirection of modern C++. Over the years, C++ gradually developed a family of practices and customs that are not obvious, even to experienced programmers. In particular, const-correctness and exception safety are pervasive and essential to everyday C++, they’re fundamentally different from other languages, they’re easy to get wrong, and once you do get stuff wrong in your code, the blight spreads rapidly. C++ style requires that, if class A publicly inherits from B, then B is a kind of A; this convention contradicts everyday usage in other familiar languages. Some facilities — run-time introspection, ubiquitous type conversion — turn out to be good things to avoid even though they seem inviting, and others, such as preferring C++ streams to C’s
printf, are worth adopting despite their initial learning curve.
There’s some of this in Objective C as well, but the focus in Effective Objective C 2.0 rests on syntax and semantics. Much of this is good and almost all is completely sound, but I think these tips miss many real issues in writing modern Objective C effectively. Some of these include:
- Key-value observing, its use, and its discontents
- The contested morality of adding new categories to standard objects
- The contested utility of introspection
- Pervasive use of task queues for managing concurrency
- Block idioms as a replacement for C++ stack-based resource management
- Design conflict between using delegates , requesting notification, and passing blocks
- Design conflict between using one broad protocol or several narrow protocols
- Desirability or otherwise of macros, especially in unit testing
Of greatest concern, I think, is the sparse discussion of concurrency. Sure, NSOperation and Grand Central Dispatch are great. But we have thousands of programmers now, all launching concurrent tasks left and right, who never studied concurrency formally and who don’t know a Petri net from a Petri dish. They need constructs and guidelines to keep them from stumbling into thickets of confusion when they’re just trying to get stuff done.