Wednesday, September 19, 2012

wrap numeric types in unit types in advance of calculations

Share photos on twitter with Twitpic

Bjarne Stroustrup spoke on C++ at National Instruments on behalf of IEEE tonight. I saw the talk. The talk was largely on good standards and practices. For example, this is not a particular intuitive interface signature:

Rectangle(int, int, int, int);

Share photos on twitter with Twitpic

The one thing I heard that I had not heard before that really interested me was the notion of wrapping numeric types in simple objects before handing them into method signatures for methods that are to undertake calculations based on the numeric data. If you have a method that is to do something with a number of miles it is probably best to hand in an object of Mile type that contains a double instead of a double. Why? Then the nature of the unit is conveyed and a programmer will not accidentally make calculations inside the method under the false assumption that the double being handed in represents kilometers.

No comments:

Post a Comment