Monday, July 15, 2013

Effective Objective C 2.0

Mark Bernstein on Matt Galloway’s Effective Objective C 2.0:

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.

I agree that there could be more discussion of key-value observing and concurrency. Also, Item 48 recommends block enumeration over for loops:

In the case of a dictionary, you get both the key and the value without any additional work, thereby saving the extra cycles required to obtain the value for a given key. Instead, the dictionary can give both at the same time, which is highly likely to be far more efficient, since keys and values will be stored together in a dictionary’s internal data structures.

This makes intuitive sense, but I seem to recall reading that, in practice, a loop with fast enumeration is faster (perhaps due to function-call overhead).

3 Comments RSS · Twitter

Think you're thinking of this old Mike Ash post.

@Clark No, there was one (more recent than that, I think) that actually timed the different versions, and the blocks one was slow. I think internally it may even have enumerated the keys and individually looked them up.

Even if slightly slower, the 'for' enumeration is easier to read, easier to write, and easier to debug. Making it a win most of the time, IMHO.

Leave a Comment