Re: message to nil? (very basic question)
Re: message to nil? (very basic question)
- Subject: Re: message to nil? (very basic question)
- From: Bill Bumgarner <email@hidden>
- Date: Mon, 21 Jan 2002 09:00:20 -0500
On Monday, January 21, 2002, at 06:36 AM, Christophe Dore wrote:
IMHO, the only problem with messaging nil is when nil in a variable is an
unexpected value at that time.
Which can be a very big problem, indeed. In my experience, a variable
taking an unexpected nil value has cost many many hours of debugging.
Often, it crops up because some fundamental resource is unavailable or
because of a data integrity problem -- actually, because of a combination
of those issues and developer laziness in that the developer didn't assert
that something is non-nil when they should have.
The other problem is that it often adds undocumented requirements on an
API and does not cause an immediate failure when that requirement-- that a
reference is non-nil-- is not met. A method that requires a non-nil
argument but executes without failure with a nil argument incurs
corruption in the object graph that eventually bites you, but often very
far from where the original problem was introduced.
It propagates and often amplifies an error situation.
So, I beleive that nil messaging is safe as long as you use assertions
for those case.
This has transitioned from a technical question to a design pattern
question.
I agree-- if your code expects something to be non-nil put in an assertion.
Better yet, create unit tests from day one of the project and ensure
that the behavior is correct across the system.
In any case, you are absolutely correct that a feature that hides a defect
can cause amplification of said defect. This makes the defect a lot
harder to debug in that the location of the defect is typically very far
away (in terms of execution path) from where the defect actually becomes
apparent.
Java being unable to assert (until maybe 1.4, still in beta) those guys
chose to disallow it.
I don't think that's the reason why the disallow it. Message-eating-nil
follows very much in Java's overall 'fail close to the defect' patterns
(strong typing, bounds checking, etc..). As well, there are other very
dynamic languages that do not allow messaging to nil; Python immediately
comes to mind and Python is significantly more dynamic than ObjC (and has
assertions).
b.bum