if
(x.c) f1(x) else f2(x)
.
Using an if-statement is the least structured and most
flexible option. However, we should choose the more struc-
tured and easier-to-optimize alternatives whenever possible.
The hierarchy option can be most useful, but was seriously
overused in the past couple of decades. Not every class natu-
rally belongs in a hierarchy, not every class benefits from the
coupling that a hierarchy introduces, and not every class is
improved by the possibility of adding to it through deriva-
tion (subclassing). Of the alternatives, a call of an ordinary
function is the easiest to understand—it’s also often easy
to inline. Consequently, overloading—statically selecting a
function based on argument types—can lead to major ef-
ficiency advantages compared to indirect calls (as used to
select among alternatives in a class hierarchy).
To become significantly more reliable, code must
become more transparent. In particular, nested conditions
Do'stlaringiz bilan baham: |