[dmd-internals] Asserts

Alex Rønne Petersen xtzgzorex at gmail.com
Fri Nov 9 00:52:06 PST 2012


On Fri, Nov 9, 2012 at 8:40 AM, David Held <dmd at wyntrmute.com> 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.
>
> Dave

I don't hack on DMD much, but here's my take on the matter: Asserting
every function parameter (where applicable of course) is good practice
because it helps you catch bugs as early as possible. If you don't do
this, you could end up with null pointers far into the program where
you then have to spend time figuring out where it came from. This is
especially nasty when e.g. building tree-like data structures and you
can't just look at the call stack to figure out where you got a null
pointer from.

I also think the documentation aspect is important. I've often found
myself wondering whether some parameter is allowed to be null when
hacking on DMD.

Regards,
Alex


More information about the dmd-internals mailing list