COVER FE ATURE
compUteR
52
information is encoded in the type system.
Consider
void increase_to(double speed);
// speed in m/s
This is an error waiting to happen. Maybe we understand
the requirements for an argument, but they only appear in
comments and other documentation. Is
increase_to(7.2)
correct? Does
7.2
represent a speed? If so, in what units?
A better try would be
void increase_to(Speed s);
Given a reasonable definition of
Speed
,
increase_to(7.2)
is an error and
increase_to(72m/10s)
is likely to be cor-
rect. This isn’t just an issue of units; I have a philosophical
problem with parameters of near-universal types. For
example, an integer can represent just about everything.
Consider
// construct a rectangle:
Do'stlaringiz bilan baham: