CodeSOD: Prepend Eternal

This post was originally published on this site

The Daily WTF

Octavia inherited a decade old pile of C#, and the code quality was pretty much what one would expect from a decade old pile that hadn’t seen any real refactoring: nothing but spaghetti. Worse, it also had an “inner platform” problem, as everything they put in their API could conceivably be called by their customers’ own “customizations”.

One small block caught her eye, as suspicious:

public void SomeFunctionality { // Other functionality here int x = SomeIntMethod(); String y = PrependZeros(x.ToString()); // Do other things with y here }

That call to PrependZeros looked… suspicious. For starters, how many zeroes? It clearly was meant to padd to a certain length, but what?

public String PrependZeros(string n) { if (n.Length == 1) { return “00” + n; } else if (n.Length == 2) { return “0” + n; } else { return n; } }

We’ve reimplemented

To read the full article click on the 'post' link at the top.