In 1999, NASA lost the $125 million Mars Climate Orbiter as it went into orbital insertion. Due to a mismatch between US customary and SI units of measurements in one of the APIs, the spacecraft came to close to the planet, passed through the upper atmosphere and disintegrated. Sadly, this hasn’t been the only instance where a mismatch between units of measurements had catastrophic consequences, but it’s certainly one of the most spectacular and expensive ones.
How could this happen? The bad news is: if you use primitive types to handle quantities in your code, due to that very same practice. At best, you’ve codified the unit in the name of the variable or the database field, e.g. calling it lengthInMetres. Otherwise, you’re only relying on convention, just like Lockheed Martin and NASA did.
Join this talk to learn how JSR-385 can help you avoid $125 million mistakes, and discover the immeasurable world of dimensions, units and quantities.
5. 5
Mars
Climate
Orbiter
“The problem here was not the error; it was the
failure of NASA's systems engineering, and the
checks and balances in our processes, to detect
the error. That's why we lost the spacecraft.”
—Edward Weiler,
NASA associate administrator for space science,
IEEE Spectrum: Why the Mars Probe went off
course
6. Other Unit
Mishaps
6
● The 12 October 1492 “mishap”
● “Gimli Glider”, 1983
○ Air Canada Flight 143
○ Received less than half the fuel
needed
● Clarence the Tortoise, 2001
● A whole set of medication
dose errors
8. What Is the Fundamental Problem?
8
Primitive (Java) types
are primitive types
9. What Is the Fundamental Problem?
9
● Primitive (Java) types are primitive types
○ As in: building blocks to build other types
● Examples fundamentally in conflict with OOP and DDD:
○ static final double C = 1079252849;
○ static final double SPEED_OF_LIGHT = 1079252849;
○ static final double SPEED_OF_LIGHT_IN_KM_PER_H = 1079252849;
● What it should be:
○ static final Quantity<Speed> SPEED_OF_LIGHT
= Quantities.getQuantity(1079252849, Units.KILOMETRE_PER_HOUR);
10. Rolling Your Own Library
10
● A lot of work
○ Development
○ Maintenance
○ Upgrades and extensions
● Error-prone
● Are you a units of measurement domain expert?
○ Even of the units of measurement system you’re used to?
11. What is JSR 385?
https://www.slideshare.net/filipvanlaenen
11
12. JSR 385: Units of Measurement API 2.0
12
Description:
This JSR is a major evolution of the Unit API 1.0 (JSR 363) specification.
Focused on the SI System redefinition, modularity and support for Java
SE 8/9 and above.
JSR 363: Units of Measurement API
This JSR specifies Java packages for modeling and working with
measurement values, quantities and their corresponding units.
16. Definition of a Physical Quantity
16
“A physical quantity is a physical property of a phenomenon, body, or
substance, that can be quantified by measurement.”
Source: Wikipedia
17. Definition of a Physical Quantity
17
“A physical quantity is a physical property of a phenomenon, body, or
substance, that can be quantified by measurement.”
Source: Wikipedia
“A physical quantity can be expressed as the combination of a magnitude
expressed by a number – usually a real number – and a unit.”
Ibidem
19. Definition of a Physical Quantity
19
“A physical quantity is a physical property of a phenomenon, body, or
substance, that can be quantified by measurement.”
Source: Wikipedia
“A physical quantity can be expressed as the combination of a magnitude
expressed by a number – usually a real number – and a unit.”
“The same physical quantity can be represented equivalently in many unit
systems.”
Ibidem
35. Question: How Much is 0 °C + 0 °C?
● 0 °C
● 0 °C
● 273.15 °C
Two absolute temperatures:
0 °C + 0 °C = 273.15 K + 273.15 K
= 546.30 K
= 273.15 °C
35
36. Question: How Much is 0 °C + 0 °C?
● 0 °C
● 0 °C
● 273.15 °C
An absolute temperature and a temperature change:
0 °C + 0 °C = 273.15 K + 0 K
= 273.15 K
= 0 °C
36
37. Question: How Much is 0 °C + 0 °C?
● 0 °C
● 0 °C
● 273.15 °C
Two temperature changes:
0 °C + 0 °C = 0 K + 0 K
= 0 K
= 0 °C
37
38. Question: How Much is 0 °C + 0 °C?
273.15 °C (546.30 K)
0 °C + 0 °C = 0 °C (273.15 K)
0 °C (0 K)
38
39. Multiplication
39
Units:
● kg × m = kg·m
● kg × mm = kg·mm
● g × km = g·km ≟ kg·m
● m × m = m2
● km × km = km2
● mm × km = mm·km ≟ m2
● µm × m = µm·m ≟ mm2
Quantities:
● 2 kg × 3 m = 6 kg·m
40. Division
40
Units:
● kg / m = kg·m-1
● kg / mm = kg·mm-1
● g / mm = g·mm-1
≟ kg·m-1
● m2
/ m = m
● m / m = 1
● km / km = 1
● km / m = km/m ≟ 1k
Quantities:
● 3 kg / 2 m = 1.5 kg·m-1
46. Systems of Measurement
46
● Metric system
○ Metre, kilogram, second, ampere, …
● Imperial system
○ Foot, ounce, pound, gallon, Fahrenheit, horsepower, …
● United States customary units
○ Foot, ounce, pound, gallon, Fahrenheit, horsepower, …
● Roman units of measurement
○ Pes, uncia, libra, …
● Norwegian units of measurement
○ Fot, favn, mål, tønne, snes, …
47. What Was the Fundamental Problem Again?
47
Primitive (Java) types
are primitive types
51. The Java Community Process
51
“The JCP gives you a chance to have your own work become an official
component of the Java platform and to offer suggestions for improving
and growing the technology. Either way, everyone in the Java community
benefits from your participation. That's one of the reasons the JCP is open
to everyone.”
Source: https://www.jcp.org/en/participation/overview
52. Becoming a JCP Member
52
“Membership in the JCP offers you a chance to become a permanent part
of the Java platform's history by contributing your work and
recommendations to the various standard specifications, and/or a chance
of serving on the Executive Committee.”
Source: https://www.jcp.org/en/participation/membership