web 2.0

The Significance of Code Reviews

I can not stress to people enough about how important it is to do a code review. This is especially true if you have a staff of young developers on your team. Unfortunately, in my company we tend not to do code reviews. It mainly stems from the fact that there are no "Lead Developers" to mentor the less experienced developers. Now even though code is written with the best intentions there is always a right and a wrong way to do things. However, presenting your arguments to others needs to be done in a positive manner. The goal is not to criticize! We are simply just trying to show them a better way of doing things. Code Reviews are a great platform for learning.

 

As a DBA I typically end up getting phone calls from end users who are complaining about performance. The phone call then precedes by a SQL trace. Once I review the results of a trace, I can typically get an idea of how the application was written. In order to fix the problems, I occasionally will ask the developers for source code. When I do this, I typically get a strange, confused look because most of the developers I work with do not think that DBAs can read code. After some hesitation they will give me the code but only because they think that I won't know enough to find any problems with it. Anyway, here is a stored procedure that I recently discovered. This procedure was used to populate a combo box on a form. This procedure would have been executed every time the form was displayed.

 

   1:  ALTER PROCEDURE uspGetMonths
   2:  AS
   3:   
   4:  CREATE TABLE #tmpMonth(
   5:      Ident INT ,
   6:      Months VARCHAR(15)
   7:  )
   8:   
   9:  INSERT INTO #tmpMonth(Ident, Months) VALUES(0, '(Select One)')
  10:  INSERT INTO #tmpMonth(Ident, Months) VALUES(1, 'January')
  11:  INSERT INTO #tmpMonth(Ident, Months) VALUES(2, 'February')
  12:  INSERT INTO #tmpMonth(Ident, Months) VALUES(3, 'March')
  13:  INSERT INTO #tmpMonth(Ident, Months) VALUES(4, 'April')
  14:  INSERT INTO #tmpMonth(Ident, Months) VALUES(5, 'May')
  15:  INSERT INTO #tmpMonth(Ident, Months) VALUES(6, 'June')
  16:  INSERT INTO #tmpMonth(Ident, Months) VALUES(7, 'July')
  17:  INSERT INTO #tmpMonth(Ident, Months) VALUES(8, 'August')
  18:  INSERT INTO #tmpMonth(Ident, Months) VALUES(9, 'September')
  19:  INSERT INTO #tmpMonth(Ident, Months) VALUES(10, 'October')
  20:  INSERT INTO #tmpMonth(Ident, Months) VALUES(11, 'November')
  21:  INSERT INTO #tmpMonth(Ident, Months) VALUES(12, 'December')
  22:   
  23:  SELECT    
  24:      Ident ,
  25:      Months
  26:  FROM
  27:      #tmpMonth (NOLOCK)
  28:  ORDER BY
  29:      Ident ASC
  30:   
  31:  DROP TABLE #tmpMonth 

Immediately after seeing this procedure I asked the developer the following questions:

  1. Why would you use a stored procedure for this?
  2. Why didn't you just hard code the month values in a combo box. It is not like the month names are going to change!
  3. Is there a reason to make a round trip to the server just to populate a combo box with a list of month names in it?

Here is how I would rewrite this in C# code:

   1:  public Form1() {
   2:     InitializeComponent();
   3:     comboBox1.DataSource = new BindingSource(GetMonths(), null);
   4:     comboBox1.DisplayMember = "Value";
   5:     comboBox1.ValueMember = "Key";
   6:  } 
   7:   
   8:  private Dictionary<int,string> GetMonths() {
   9:     Dictionary<int, string> results = new Dictionary<int, string>();
  10:     results.Add(0, "(Select One)");    
  11:     for (int i = 1; i <= 12; i++) {      
  12:         DateTime myDate = new DateTime(2000,i,1);
  13:        results.Add(myDate.Month, myDate.ToString("MMMM"));
  14:     }    
  15:     return results;
  16:  }

Now even though this particular procedure would probably not be considered a major performance killer it definitely does not help the cause. If this procedure was deployed to production it would have been executed repeatedly and caused a lot of unnecessary network I/O. In my opinion, this is a perfect example of how a code review can save everyone a lot of time and aggravation. If you are not doing code reviews with your team then start doing them now. No excuses!

Comments

Michael Ceranski , on 12/17/2009 2:29:57 PM Said:

Michael Ceranski

That is a good suggestion. I wasn't aware of the possible sorting issues with a Dictionary class. MSDN does not really even document it on their page but I did find a few people who mentioned it on some forums. Thanks for the heads up.

Michael Ceranski , on 12/17/2009 2:29:57 PM Said:

Michael Ceranski

I would like to say that I was checking to see if anyone would catch it but that would be a lie. Thanks for pointing it out. Just goes to show the value of a code review!

Michael Ceranski , on 12/17/2009 2:29:57 PM Said:

Michael Ceranski

I can see why people would adapt such a strategy. People are very passionate about the code that they write. In my experience people tend to take suggestions about coding negatively. I will check out the whitepaper.

I did recieve the free book from your website a couple of years ago. It is a good read.

Anonymous , on 12/17/2009 2:29:57 PM Said:

Anonymous

I wish our company spent time doing this... but everything is rush, rush... so very little is reviewed. So it places a big burden on the testers.

CK , on 12/17/2009 2:29:57 PM Said:

CK

Great post! I fully agree with you, code reviews are a great way to avoid errors and learn, especially for new team members.

btw, why did you create the "now" DateTime variable in the GetMonths() method?

ppetrov , on 12/17/2009 2:29:57 PM Said:

ppetrov

Hi Michael, I agree with you about the code review and about the unnecessary db road trip.  However I have a little concern about Dictionary<int,string> the order of elements in the dictionary in not guaranteed to be the order of their insertion. So you can end up with completely random ordering of months in the combo box. Please consider to change you collection. Probably something like SortedList<TKey, TValue>

gsporar , on 12/17/2009 2:29:57 PM Said:

gsporar

>The goal is not to criticize! We are simply just trying to show them a better way of doing things.

You can actually even go a step further than that, based on the specific situation you are in.  Some teams restrict code review just to pointing out problems and specifically do *not* try to point out solutions.  In other words, to avoid bogging down the review process, focus on identifying places for improvements but don't get bogged down in the details of how to fix something. Naturally, each team is different so your mileage may vary.  Smile

FWIW, additional tips are available on our web site: http://smartbear.com/codecollab-white-paper.php

Comments are closed