[dmd-internals] Asserts

Jonathan M Davis jmdavisProg at gmx.com
Fri Nov 9 00:20:04 PST 2012


On Thursday, November 08, 2012 23:40:40 David Held wrote:
> My personal policy is to assert early and assert often.  I see many
> functions taking pointer args and dereferencing them without checking
> for NULL first.  I know Walter likes to claim that dmd doesn't have any
> NPEs because he is a careful coder; but the fact is, he is not the only
> coder, and most functions don't explicitly document which arguments are
> allowed to be NULL and/or when.  Of course, it would be better for
> non-null arguments to just pass by reference, but fixing that would be
> quite disruptive in most cases (and outright infeasible in others, like
> void*).
> 
> However, I also know that asserting in every function is a religious
> issue for some, so I thought I would ask the dev community about
> thoughts on this topic.

I would point out that asserting that something is non-null doesn't really buy 
you much. The compiler will just segfault if it tries to dereference a null 
pointer. Even if you don't have core dumps turned on, compilers are highly 
deterministic, so all you have to do is try and compile the same program again 
in a debugger, and you'll see exactly what happened (which is what you'd 
probably have to do anyway if it failed an assertion). So, there really isn't 
much point in adding a bunch of assertions to check that pointers aren't null.

- Jonathan M Davis


More information about the dmd-internals mailing list