To #Region Or Not To #Region

Code Practices Add comments

That truly is the question. A question that has surfaced today when a co-worker started bitching about the 4 layers of embedded regions in the code he was working on. If you’re not familiar with regions here’s a short explanation. In .NET you can surround your code with #Region and #Endregion to make it possible to hide those lines of code. The purpose is to make the code clearer by occupying fewer lines.

Use Fewer Lines Of Code!

As neat as that functionality is I have a better idea. User fewer lines of code. That will not only make it clearer, but it will most likely perform better as well. The long term advantages of minimizing the amount of code you use cannot be exaggerated.

Here’s an example and a quiz.

Your boss hands you a new assignment. Shockingly an end user has found a bug in your software. He needs you fix it. It’s not too hard for you to discern the location of the bug. Now here’s the quiz. Would you like to work on a bug in a file with:

  1. 488 lines of code
  2. 3627 lines of code

Yeah! That’s what I thought. Quantity is not the same as quality.

As luck would have it the file you’re working on has 3627 lines of code. It happens and as usual you don’t have time to refactor the code. Do you think it will be helpful to have 20+ regions in the code?

Not really, regions may have their purpose, but I’d rather have fewer code lines with useful comments.  To be conservative I would say that anymore 4 digits on the line count is too much, personally I’d prefer to keep it below 500, but that’s just me. So do me a favor the next time you have to scroll for several minutes before you reach the bottom of your code, try to see if you can refactor some bits here and there. There’s a lot to gain from doing so.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • MySpace
  • Technorati
  • Furl
  • Google
  • LinkedIn
  • E-mail this story to a friend!
  • Print this article!
Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio | SEO | Silver Cross Jewelry | Online Marketplace | B2B | Blogging | Barter | Entries RSS Comments RSS Log in