Saturday, January 15, 2011

Should We Refactor Old Code

Technology is growing so fast that if we do not keep us upgraded, we may stand far behind in the techno race. Every developer will agree with this that he can code a better solution whenever he looks back at his code after few months. Someone may disagree by saying that why should he touch the stable code. Let me explain this with an example.
Suppose, you have coded the following script in your SQL Server stored procedure.
DECLARE @unitPrice int
DECLARE @qty int
DECLARE @total int
DECLARE @grandTotal int

SET @grandTotal = 0

DECLARE cursorTxn CURSOR FOR
SELECT unitPrice, qty
FROM Transactions

OPEN cursorTxn
FETCH NEXT FROM cursorTxn INTO @unitPrice, @qty

WHILE @@FETCH_STATUS = 0
BEGIN
       SET @total = (@unitPrice * @qty)
       SET @grandTotal = @grandTotal + @total

       FETCH NEXT FROM cursorTxn INTO @unitPrice, @qty
END

CLOSE cursorTxn
DEALLOCATE cursorTxn

This snippet works fine, calculates the correct grand total of all the transactions and return the result to the caller. One can say that it is a stable procedure and does the job well. However, if he checks his code after few months and he has already polished his skillsets, he may call this piece of code a crap. He can easily upgrade this piece of code by using the following alternative.
DECLARE @grandTotal int

SELECT @grandTotal = SUM(unitPrice * qty)
FROM Transactions

The above code will return the same result but in a much optimized way.

So, instead of rewriting your code, do refactor your code.

7 comments:

  1. I do agree with you.. I got a chance to see code i wrote couple of years ago.. I was literally stunned to see the way i used to code.. but we learn things by experience.. its always nice to evaluate yourself to see in past..

    ReplyDelete
  2. True. both experience and knowledge. Review the old code sometime opens the door to polish your old code.

    ReplyDelete
  3. Good certification books also help a lot if these been studied seriously. for instance, following both methods work ok to set Boolean variable based on some condition, I learnt the optimized one during MCSD. and believe me many senior fellows did not know about it.
    and even after knowing , afraid to try this to avoid any issue in their work ahhhh.... what to do when you have experience , knowledge but no confidence?

    dim blnRecordFound as Boolean
    dim rsData as RecordSet
    .......some record set open code..........

    .... apply check ......
    If rsData.RecordCount>0 THEN
    blnRecordFound = True
    ELSE
    blnRecordFound = False
    END IF


    ' Optimized one
    blnRecordFound = rsData.RecordCount>0

    ReplyDelete
  4. There is another approach to identify your coding mistakes by showing your code to some experienced developer or your colleague. One problem can be solved in different ways, but we should strive to find the best solution. Secondly, if we read code of other developers we may find better approaches to solve identical problem.

    ReplyDelete
  5. If rsData.RecordCount>0 THEN
    blnRecordFound = True
    ELSE
    blnRecordFound = False
    END IF

    above is a common problem that most of the developers would be doing in their daily professional life.
    So I believe its necessary to keep your mind active about what you are doing not just writing everything (logic) that is going on in your mind.

    In terms of speed it would make MUCH difference but when you are developing a software with huge codebase you have to consider "Code Optimization and Management" as a key thing

    ReplyDelete
  6. it wouldn't make MUCH different i meant

    ReplyDelete
  7. That's correct. We should spare some time to look back and analyze our code units and fix it right away. Majority of us do not create unit tests and then afraid of taking risks to refactor it which may create a ripple of disaster into our whole code base. Documentation, comments, and unit tests are the helping hands to craft the maintainable code.

    ReplyDelete