"Constructor" was a Very Bad name choice we have never recovered from (Was Re: D is dead)

John Carter john.carter at taitradio.com
Mon Aug 27 03:06:17 UTC 2018


On Friday, 24 August 2018 at 02:33:31 UTC, Jonathan M Davis wrote:
> Walter Bright wrote:
>> My personal opinion is that constructors that throw are an 
>> execrable programming practice, and I've wanted to ban them. 
>> (Andrei, while sympathetic to the idea, felt that too many 
>> people relied on it.) I won't allow throwing constructors in 
>> dmd or any software I have authority over.
>
> Wow. I'm surprised by this.
>
> I expect that you'd have a riot on your hands though if you 
> actually tried to push for getting rid of throwing constructors.

A generation of programmers have been mislead down a deep rabbit 
hole thinking that "Constructors" are things that "Construct" 
objects.

This has to led to a generation of vaguely smelly code that "does 
too much work in the constructor" (of which throwing exceptions 
is evidence).

The last few years I have told myself (and anyone who doesn't 
back away fast enough) that "Constructors" do _not_ construct 
objects, they are "Name Binders." (Sort of like lisp's "let" 
macro)

They bind instance variable names to pre-existing sub-objects.

This attitude coupled with an a rule of thumb, "make it immutable 
unless I prove to myself that I _need_ it to be mutable" has led 
to a major improvement in my code.


More information about the Digitalmars-d mailing list