Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Dec 2013

Where's Windows?

by in CodeSOD on

Java was touted as write once, run anywhere to emphasize its cross-platform advantages. Of course, the devil is in the details, and to interact with the local operating system, you still need to know something about it.

Without a built-in facility, the only real way to figure out the local OS is to try to exec commands unique to each OS and then check to see which ones succeed, and what value(s) get returned. For example, uname -a provides useful information on *nix systems, but is useless on Windows. It can be as painful as trying to find Waldo.


Don't Wear Out Your Methods

by in CodeSOD on

Each year, whenever summer weather faded and the air started to turn brisk, Craig knew that it could mean only one thing - a new round of interns would be arriving in the department. Sure, some were markedly better than others, but a little bird had mentioned to Craig that his intern-of-the-year actually had some coding experience on his resume!

Maybe this was why Craig let his intern run loose with what he had considered a simple spec. It was meant to be a learning task. You know - get familiar with checking code in and out of source control, explore the code base, and so on. The result? A 2,300 line source file named "Functions".


Calling All Zip Codes

by in CodeSOD on

It's been an active couple of months for O. Z.'s group.

In an effort to combat keying errors and all around bad data, some long overdue validation was added by one of O. Z.'s co-workers to several of the screens, and perhaps most importantly, to the address entry screens.


Printing Decimal Numbers is HARD!

by in CodeSOD on

Decimal numbers are sometimes difficult to work with because they can't always be stored with exact precision. This also leads to difficulty in displaying the value of a decimal number because you need to deal with getting the precision right. Fred G. found this bit of ingenuity:

  // File format
  //
  // ABBBBBCDDDDDEEEEEEE...
  //
  // A - bytes 0-0   integer portion of field x (range: 0-9)
  //     (done this way because we can not represent a decimal number exactly)
  //
  // B - bytes 1-5   decimal portion of field x (range: 00000-99999 to represent 0.00000-9.99999)
  //     (done this way because we can not represent a decimal number exactly)
  //
  // C - bytes 6-6   integer portion of field y (range: 0-9)
  //     (done this way because we can not represent a decimal number exactly)
  //
  // D - bytes 7-11  decimal portion of field y (range: 00000-99999 to represent 0.00000-9.99999)
  //
  // E - bytes 12-18 discount rate (range: 0.00000-9.99999)
  // ...
  public class Xyz {

    public class Data {
       private double ab;
       private double cd;
       private double e;
       // ...

       public Data(double ab, double cd, double e /* ... */ ) {
         this.ab = ab;
         this.cd = cd;
         this.e  = e;
         // ...
       }

       public double getAB() { return ab; }
       public double getCD() { return cd; }
       public double getE()  { return e;  }
       // ...
    }

    public String createDataToWriteToFile(List<Data> list) {
      DecimalFormat df1 = new DecimalFormat("0.00000");
      DecimalFormat df2 = new DecimalFormat("00000");
      DecimalFormat df3 = new DecimalFormat("0");

      StringBuilder sb = new StringBuilder();

      for (Data d : list) {
          String s = df3.format(Math.floor(d.getAB())) + 
                     df2.format((d.getAB() - Math.floor(d.getAB()))*100000) +
                     df3.format(Math.floor(d.getCD())) + 
                     df2.format((d.getCD() - Math.floor(d.getCD()))*100000) +
                     df1.format(d.getE());
          sb.append(s);
          sb.append("\r\n");
      }

      return sb.toString();
    }
  }

The Uninstalling Installer

by in CodeSOD on

From a technical standpoint, Digital Rights Management is a WTF- it’s a crypto system which requires the user to have access to the key, and simply hopes to make getting the key out of the device too cumbersome to be worth a pirate’s time. The shoddy technical reasoning behind it sometimes leads to shoddy technical implementations.

Benjamin tried to help a friend install a DRM client on their machine. The installer continuously failed, and perhaps foolishly, Benjamin decided to run it with elevated permissions. After restoring from backup, Benjamin cracked open the installer and looked at some of the shell scripts that it ran.


Fixing Delphi

by in CodeSOD on

Delphi has a rounding function that uses bankers' rounding instead of mathematical rounding, which can cause issues in mathematical applications. Throughout the years, folks have used numerous solutions, frequently rewriting the round function to use mathematical rounding, or else adding both a bankers rounding function and a mathematical rounding function to allow for both methods. These have typically been only a few lines long.

Paul M. was upgrading some legacy code when he happened upon a unique solution to "fix" the Delphi rounding issue:


Exceptionally Hard to Swallow

by in CodeSOD on

Does a tree make a sound if it falls in the woods and there's nobody there to hear it?

B. M.'s coworker was the stuff of which legends are made; code was always delivered ahead of schedule and it never threw an error. Her code was the least buggy in the entire department, and so became the standard against which all other code was measured.