Exceptional coding style

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jan 14 13:40:53 PST 2013


On Mon, Jan 14, 2013 at 09:57:25PM +0100, monarch_dodra wrote:
[...]
> >>On 01/14/2013 11:24 AM, Walter Bright wrote:
> >>>Quite a nice read on the coding style used in Doom.
> >>>
> >>>http://kotaku.com/5975610/the-exceptional-beauty-of-doom-3s-source-code?post=56177550
[...]
> Apart from a few *style* issues, the only thing the article contains
> was mostly hate for the stl.
> 
> I understand one might dislikes the stream operators due to the
> syntax (myself included), but once you've used them more than once,
> and know how to use them, they aren't a problem. The strong typing
> they provide is simply unmatched in C++. The article even mentions
> that NOT using stream operators was one of their biggest source of
> bugs. And Carmack himself replies stating that in retrospect:
> StrongTyping > WeirdCode.

Stream operators are teh eviil. Overloading << and >> for I/O was a
horrible design decision. Operator overloading should be reserved for
numeric types. But then again, Stroustrup didn't have the benefit of
hindsight back then, and certainly, on the surface, overloading <<
and >> seemed like a cool thing to do. And C++ didn't (still doesn't?)
have typesafe handling of variadics, so the desire to not have to write
"cout.put(x); cout.put(y); cout.put(z); cout.put(w); ..." is
understandable.

But none of that changes the fact that overloading << and >> for I/O was
a horrible idea.


> There's also hate for stl's containers, stating they are *too
> generic*, stating that it is better to use *Only* a HashTable of
> <int, int> or <char*, int>. Nobody is stopping you from doing it
> with the stl. Not being overly generic is one thing. Not using
> something specific because it *spawned* from something generic is
> another.

I don't think it's so much as a complaint about genericity, as the lousy
C++ template syntax.


> The argument is *always* the same "the syntax is ugly and hard to
> use and outside of my comfort zone".

Which is why D's template syntax is such a big plus.

The prevalent hatred for templates is mostly the fault of poorly-chosen
syntax on C++'s part. Overloading < and > to delimit template arguments
was just such a horribly bad idea. It makes C++ impossible to lex before
it's parsed, and gives templates an undeservedly frightening appearance.
Unfortunately, the best of us coders are prone to judge by appearances,
especially when dealing with an unfamiliar new language feature (which
was the case when C++ templates first appeared on the scene). Sometimes,
syntax matters.

I think people learning about templates for the first time in D syntax
would be much less liable to developing an aversion to it. In fact, TDPL
was written in such a way that the word 'template' isn't even used until
the reader has already gotten used to the concept of "compile-time
parameters". When understood in this way, templates are far more
approachable than is the case in most introductions to C++ templates.

D templates can still stand some improvement, though. Maybe not so much
at the language level, but template-related errors in DMD are still a
frightening sight to behold for the uninitiated. Phobos also can stand a
lot of improvement in providing human-readable errors in a catch-all
default template, so that a failed instantiation doesn't produce 27
pages of errors, most of which involve details of internal Phobos
implementation structures that no outside user should need to know
about.  Improvement this area will help a lot towards newbie acceptance
of the concept of templates.


T

-- 
One disk to rule them all, One disk to find them. One disk to bring them
all and in the darkness grind them. In the Land of Redmond where the
shadows lie. -- The Silicon Valley Tarot


More information about the Digitalmars-d mailing list