assert(obj) is an atrocity

Kapps Kapps at NotValidEmail.com
Tue Nov 8 23:24:32 PST 2011


Agreed. It would be nice to have an error thrown instead of just a 
segfault that you may or may not get a reliable backtrace for and then 
spend the next few days trying to find out where it is. -_-

On 08/11/2011 6:19 PM, dsimcha wrote:
> On 11/8/2011 7:05 PM, Timon Gehr wrote:
>> On 11/08/2011 11:35 PM, Alex Rønne Petersen wrote:
>>> Hi,
>>>
>>> As the title suggests, I'm going to be rather blunt about this.
>>> assert(obj) testing the invariant *without* doing a null check is insane
>>> for the following reasons:
>>>
>>> 1) It is not what a user expects. It is *unintuitive*.
>>> 2) assert(!obj) does an is-null check. assert(obj) is a completely
>>> broken opposite of this.
>>> 3) No AssertError is thrown, which is the entire point of the built-in
>>> assert().
>>> 4) The few added instructions for the null check hardly matter in a
>>> *debug* build of all things.
>>>
>>> I don't mind assert(obj) testing the invariant of obj. In fact, that
>>> very much makes sense. But please, please, *please* check the object for
>>> null first.
>>
>> +1.
>
> +1. **AND** in debug mode all pointers should be checked for null.
> Conceptually, I fail to see how a null pointer dereference is different
> from an array bounds error. A null pointer is effectively an array with
> length zero.



More information about the Digitalmars-d mailing list