97 Things Every Programmer Should Know

(Chris Devlin) #1

(^130) 97 Things Every Programmer Should Know


Prefer Domain-Specific Types to Primitive Types ..........


Prefer Domain-Specific Types to Primitive Types ..........


Einar Landre


ON SEPTEMBER 23, 1999, the $327.6 million Mars Climate Orbiter was lost
while entering orbit around Mars due to a software error back on Earth.
The error was later called the metric mix-up. The ground-station software
was working in pounds, while the spacecraft expected newtons, leading the
ground station to underestimate the power of the spacecraft’s thrusters by a
factor of 4.45.


This is one of many examples of software failures that could have been pre-
vented if stronger and more domain-specific typing had been applied. It is
also an example of the rationale behind many features in the Ada language,
one of whose primary design goals was to implement embedded safety-critical
software. Ada has strong typing with static checking for both primitive types
and user-defined types:


type Velocity_In_Knots is new Float range 0.0 .. 500.00;
type Distance_In_Nautical_Miles is new Float range 0.0 .. 3000.00;
Velocity: Velocity_In_Knots;
Distance: Distance_In_Nautical_Miles;
Some_Number: Float;
Some_Number:= Distance + Velocity; -- Will be caught by the compiler as a type error.
Free download pdf