TalesFromTheSmellySide<Code> - Episode #1

posted @ Monday, October 29, 2007 2:47 PM

 

What is this?

Well, it was inevitable that my long stint of greenfield projects was going to come to end at some point, especially since I'm in the consulting world.  So now that I'm starting to find myself working in some pretty scary code bases again, my fellow LosTechies dude suggested I start blogging about my experiences. 

And since I've already seen many, many things that make me say "huh?" and, at times, brings tears to my eyes, I figured I'd give this first post a number (assuming there are many more coming).

Ok, so really this is just an opportunity for my fellow developers to perhaps not feel so bad about the code bases they are working in, because maybe, just maybe, someone else out there has it worse.  (As I'm sure there are folks who are forced to work in stinkier code than I...)

In order to make these somewhat useful, I'm making them all about the code.  Smells that I see, and hopefully some simple solutions for fixing them, leading to more maintainable and readable code.

Episode #1

catch (Exception ex)
{
    if (ex.GetBaseException().Message.Contains("You gotta know this just *feels* wrong!"))
    {
        // do something
    }
}

This one definitely had me scratching my head.  I can honestly say this is the first time I've ever seen a conditional expression based on the actual string message on an exception.   Scary, is it not?

Of course the first way to correct this smell that jumped out at me was just to simply create a custom exception and catch it in its own catch block.

catch (SomeCustomException ex)
{
    // do something
}

 

What kind of smelly exception-related code have you seen and corrected?

Comments
Jimmy Bosse - 10/30/2007 10:36 AM
# re: TalesFromTheSmellySide<Code> - Episode #1
Well these posts should sure be an adventure, but c'mon Steve, ease up. Firstly as "developer" who fell into application development in the .com craze and who's "official" computer training was pascal and fortran in college, I can say there are many developers out there who have had the misfortune of not being young enough to have been in high school when OO gained a foothold and don't have the endless amount of free time a student does to play with all the new toys.

Sometimes when the deadlines are tight and our experiance is limited, simply not getting an error message is good enough. I have the pleasure of now being an in house developer maintaining a .Net 1.1 application I myself developed for the company when the company was just a client and our #1 priority was meeting deadlines not worrying about long term maintanability. I can smell my code when I get on the elevator every morning! ;-)

I think this series will be an entertaining look at how WE ALL have the pleasure on looking back at yesterday's code with today's knowledge and revel in how much better we can make the code for tomorrow. But rest assured the next guy in that code after you will also be wondering what the hell you were thinking.
Post Comment






Please add 5 and 5 and type the answer here: