Jeff Atwood recently wrote about regions in the Visual Studio IDE and Eric Gunnerson followed up with a post on the same topic. I was crusing the internet this morning looking for pictures of kittens things to write about and since regions are apparently the hot topic de jour, I figured I’d discuss (not to mention that I have discovered one very good way to get more traffic to your blog is to link to other, smarter, more well-read people and wait for the trackback to kick in) regions too.
So I’ll lay it right out there: I like regions in code. I also like drinking, eating, gambling and driving fast. What do all of those things have in common? They are perfectly OK when you use them in moderation with reasonably strict guidelines on what you are doing. If you do any of them to excess, you end up like Hunter S. Thompson and you have your ashes blown out of a cannon by Johnny Depp. Wait a minute, that sounds awesome! I digress.
I think regions are great when used in moderation. I use them to organize code files into logical areas like Public Methods or Private Fields. This enforces discipline across a code base (which typically I don’t need because I’m obsessive compulsive about such things but that’s another topic) which contrary to Jeff’s assertion, actually makes code easier to read because it’s well organized. Reading things that are disorganized is difficult and code is no exception.
Of course, when used to excess, regions really are evil. Nested regions make me want to waterboard puppies. I should be able to click one little expansion point and be able to see the code at hand. If I have to click three little expansion points, I’m liable to give up and just rewrite your code. Using regions as comments is just wrong. Use comments for that. Yes, I know that sounds so avante garde but give it a try.
In the end, I think using regions for organizing code files is perfectly valid. Lots of other smart people disagree with me but just because they are smart doesn’t make them right.
The Daily Palliative: Tricking Grad Students