Prefer Domain-Specific Types to Primitive Types

From WikiContent

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
At 23 of September 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 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 spacecrafts thrusters with a factor of 4.45.
+
On 23rd September 1999 the $327.6 million Mars Climate Orbiter was lost while entering orbit 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 prevented if strong or definite typing was applied. It also an example of the rationale behind the Ada language, primarily designed to implement embedded safety critical software. Ada support strong typing to the extreme, as illustrated in the example below.
+
This is one of many examples of software failures that could have been prevented if stronger and more definite 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 supports strong typing with static checking:
-
subtype Velocity_In_Knot is Float range 0..500.00;
+
subtype Velocity_In_Knots is Float range 0..500.00;
subtype Distance_In_Nautical_Miles is Float range 0..3000.00;
subtype Distance_In_Nautical_Miles is Float range 0..3000.00;
-
Velocity: Velocity_In_Knot;
+
Velocity: Velocity_In_Knots;
Distance: Distance_In_Nautical_Miles;
Distance: Distance_In_Nautical_Miles;
Line 15: Line 15:
Some_Number:= Distance + Velocity; -- Will be caught by the compiler as a type error.
Some_Number:= Distance + Velocity; -- Will be caught by the compiler as a type error.
-
Developers in less demanding domains might also benefit from applying definite typing, especially those working in languages with abstract data types such as Java, C++ and Phyton. In these languages the abstract data type is know as <code>class</code>. Using classes such as <code> VelocityInKnot</code> and <code>DistanceInNaticalMiles</code> adds a lot of value with respect to code quality:
+
Developers in less demanding domains might also benefit from applying definite typing, where they might otherwise continue to use the primitive data types offered by the language and its libraries, such as string and floats. In Java, C++, Python, and other modern languages the abstract data type is know as <code>class</code>. Using classes such as <code> VelocityInKnots</code> and <code>DistanceInNaticalMiles</code> adds a lot of value with respect to code quality:
-
1. The code become more readable as it expresses concepts of a domain, not only Float or Strings.
+
# The code becomes more readable as it expresses concepts of a domain, not just <code>Float</code> or <code>String</code>.
-
2. The code become more testable as the code encapsulate behavior that is easily testable.
+
# The code becomes more testable as the code encapsulates behavior that is easily testable.
-
3. The code facilitate re-use across applications and systems.
+
# The code facilitates reuse across applications and systems.
-
The approach is equally valid for users of both statically and dynamically typed languages. The only difference is that developers using statically typed languages get some help from the compiler while those embracing dynamically typed languages need to rely on their unit tests alone.
+
-
The morale is to start exploring definit typing for the purpose of developing quality software.
+
The approach is equally valid for users of both statically and dynamically typed languages. The only difference is that developers using statically typed languages get some help from the compiler while those embracing dynamically typed languages are more likely to rely on their unit tests. The style of checking may be different, but the motivation and style of expression is not.
 +
 
 +
The moral is to start exploring definite typing for the purpose of developing quality software.
By [[Einar Landre]]
By [[Einar Landre]]

Revision as of 21:37, 27 November 2008

On 23rd September 1999 the $327.6 million Mars Climate Orbiter was lost while entering orbit 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 prevented if stronger and more definite 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 supports strong typing with static checking:

subtype Velocity_In_Knots is Float range 0..500.00;

subtype Distance_In_Nautical_Miles is Float range 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.

Developers in less demanding domains might also benefit from applying definite typing, where they might otherwise continue to use the primitive data types offered by the language and its libraries, such as string and floats. In Java, C++, Python, and other modern languages the abstract data type is know as class. Using classes such as VelocityInKnots and DistanceInNaticalMiles adds a lot of value with respect to code quality:

  1. The code becomes more readable as it expresses concepts of a domain, not just Float or String.
  2. The code becomes more testable as the code encapsulates behavior that is easily testable.
  3. The code facilitates reuse across applications and systems.

The approach is equally valid for users of both statically and dynamically typed languages. The only difference is that developers using statically typed languages get some help from the compiler while those embracing dynamically typed languages are more likely to rely on their unit tests. The style of checking may be different, but the motivation and style of expression is not.

The moral is to start exploring definite typing for the purpose of developing quality software.

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