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.

??130 97 Things Every Programmer Should Know

?

???????????????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 as Velocity_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 or String.

? 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 dynami- cally 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.

时间: 2024-07-29 00:40:28

Prefer Domain- Specific Types to Primitive Types的相关文章

Java中的原始类型(Primitive Types)与引用类型(Reference Values)

Java虚拟机可以处理的类型有两种,一种是原始类型(Primitive Types),一种是引用类型(Reference Types). 与之对应,也存在有原始值(Primitive Values)和引用值(Reference Values)两种类型的数值可用于变量赋值.参数传递.方法返回和运算操作. 原始类型与值 Java虚拟机支持的原始数据类型包括数值类型.布尔类型和returnAddress类型. 数值类型包括:整数类型和浮点类型. 整数类型包括: 1.byte 2.short 3.int

Primitive Types and Expressions

Class Data or Attributes state of the application Methods or Functions have behavior Namespace is a container for related classes Assembly (DLL or EXE) is a container for related namespaces is a file which can either be a EXE or  a DLL Application in

Primitive Types in Go

Introduction As a strong  type static language, Go has many primitive types we will care about. In first level, they can be divided into two groups: non-collection and collections(e.g. array, slice, map). In this article we will talk three different

Oracle OCA J2SE7 Cetificate - Rules for Primitive Types

Any bigger than an int can NEVER be assigned to an int or anything smaller than int (byte, char, or short) without explicit cast. Constant values up to int can be assigned (without cast) to variables of lesser size (e.g. short to byte) if the values

Domain Specific Biases(Geographical influence)Updated Aug,11st

data used :foursquare NYC data, this code has not been finished yet.. here  is the link: https://github.com/FassyGit/LightFM_liu/blob/master/DomainBiases.py I have succeeded in calculating the distannces, there are some functions in the codes. I will

Java中的Union Types和Intersection Types

前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联合类型)使用比特或运算符|进行构造: A | B | C 注意:用|符号来构造Union Type类型只是Java语言的规定,|在这里不代表比特或的含义. 上例中,A | B | C是一个Union type,Union type的含义就是"或",只要满足其中一个即可. 实例:捕获多个异常

JPA 教程

Entities An entity is a lightweight persistence domain object. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity c

spark优化之数据结构(减少内存)

官网是这么说的: The first way to reduce memory consumption is to avoid the Java features that add overhead, such as pointer-based data structures and wrapper objects. There are several ways to do this:1.Design your data structures to prefer arrays of object

[Android]Android性能优化

安卓性能优化 性能优化的几大考虑 Mobile Context 资源受限 内存,普遍较小,512MB很常见,开发者的机器一般比用户的机器高端 CPU,核心少,运算能力没有全开 GPU,上传大的纹理(texture),overdraw 内存开销大,会导致系统换入换出更频繁,GC更频繁,APP被kill.被重启更频繁,不仅会消耗更多电量,而且GC会消耗大量时间,使得应用程序渲染速度低于60fps(GC耗时dalvik 10-20ms,ART 2-3ms) 外部存储与网络,也是受限的,需要考虑资源的使