CodeSOD: Bitmaps and Streams

Robert has inherited a .NET application. It's a big-ball-of-mud, full of monstrous classes of thousands of lines and no coherent purpose, complete with twenty constructors.

It's ugly and messy, but it's mostly just generically bad. This method, however, is a lot of bad choices in very few lines.

/// <summary> /// TODO implement !!! /// </summary> /// <param name="file">The file.</param> /// <returns></returns> public static Bitmap WriteFileFromByte(byte[] file) { Bitmap retVal = null; using (MemoryStream ms = new MemoryStream(file)) { retVal = new Bitmap(ms); } return retVal; }

We start with a TODO, one of my "favorite" comments. The method is called WriteFileFromByte, but that's exactly what doesn't happen.

Instead, this converts a byte array into a MemoryStream, and then creates a Bitmap off of that stream. That's actually all fine. We need to convert the byte array into a stream so that the Bitmap class can ingest it.

So, what this method does is simply convert a byte array into an image object. You wouldn't know that from the name, you wouldn't know that from the comments. The one thing that hints at a deeper WTF is that the byte[] is called file, which implies that we loaded this data from a file- when we could have just used a stream in the first place.

Oh, there's one more WTF. This method has been in the code base for a long time- but it's actually never been invoked anywhere. It's a dead method with a bad name and there are a bunch of other, similarly bad and confusing methods, which do the same task but in different (and frequently harder to use) ways.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.