Talk:Prefer Domain-Specific Types to Primitive Types

From Programmer 97-things

Jump to: navigation, search

By SDX2000:

I think more examples/libraries need to be provided to flesh this topic out. For example a link to the Boost Units/Meta programming libraries may be helpful.

By Simoncpu:

  • This article is enlightening. Now I know what this hoopla about strong typing is all about. Simoncpu 00:06, 17 September 2009 (PDT)

By Paul Colin Gloster:

I agree with the intended message of this contribution and it would be good for this to be printed in the book.

However, though on October 6th, 2009 the wiki server has claimed the webpage "has been accessed 1,010 times" and it had last been edited in July 2009, a number of details are wrong.

There was an Ada mistake twice:

range 0..

had been wrong unlike

range 0.0 ..

Furthermore the code example had subtypes instead of derived types, so the comment in the line

Some_Number:= Distance + Velocity; -- Will be caught by the compiler as a type error.

is false. This is actually completely valid Ada and will not be rejected by the compiler (nor the runtime) because the types of both Distance and Velocity are subtypes (instead of derived types) of the type of Some_Number. Refresh your memory of the distinction in any Ada textbook. I would recommend using derived types in the example instead.

If the example code is supposed to be embedded, then it may be better to use a fixed point type instead of a floating point type. However, many of the readers who work on desktop systems might be more comfortable with floating point types. Maybe you could introduce the concepts of subtypes; derived types; fixed point types; and floating point types instead of having just one example.

Consider using strictly one of the names Velocity_In_Knots and VelocityInKnots and strictly one of Distance_In_Nautical_Miles and DistanceInNauticalMiles (I prefer Velocity_In_Knots and Distance_In_Nautical_Miles). (I realize that the underscored versions were for Ada and the camel versions were for inferior languages.)

By Klaus Marquardt:

While your point about strong typing is well motivated and perfectly valid, you might consider discussing alternative approaches.

1. The unit mismatch is a classic, and can be solved in many different ways...

* Martin Fowler's Analysis Patterns offer a Quantity combining a value and a unit; this solution allows for conversion (if the possible runtime penalty and/or lookup failure is OK in your system), or assertions checked at startup time;
* in C++, template parameters can do the unit checking.

2. strong typing might not scale for ultra-large systems (like the internet) - but in those systems your life and money typically does not depend on blind understanding, there is an element of checking involved.

Personal tools