Blog Closed

This blog has moved to Github. This page will not be updated and is not open for comments. Please go to the new site for updated content.
Showing posts with label BadCode. Show all posts
Showing posts with label BadCode. Show all posts

Monday, February 15, 2010

Software Anti-Pattern: The Exception Repeater

Since I've seen this pattern several times in a software project I'm trying to maintain, I've decided to give it a name: The Exception Repeater anti-pattern. Here's a particularly succinct implementation of it, direct from the repo:

public static void ValidateData() {
try {
ValidateDataInner();
} catch (Exception e) {
throw e;
}
}

Since I've seen this exact thing several times in the codebase today, I'm wondering if maybe it has some kind of magical benefit or side-effect that I'm not aware of. The only benefit that I can think of is that an unintelligent programmer can claim to be programming "defensively", or something like that.

Wednesday, February 10, 2010

So Bad It's Funny

At work I've been taking a program written by another engineer and trying to merge the functionality into a program that I've been writing. During this process, I have found some code so bad that it's both funny and depressing at the same time. Here is a great example:
[Update 10 Feb 2010: This code is C#]

public bool CheckMsgValid(byte[] _myArray, int _myIdx) {
bool t;
t = ((_myArray[_myIdx] & 0x80) == 0x80) ? true : false;
return t;
}

...which is basically the long way to write:

public bool CheckMsgValid(byte[] b, int x) {
return b[x] & 0x80 != 0 ;
}

I guess the original coder wanted to make double-sure that the two values were equal.