Sending messages to nil


Hey Joe,

I was reading the second chapter of your book and came across the part where you mention that we can just send messages to objects without nil-checks; it came off as if you were suggesting we should adopt this style of programming. As developers, we are all naturally lazy; we love to automate so that we don’t have to do rudimentary tasks, however I have to disagree with this statement.

I’ve always learned that the programs we write should be as lean as possible, and that good programming standards should be followed. Being new to iOS, I’m not sure if it’s the case as it is with other languages, but it seems to me that checking a value to make sure it’s nil would be operationally lighter and faster than sending a message to an object, and then waiting for a response; even if that object is nil.

I would argue that prospective developers should be advised to follow good practices and always check to make sure that the items they are passing, and objects they are using are valid, before using them. This would be a great lesson to impart to people who would potentially go to other languages that would crash if said precautions were not taken.

Any thoughts on the matter? Please correct me if I’m wrong on the speed comment.

Thanks for your time,



Don’t want to speak for any of the Ranchers, but I do not believe that speed is an issue here.

If the object were a proxy to a remote system, absolutely, you run the risk of a message to nowhere and a delay.

However, here we’re talking about code that’s compiled to binary form to be run on the local system, and I’d bet a fair bit of change (at least 50¢!) that the underlying machine code is very similar to the if construct you describe.

Whether code correctness is encouraged or discouraged by this practice is an entirely different matter, and one to which I’m not qualified to speak.