SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Review of Refactoring: Improving the Design of Existing Code

By Andy Warren,

This isn't a SQL book. In fact the code is in Java! So why should you be interested? Let's talk briefly about what refactoring is, then I'm hoping you'll be willing to read the rest.

The title says a lot - "improving the design of existing code". A simple definition of refactoring tailored to the SQL world would be to take an existing stored procedure and rework the code so that's better, faster, more elegant. Without the application or other users of the procedure knowing that anything changed. Sometimes it means taking a stored procedure that has a lot of duplicated code and moving the common part into a user defined function.

What this books discusses is how to make those changes in a controlled manner. Think of it as very low level design patterns. For example on p275 a technique called "Add Parameter" is discussed. It discusses the steps to follow. I'll paraphrase here:

  • Create a copy of the procedure
  • Add the parameter
  • Verify it compiles
  • Change the original procedure to call the new one, passing a default for the new parameter
  • Test
  • Find all references to the old procedure, change one at a time and test
  • Remove the old method
  • Test again

Adding a parameter to a stored proc or a code sub is something we do all the time. This book takes the time to lay out a series of steps to help you accomplish it quickly and safely. It also discusses for each "refactoring" why you should do and what the alternatives are.

There's a lot of good stuff in this book. The first four chapters talk about defining refactoring (far better than I have done here), core principles, code that "smells bad", and building tests. The rest of the book is really more a manual, for each refactoring it lists the reasons to use it, the mechanics of applying it, and often some good insight into whether a different approach might make more sense.

What is code that "smells bad"? It's how the author describes code that you look at that works, but just doesn't look right. Very very very long procedures. Bad variable naming. Etc. It's worth reading, because there really is a difference between code that works and good code. Most of us know it when we see it. But try explaining that to the developer!

The books discusses that you can't always get the design right the first time, that sometimes you won't know you need to refactor until you catch yourself about to copy/paste the code to create a minor variation of the first procedure.

This is a book that beginners would hugely benefit from, but they are just trying to get the syntax down and get something to work, thus they don't see patterns often. I think intermediate and advanced users that have enough experience to step back from the problem will find the book useful. Even better, once you realize that even realize that so many every day tasks can be explained as patterns, I think you have a powerful tool for trying to help the entry level/junior developer become more effective and productive.

I've not a Java programmer so I struggled some with the sample code and still found the book useful. I'll give it a solid five stars for interesting writing and for opening my eyes to a better way to solve problems. I'd really like to see a VB or SQL version of the book!

You might also want to visit the authors site and the Refactoring Home Page. Buy it from Amazon for about $45.

Total article views: 3490 | Views in the last 30 days: 0
Related Articles

survey about refactoring

it would be pretty if you could help us by answering 14 short questions about refactoring


A Review of SQL Refactor

Longtime author Dinesh Asanka takes a look at one of the new tools on the market, SQL Refactor from ...


Refactoring in SQL Server Data Tools - SSDT

In this post I will talk about the in-built refactoring support in SSDT – the language is slightly d...


More LINQ Discussion

Steve Jones recently posted an editorial about LINQ and the resulting discussion encapsulates most o...


need help about procedure


book reviews