Screaming to Refactor

I am in the middle of coding some change for our next incremental release of the software. After implementing some changes in a stored procedure, I ran my tests. Something was still not right. I traced it down to a trigger calling a stored procedure. It was undoing my changes on the fly. I needed to make another change.

When I got into the other stored procedure, I was lost. The procedure was 850+ lines of code. There were nested IFs that were six, seven, and even eight levels deep. I was around when this procedure was changed over the years. And I still was getting confused over the thing. I decided to print out the code, and line up the IF and ELSEs.

My goal was to refactor this massive procedure. The plan was to break down this monster proc into many subroutines. In the end, the main procedure got down to 150 lines of code. It was much easier to follow. Of course all the lines of code went to small subroutines. I needed to do a bit of regression testing to make sure I did not break anything. With a change of this magnitude, there are going to be some bugs introduced. My unit tests helped flush them out. I am leaving this procedure is much better shape. I am proud.