by Conrad Weisert
November 1, 2011
© 2010 Information Disciplines, Inc.
This article may be circulated freely as long as the copyright notice is included.
Several months ago I observed that many programs were using too many
String data items. Although many of those data
natural candidates for object-oriented classes, some programmers, especially in Java
were ignoring OOP for nearly all text data.
Several readers pointed out that the exact same situation prevails with respect to numeric data. And again Java programs (and textbooks) are worse offenders than programs coded in other object-oriented languages. One observer noted: "It's as if they were coding in Fortran!"
It's not hard to see why, of course. Java presents serious obstacles to handling numeric classes and objects in a natural way:
double, etc. are primitive data, subject to restrictions that constitute almost a separate language from the reference language that deals with objects.
Weightcannot be used in ordinary easy-to-read numeric expressions.
Bjarne Stroustrup, the principal designer of C++, advised long ago: "Provide as good support for user-defined types as for built-in types." The Java approach has been almost the opposite: Make user-defined types as incompatible as possible with the core Java language.
So, many programmers using Java, when confronted with those obstacles, just conclude that it's easier to forgo numeric objects altogether than to struggle with ugly work-arounds. Experienced software engineers disagree, however. Although the expression syntax is ugly and you have to go to a good bit of extra trouble in Java to define a numeric class, object-oriented technology still offers major advantages in programming productivity and software reliability. For example:
Distanceobject by a
Timeobject the result will automatically be a
Speedobject in the proper units.
commissioninstead of by
Temperaturein negative degrees Kelvin or an
Angleexceeding 360 degrees.
Furthemore, once those numeric classes are developed and made available in a library, future application development, compared with using raw floating-point numbers, will be both faster and comparatively free of computational bugs. There is no downside to using classes and objects to model numeric data.
What sorts of numeric data should not be objects?
A thorough treatment
Object-Oriented Computation in C++ and Java, ISBN 978-0-932633-63-7, explores the definition and handling of numeric objects in depth.
Even when we exploit object-orientation to the fullest degree, we still have needs for pure
numbers, and the primitive built-in types work well. Abstract quantities such as loop counters or dimension
specifications are most clearly and most efficiently given as
Convergent series and numeric integrals are usually evaluated with
variables. Intermediate results in a complicated calculation may not belong to any useful application-domain
type. Those data are most naturally and most simply represented by pure numbers.
But as a general rule, if the data item has a unit of measure it's a candidate for a class definition in any object-oriented programming language, even Java.
Return to table of contents
Return to technical articles
Last modified November 1, 2011