Some Thoughts on Designing the Simple Java Rational Library

The current version of Simple Java Rational is quite full featured and weighs in at 8.5kb for the compiled class file. I had set a goal of not exceeding 10kb in size. In retrospect, I wonder: how much smaller could it be? Is 8.5kb too large?

For the first question: The first iteration of the code was very simple and weighed 2.5kb. However, it was not very user friendly. It had two construction methods: one from BigIntegers, and one from BigDecimals. It supported arithmetic and basic functionality of Java objects (equals, hashCode, toString, etc.). It did not support conversion to other formats, other math functions, or extend Java’s Number class. I think that this is about as small as it can get, while still being useful.

For the second question, the answer is dependent on need. I wrote this library since I needed rational arithmetic in a program that was about 2 megabytes in size, and I didn’t want it to grow much larger. The Apache Commons Math 3.6 jar file is 2.1mb, while JScience 4.3’s is 670kb. Even when I recompiled the source to only include the rational classes and its dependencies, the numbers were still huge. Jscience’s Rational implementation spans 17 classes and 1 extra jar dependency that total 346kb, while Apache’s spans 124 files that total 610k. By comparison, the full-featured version of Simple Java Rational is between a 40x to 70x reduction in total size. For my needs, an extra ~8kb for a single library is not a big deal, whereas ~350kb is (each library adds up over time).

There maybe a need for an even smaller library on an embedded device or phone, but this use case seems very unlikely. If the device is scraping for spares kilobytes of space, it probably is not running Java in the first place. Thus, I believe the full featured version at 8.5kb is about the right size.