Prefer Domain-Specific Types to Primitive Types
来源:互联网 发布:2016程序员笔记本推荐 编辑:程序博客网 时间:2024/05/21 09:09
On 23rd September 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 themetric 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 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.
Developers in less demanding domains might also benefit from applying more domain-specific typing, where they might otherwise continue to use the primitive data types offered by the language and its libraries, such as strings and floats. In Java, C++, Python, and other modern languages the abstract data type is known as class
. Using classes such asVelocity_In_Knots
and Distance_In_Nautical_Miles
adds a lot of value with respect to code quality:
- The code becomes more readable as it expresses concepts of a domain, not just
Float
orString
. - The code becomes more testable as the code encapsulates behavior that is easily testable.
- 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 domain-specific types for the purpose of developing quality software.
By Einar Landre
This work is licensed under a Creative Commons Attribution 3
- Prefer Domain-Specific Types to Primitive Types
- Prefer Domain- Specific Types to Primitive Types
- Primitive Types and Objects
- Specializing for primitive types
- 基本类型 primitive types
- 02. Primitive Types
- MATLAB Primitive Types
- Day1-2.Primitive types
- Primitive Types and Reference Types in Javascript
- Convert Long to numeric primitive data types example
- Type Fundamentals (.NET: Primitive types, reference types and value types )
- Working with Primitive Data Types
- Primitive vs. Reference Data Types
- types
- the Primitive Built-in Types bool
- use scalar properties for primitive data types
- Thinking in java (2)----primitive types
- use scalar properties for primitive data types
- IT领袖峰会精彩观点:4G时代不存在信令问题
- Pair Program and Feel the Flow
- linux多线程学习(一)
- SQL Server FOR XML PATH 语句的应用
- DatabaseMirroring搭建
- Prefer Domain-Specific Types to Primitive Types
- 利用.net技术解决C++导出函数char*转C#string
- 流行的视觉追踪方法
- 关于action层中success的属性配置
- Prevent Errors
- 话说电容
- ubuntu12.04 gvim启动慢解决办法
- 初识MySql数据库
- 【多媒体封装格式详解】--- AAC ADTS格式分析