Saturday, March 17, 2012

So pretty please, with sugar on top; stop using #region

Something that is common in .Net applications is the use of #region:
#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. 
This blog post is going to be dedicated to how much I hate it. Why do I have such disdain for such a practical feature? Because it hides how complex and messy the code is.
My favorite example of this abuse of a practical tool is System.Windows.Control.DataGrid; as of PresentationFramework version 4.0 this "component" is 8046 lines of code separated by 30 top level regions. It inherits from MulitSelector which is a mere 112 lines, which inherits from selector, which is 2474, and so on....  Just because a component or class is feature rich does not mean it cannot be modular. DataGrid could be broken up into multiple classes, collaborators, and strategies that will make it much easier to reason about.
I encourage you to stop using #region and to start writing modular code.

No comments: