Code Craft
The art, science and craft of writing quality software

Aug. 18, 2005 - Six / ten

Posted in Programming

This is real code.  Don´t laugh.

private static double six = 6;

private static double ten = 10;

 

/**

 * Utility method to convert int minutes to a decimal hours to

 * one decimal place.

 */

public String getHours( double minutes)

{

    double hours = java.lang.Math.round( minutes / six ) / ten;

    return String.valueOf(hours);

}

One has difficulty knowing where to start.  There are soooo many things wrong in just two lines of functional code.  Comments not matching function (int vs. decimal), bad function naming, possible missed opportunity to hold data in a Class, the possible use of the java Format class, the possible addition of a math library that would allow round( minutes / 60.0, 1), the list is just to big.

 

Let me just address the amazing use of six and ten.  Why would one possibly define quantities like six and ten?  I can only think of three possibilities. 

 

Possibility one: the coder is very concerned about possible future changes in the cardinal number naming system.  After all, just because 6 has implied the same quantity for a few thousand years one can never be sure that it might not change suddenly.

 

Possibility two: the coder has gotten religious about replacing concrete numbers with defined values (and yet not learned about the "final" keyword and capitalization conventions).  This is the worst kind of religion, pure form without understanding.  The advice is good but it doesn´t apply here.  The reason for using PI instead of 3.141592653589793d is that it is easier to type (accurately) and enhances meaning and readability.   

 

Even if the code was something like: "minutes = hours / MINUTES_PER_HOUR" I think you are complicating things.  "minutes = hours / 60.0"  is actually easier to read.  People will scan it and understand the meaning without any thought.  If it was "inches = feet * 12" the argument is less clear since some people will be unfamiliar with this conversion rate. 

 

Possibility three: the coder had to fix a bug and was just trying things.  This takes a bit of thinking, but bear with me as I follow the forensic trail.  The comment says it converts an "int minutes" so pretend that minutes is an int.  Taking that one assumption, it is possible that the code used to read: "double hours = java.lang.Math.round( minutes / 6) / 10;".  You´ve probably spotted the bug.  It´s the classic integer divide issue.  The round does nothing because the initial divide was in an integer context, so rounding would be "broken". 

 

If this was a very junior Java programmer, perhaps they couldn´t figure it out.  Perhaps they did but were unaware of how to express a number as anything but an integer (6.0, 6.0d, casting).  As they flailed away, they tried a lot of things and eventually got it to work.  This seems plausible to me, but in that case the lesson is clear.  If the answer works but feels wrong, go ask someone else!

 

 

Post A Comment!

Aug. 25, 2005 - safety in freedom

Posted by paul
Marvelous article. Good food for thought.

What do you think about cases where freedom languages add type-checking? For example, Lisp (surely one of the Freest of the Free) variables can be optionally declared to be of some type, and the language can optionally add type checking at runtime to make sure values are appropriate. That could be even "safer" than static type checking. Also, using metaclasses or decorators, Python functions could be made to check incoming and outgoing values for certain types.

Now, granted, those strategies are not widely used, and they make the code less "free" by introducing some of the obstacles you discuss. But perhaps it's possible that a language can be used in both ways-- even in the same codebase.
Permanent Link

Aug. 25, 2005 - wrong article

Posted by paul
I seem to have managed to post a comment on the wrong article. Wow.
Permanent Link

Aug. 25, 2005 - Good points

Posted by Kevin Barnes
Good points (even if they are in the wrong thread. I'll probably do a follow up peice at some point talking about some of the ways you can use things like dynamic type identification during unit testing to get back many of the things lost due to dynamic typing. It's all about your prject and what it needs, there is no need to feel religiously about these things.
Permanent Link

Aug. 27, 2005 - Easier to read but wrong (and not really easier to read)

Posted by Anonymous
"Even if the code was something like: ??minutes = hours / MINUTES_PER_HOUR? I think you are complicating things. ??minutes = hours / 60.0? is actually easier to read."

From the "complicated" version, by simple dimensional analysis the units on the right hand side are square hours per minute, so it's obviously wrong. "minutes = hours * MINUTES_PER_HOUR" has the right units. In the easier-to-read version there's no such sanity check.
Permanent Link

Aug. 27, 2005 - Insanely Funny

Posted by Kevin Barnes
Actually, the bug in my "code" is insanely funny, so rather than correcting it I'll leave it so that your comment stays valid. I swapped hours and minutes from the example. Of cource MINUTES_PER_HOUR being set to 60.0 (the correct value as there are 60 minutes in an hour) would have still been wrong so I don't buy it being any better. But at least we prbably agree that int six = 6; is bad code.
Permanent Link

Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb
<- Last PageNext Page ->

Kevin Barnes

Code Craft is the place for my thoughts, rants, ideas and occassional jokes on what it means to write code, why some people are better at it than others, and how we think about software in general.

Links

Home
View my profile
Site Feed (RSS)
Archives
Email Me
StorePerform
TechNexxus
Soul Craft (my stories and discussions)
My Wall

Joel on Software
Paul Graham
Patric Logan
Martin Fowler
Free Blogs
Free Blog



Copyright (C) 2005, Kevin Barnes. All rights reserved.

portfolio