Dustin's Pages

Tuesday, October 25, 2011

Between Two Points with Groovy

I needed to check a ninth grader's math homework related to calculating three things about a line between two points. The assignment was to calculate the distance between two points, the slope of a line connecting the two points, and the midpoint on a line between the two points. Because I was tired after a long day and because I inherently possess the long-advertised software developer "laziness," I decided to write up a quick Groovy script to calculate these numbers for me. There were enough problems to check that I believe I either broke even or even came out a little ahead in terms of time required to write the script versus plugging the numbers into a calculator. I include that script in this post for easy future access when needed. It also provides a chance to look at some Groovy advantages.

This is a fairly basic Groovy script, but it does remind one of how nice it is to strip away the normal ceremony of having a class and methods and all the other overhead to leave a fairly straightforward logic-heavy script. Some nice Groovy-isms are demonstrated including use of the implicit args handle, use of the as keyword to fluently cast the String arguments into BigDecimal instances, use of Groovy's (GDK) String.isNumber(), and use of placeholders in Groovy Strings for greater readability. It is really easy to quickly identify the core of this script, which is the mathematical functions used to calculate midpoint, slope, and distance.

The following screen snapshot shows a couple of examples of running this script on different values.

Conclusion

It says something for ease of use of a scripting language when it's easier to code up a simple script for performing calculations than it is to use a calculator to perform the same calculations manually. Of course, I could have programmed a sophisticated calculator to do the same thing, but I think it was probably easier to code this script than to go find my calculator or learn how to use and program the ninth grader's calculator!