"C++ has made templates a dirty word"
I have never quite understood the general aversion towards C++ templates
out there. Maybe I'm biased.
People don't like power
.
I find that C++ templates make writing programs simpler, not more
complicated. Many things can be done with simple one-liners which would
otherwise require dozens of lines of complicated code.
There are many uses of C++ templates. For things like typesafe
containers, they are exceptionally good---it's not an accident
that Java duplicated the syntax exactly (including the mistake
of using <..>, rather than some other parenthesing). For things
like metaprogramming, the obvious objection is that they weren't
really designed for it, and the syntax required quickly makes
the code unreadable. But of course, this complaint generally
comes from supporters of languages which don't have any
metaprogramming possibilities at all. In the end, nothing is
free, and in every individual case, you have to weigh the cost
(in terms of readability, etc.) of using metaprogramming vs. the
advantages (less code, often simpler to use, etc.) Sometimes,
the balance will weigh one way, other times the other. When the
balance is against metaprogramming, don't use it.
[...]
When Java was designed as a "better C++" (which is the case, no matter
how much Java people deny this irrefutable fact), they naturally left any
kind of support for templates completely out because, you know, templates
are "evil" and produce bad code.
Or perhaps because when Java was being designed, they were still
largely unknown territory. Java was designed as a "better C++"
based on the experience we had with C++ around 1990. Some of
the things it incorporates from C++ are things we later learned
were errors, to be avoided (i.e. putting the function
definitions inside the class, or having an iterator in which
access and stepping were bound up in a single function).
Many years later they came to regret this, as a lack for any kind of
generic code (besides what OOP offers) was a kind of limiting factor and
produced ugly code. So rather than to succumb to the lure of C++ templates
they introduced a "better template mechanism" (and because "template" is
a curseword they couldn't use that name, and hence they invented a new
name for it, namely "generics", to avoid any negative comparisons).
The name "generics" precedes that of "templates". (I don't know
why C++ chose "template", rather than "generic".)
Of course these "better templates" are much less useful
because they cannot be used to eg. create generic containers
which can support basic types (such as ints).
That's part of a larger problem in Java. Java's authors clearly
recognized the need for types with value semantics, but decided
that they knew which ones you needed, for all time. A cleaner
design would have not had the built in types at all, but only
Integer and Double classes, pre-defined along the lines of
String. And operator overloading, so that you could add and
subtract them with a reasonable syntax.
I don't know how the "generics" in C# work, so maybe they are honestly
"better" there.
I don't know if there's a "better". Different might be a more
appropriate word. (In general---I don't know C# either.) Java
templates are very good for the problem they were designed to
solve. Almost as good as C++ templates for that problem. They
intentionally don't solve other problems. C++ templates can't
be said to have a congenial syntax for many of the
metaprogramming problems, but in the end, they're better than
nothing (which is what Java offers in this regard).