Prefer Domain-Specific Types to Primitive Types

From WikiContent

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
September 23 1999, the $327.6 million Mars Climate Orbiter was lost while entering orbit due to a software error back at Earth. The error was later called the metric mixup. The ground station software was working in pounds while the spacecraft expected newtons, leading the ground station to underestimate the power of the spacecrafts thrusters with a factor of 4.45.
September 23 1999, the $327.6 million Mars Climate Orbiter was lost while entering orbit due to a software error back at Earth. The error was later called the metric mixup. The ground station software was working in pounds while the spacecraft expected newtons, leading the ground station to underestimate the power of the spacecrafts thrusters with a factor of 4.45.
-
This is one of many examples of software failures that could have been prevented if strong typing was applied. It also the rationale why the Ada language, primarily designed to implement embedded safety critical software is so strong typed. Ada developers are encouraged to utilize the strong typing offered by the language. The benefit is safer code as can be seen from the example below:
+
This is one of many examples of software failures that could have been prevented if strong typing was applied. It also the rationale why the Ada language, primarily designed to implement embedded safety critical software support strong typing to the extreme. Ada developers are encouraged to to code as illustrated in the example below:
subtype Velocity_In_Knot is Float range 0..500.00;
subtype Velocity_In_Knot is Float range 0..500.00;
Line 11: Line 11:
Distance: Distance_In_Nautical_Miles;
Distance: Distance_In_Nautical_Miles;
-
Some_Number: Float;
+
Some_Number: Float;
With these types and variables defined the statement <code>Some_Number:=Distance+Velocity;</code> will be caught by the compiler as a type error.
With these types and variables defined the statement <code>Some_Number:=Distance+Velocity;</code> will be caught by the compiler as a type error.

Revision as of 20:20, 27 November 2008

September 23 1999, the $327.6 million Mars Climate Orbiter was lost while entering orbit due to a software error back at Earth. The error was later called the metric mixup. The ground station software was working in pounds while the spacecraft expected newtons, leading the ground station to underestimate the power of the spacecrafts thrusters with a factor of 4.45.

This is one of many examples of software failures that could have been prevented if strong typing was applied. It also the rationale why the Ada language, primarily designed to implement embedded safety critical software support strong typing to the extreme. Ada developers are encouraged to to code as illustrated in the example below:

subtype Velocity_In_Knot is Float range 0..500.00;

subtype Distance_In_Nautical_Miles is Float range 0..3000.00;

Velocity: Velocity_In_Knot;

Distance: Distance_In_Nautical_Miles;

Some_Number: Float;

With these types and variables defined the statement Some_Number:=Distance+Velocity; will be caught by the compiler as a type error.

One of the main features provided by object-oriented languages are abstract data types and strong typing. Still many developers uses the primitive data types offered by the language such as String. Wrapping complex data structures as strings are simple as the object method basically can take any data, but it's also an unsafe style of programming, where the developer avoids delegating appropriate quality control to the compiler. The same level of strong typing can be achieved by any object-oriented programming language such as Java, C++, and C# by defining the appropriate data type as a class. The Value Object pattern from Eric Evans book Domain-Driven Design provide good programming practices for these languages. By defining values for velocity, money, and distance in terms of classes, safe arithmetical operations can be supported combined with many other benefits such as more readable code, fewer lines of code and increased reuse.

Therefore, developers should stop to misuse primitive data types for convenience, and start on the journey of exploring the power of strong typing and the Value Object pattern.

By Einar Landre

This work is licensed under a Creative Commons Attribution 3


Back to 97 Things Every Programmer Should Know home page

Personal tools