Skip navigation.
 
mlRe: Why is [nil aMessage] a no-op?
FROM : Rob Napier
DATE : Fri Apr 18 18:19:17 2008

On Apr 18, 2008, at 9:56 AM, Adam P Jenkins wrote:
>
> On Apr 18, 2008, at 1:16 AM, Graham Cox wrote:

>>
>> Here's a simple example:
>>
>> - (void) dealloc
>> {
>>  [someIvar release];
>>  [super dealloc];
>> }
>>
>> is <someIvar> really initialised? Maybe it's nil? Do I care at this 
>> point? no - either way, the code is correct - if the object was 
>> made, it's released, if it wasn't, it's a no-op. Exactly what you 
>> want.

>
> That makes a lot of sense.  I can now picture many lines of code 
> I've written over the years which wouldn't have been necessary with 
> this feature.
>
> Thanks a lot to everyone who responded to my question.  I now 
> understand the pros and cons of the nil-eats-messages feature much 
> better, and it doesn't seem like a mis-feature to me now.



In Martin Fowler's "Refactoring: Improving the Design of Existing 
Code", even as a Java-centric writer he discusses the utility of the 
Null Object (the strict-typing version of the nil-eats-message 
pattern). Some background he quotes from Ron Jeffries (of Extreme 
Programming fame):

---
   We first started using the null object pattern when Rich Garzaniti 
found that lots of code in the system would check objects for presence 
before sending a message to the object. We might ask an object for its 
person, then ask the result whether it was null. If the object was 
present, we would ask it for its rate. We were doing this in several 
places, and the resulting duplicate code was getting annoying.
   So we implemented a missing-person object that answered a zero rate 
(we call our null objects missing objects). Soon missing person knew a 
lot of methods, such as rate. Now we have more than 80 null-object 
classes.
...
   An interesting characteristic of using null objects is that things 
almost never blow up. Because the null object responds to all the same 
messages as a real one, the system generally behaves normally. This 
can sometimes make it difficult to detect or find a problem, because 
nothing ever breaks. Of course, as soon as you begin inspecting the 
objects, you'll find the null object somewhere where it shouldn't be.
---

Like ObjC memory management, nil-eats-message is something that new 
ObjC programmers rail against (I know I did), but that experienced 
ObjC programmers grow comfortable with and eventually quite fond of. 
It doesn't make it the one-and-only "right way," but it demonstrates 
that it's generally more useful than it is dangerous.

-Rob

Related mailsAuthorDate
mlWhy is [nil aMessage] a no-op? Adam P Jenkins Apr 18, 00:42
mlRe: Why is [nil aMessage] a no-op? Nick Zitzmann Apr 18, 01:00
mlRe: Why is [nil aMessage] a no-op? Bill Bumgarner Apr 18, 01:07
mlRe: Why is [nil aMessage] a no-op? Jeff Apr 18, 02:46
mlRe: Why is [nil aMessage] a no-op? Graham Cox Apr 18, 03:19
mlRe: Why is [nil aMessage] a no-op? David Wilson Apr 18, 03:51
mlRe: Why is [nil aMessage] a no-op? Michael Ash Apr 18, 04:13
mlRe: Why is [nil aMessage] a no-op? Adam P Jenkins Apr 18, 06:20
mlRe: Why is [nil aMessage] a no-op? Bill Bumgarner Apr 18, 06:47
mlRe: Why is [nil aMessage] a no-op? Adam P Jenkins Apr 18, 06:56
mlRe: Why is [nil aMessage] a no-op? Adam P Jenkins Apr 18, 07:01
mlRe: Why is [nil aMessage] a no-op? Graham Cox Apr 18, 07:16
mlRe: Why is [nil aMessage] a no-op? Michael Vannorsdel Apr 18, 07:21
mlRe: Why is [nil aMessage] a no-op? Bill Bumgarner Apr 18, 07:46
mlRe: Why is [nil aMessage] a no-op? Sherm Pendley Apr 18, 07:52
mlRe: Why is [nil aMessage] a no-op? Jens Alfke Apr 18, 08:13
mlRe: Why is [nil aMessage] a no-op? Gerriet M. Denkman… Apr 18, 10:32
mlRe: Why is [nil aMessage] a no-op? ab_lists Apr 18, 10:56
mlRe: Why is [nil aMessage] a no-op? Citizen Apr 18, 15:22
mlRe: Why is [nil aMessage] a no-op? Adam P Jenkins Apr 18, 15:56
mlRe: Why is [nil aMessage] a no-op? Scott Ribe Apr 18, 16:49
mlRe: Why is [nil aMessage] a no-op? Andy Lee Apr 18, 17:18
mlRe: Why is [nil aMessage] a no-op? Rob Napier Apr 18, 18:19