Isn't `each` too much of a good thing?

H. S. Teoh hsteoh at
Fri Sep 18 15:22:58 UTC 2020

On Fri, Sep 18, 2020 at 02:55:39PM +0000, 12345swordy via Digitalmars-d wrote:
> On Thursday, 17 September 2020 at 21:03:08 UTC, H. S. Teoh wrote:
> > Yeah, implicit is bad.  I used to be a fan of implicit things,
> > because of the convenience... but more and more, with more
> > experience, I'm starting to be convinced that explicit is always
> > better -- for readability and maintainability. Too many layers of
> > implicit things, and the code becomes obscure, hard to read, or easy
> > to *wrongly* read, and consequently fragile.
> By implicit I am assuming your talking about c++ here correct?

I'm speaking in general.  It applies to any language, really.

> The main case for it is writing:
> T t = a;
> Instead of:
> T t = (t)a
> That the main benefit of implicit conversion that we should be
> focusing on.

Just write:

	auto t = T(a);

Mission accomplished, no need for implicit conversion, and it's clear
exactly what's going on.

In non-trivial cases, I know it's convenient not to have to write T(a)
all the time, but really, it's better to write it out explicitly so that
future readers of the code (i.e., you after 3 months :-D) know exactly
what conversions are taking place and how.  When there are too many
implicit conversions, the code may look better superficially, but
actually it may be hiding inefficient implicit conversions, or it may
look like it's doing something but it's actually doing something else
because of the implicit conversions.  It's more inconvenient to write
things out explicitly, but in the long run it's better.


Computers aren't intelligent; they only think they are.

More information about the Digitalmars-d mailing list