SQLServerCentral Editorial

What's a Code Smell?

,

This editorial was originally published on Oct 5, 2015. It is being re-run as Steve is out of town for VS Live.

We all have a variety of code patterns and practices that we follow. Most of them were probably picked up along the path of our career. A suggestion from a colleague. A piece of sample code that solved a problem. A performance tuning trick that stopped our phone from ringing. These methods of learning are the way that most of us actually grow our skills over time.

However just because we learned something, or because a technique solved a problem doesn't mean it was a good piece of code. In fact, often the code we may think works well might not be the most efficient way to structure the code. Many developers have learned this over the years, as they read about new techniques that are more efficent, elegant, or just simpler.

Kent Beck and Massimo Arnoldi coined the term code smell years ago, as a way of noting the development patterns and practices that  lead to poorly written, or difficult to maintain code. There have been other attempts to document practices which are not recommended, though the success is probably limited as many developers continue to build on poorly written code rather than refactoring and cleaning their codebase over time.

Simple Talk  and Phil Factor published a SQL Code Smells ebook awhile back, trying to document the signs of poorly written T-SQL. The book is good, with guidance about particular patterns that can cause you problems over time. The items aren't meant to be rules, but rather guidelines that you adhere to unless you have a good, specific reason that you can justify to others.

I ran into a code smell recently where a developer noted that their application depended on a specific database name in order to work.  That's not in the ebook, but I think it's easily one I'd avoid. My connection should determine the database, not the application itself. I know there may be exceptions here, but in general, application code shouldn't be dependent on a particular name.

I'd urge you to pick up the ebook (it's free) and keep it handy. See if any of the items listed are habits you might have picked up over time and not realized that they are, in fact, poor practices. I would also recommend you peruse Aaron Bertrand's Bad Habits to Kick series, as a way of improving your own code.

Rate

4.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

4.5 (2)

You rated this post out of 5. Change rating