Against if (!expression) throw new Exception(msg);

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Mar 16 21:00:57 PDT 2011


On 03/16/2011 09:33 PM, bearophile wrote:
> Andrei:
>
>> No need to blow out of proportion everything that serves a point.
>
> You are right, but probably I am doing that because I see you nearly
> deaf to the problems I see in  Phobos usages of enforce().

It's not about me. Phobos has several contributors (including 
non-committers) who could be receptive to such issues and proactive 
about fixing them either in the language or the library.

>> There are only few places in which use of enforce (or checking in
>> general) slows things down.
>
> I have hit some of such cases in my code, finding them through
> profiling. At first I didn't expect Phobos functions to give those
> problems :-(

There are indeed a few places in which using enforce makes things 
slower. The principled approach to such things is not to blame enforce, 
but to fix the compiler to allow such a simple and effective idiom.

>> Because I don't condone defining large exception hierarchies.
>
> The WrongArgumentException I am talking about comes from a set of
> less than about ten most useful exceptions, and I mean this hierarchy
> to be flat, all of them come from Exception. So this is not a large
> exception hierarchy.

You may want to add a DIP at 
http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel (possibly backed up by 
a pull request) and discuss it here. There are always ten "most useful" 
exceptions, except not all people think of the same ten.

>> but that's not an issue that is the charter of enforce or that
>> enforce prevents.
>
> When you design a language and its standard library you have to keep
> a balance between making things very easy but not good enough, and
> very hard/fussy but better (see the simplicity of ddoc and D
> unittests, ddoc is mostly OK, but unnittests are probably a bit too
> much simple, they miss some essential features like a name). In my
> opinion a standard library is meant to throw a bit better exceptions
> than fully generic ones.

The rhetoric sounds great. Now let's see a concrete proposal.

>> It makes the standard library writers productive.
>
> enforce() has clearly some disadvantages. I believe the very small
> convenience it brings to Phobos writers is not enough to justify its
> usage in many cases.

enforce is convenient and leads to short, simple, and correct functions. 
Small and large teams have used and are using similar artifacts in 
codebases at three other companies I worked and work for. On what basis 
are you assessing the amount of convenience it brings?

> One of such cases is inside iota(), where I suggest to replace its
> usage with a if+throw to allow iota() to be usable in pure functions
> too.

I'd almost suggest using bugzilla.

>> Hoping less and doing more would be great.
>
> You are right. I am sorry. I am trying to help, even if I am not doing enough.

If anything please don't go again the sheepish route. If you were really 
sorry you wouldn't have started with something to be sorry about - and 
in what tone - in the first place. After the initial self-righteous and 
over-confident stance, it always seems awkwardly insincere. Wouldn't a 
simple and honest discussion be best? Framing everything as a diatribe 
against a two-line function is just ungainly.


Andrei


More information about the Digitalmars-d mailing list