Skip navigation.
 
mlRe: Why is [nil aMessage] a no-op?
FROM : Scott Ribe
DATE : Fri Apr 18 16:49:22 2008

> Here's a simple example:
>
> - (void) dealloc
> {
>    [someIvar release];
>    [super dealloc];
> }


It's worth noting that even in C++ there's a special case for delete, so
that one can write "delete myptr" rather than "if(myptr) delete myptr)" 10
bazillion[1] times. I was around when this behavior was standardized, and
there were people arguing strenuously that this would support "sloppy
programming", whereas my opinion is that writing redundant code
unnecessarily is what is sloppy.

> 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.


Here's an example from my own use. Imagine a "workflow" type application.
You'd have some kind of big chunk of structured data (probably backed by
database, but that's irrelevant here). At various stages in the process, you
would show different subsets of the details to different users, using
different forms from different nibs.

Approach 1: different window controller for each form. Lots of common code
for handling controls is duplicated between forms.

Approach 2: extract code for common controls into a base class. Each form
has its window controller that inherits from the base class and implements
the non-common controls. There is the possibility that either some forms
still have duplicated code, or that to eliminate duplicated code requires
more than one level of inheritance (yuck!). But the biggest problem is that
as requirements change, some controls may change from being common or not,
and the code may have to be moved around in the hierarchy.

Approach 3: put code for all controls in the base class, and count on the
fact that controls that don't exist in a nib will have their outlets set to
nil, and messages to them will be noops. There *may* still be some cases
where you will need to know if a control actually exists, but in most cases
you simply don't need to know. (Bindings change the balance, because they
eliminate a lot of code that interacts with controls.)

[1] Yeah, yeah, yeah. Templates, Boost, Loki: modern C++ code should have
far fewer explicit invocations of delete than what we were writing back in
the '80s.

--
Scott Ribe
<email_removed>
http://www.killerbytes.com/
(303) 722-0567 voice

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