checkedint call removal
Daniel Gibson via Digitalmars-d
digitalmars-d at puremagic.com
Fri Aug 1 07:09:31 PDT 2014
Am 01.08.2014 05:27, schrieb Walter Bright:
> On 7/31/2014 1:11 PM, Daniel Gibson wrote:
>> I guess in many cases I'd avoid using assert() in fear of breaking my
>> defensively written program (like that example from earlier: assert(x
>> !is null);
>> if(x) { x.foo = 42; }).
>
> I just hang my head in my hands at that code :-)
>
> Really, you're much better off writing something like:
>
> if (x) {
> x.foo = 42;
> }
> else {
> writeln("Sack the QA dept. for not testing this code");
> abort();
> }
>
> which expresses what you intend directly and would be perfectly fine.
>
Not exactly - in release mode I don't want it to abort.
But it could be rewritten as
if(check(x != null, "Sack the QA dept. for not testing this code")) {
x.foo = 42;
}
where check throws in debug mode and returns false in release mode if
the condition fails.
I just never considered assert() to have the meaning you're giving it,
even though maybe it was always considered to be like that but just not
implemented "properly" by common languages. I wrote a bit more about
this in another reply.
Cheers,
Daniel
More information about the Digitalmars-d
mailing list