Friday 15 February 2008

The Evils of regions#region

I still remember when I first used the region field, it was a happier time, I was blissfully unaware of the problems that regionalizing your code was creating, I think everyone was. I picture the region directive being added as almost an after thought. I see someone at Microsoft saying "wow this tool sure generated so butt ugly code cant we hide that?" Regions to the rescue! A region is a preprocessor directive that allows programmers to hide sections of there code by using #region and #endregion syntax.
MSDN says:
#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor.
For example:


#region MyClass definition
public class MyClass 
{
static void Main() 
{
}
}
#endregion


Regions the clean solution
Now I know what you might be thinking. What is so bad about that? How could that little region be causing so many problems? It is only there to help clean up your code a little. Ahh but wait. Lets take a closer look at that. It helps clean up your code. Does it? Or does it just make the code LOOK cleaner.

Regions making the ugly look pretty

Sadly, the same thing that spawned the region directive is the same thing it is being misused for now. I will just make a little hack here or there add a region directive and everything will be OK. The sooner everyone stops using regions the sooner developers will stop thinking its Okay to sweep their hacks under a rug that I find myself constantly cleaning.

No comments:

Post a Comment