I just read a post by Casademora on “public abstract string Blog()”
arguing that not only should we NOT use code regions, but if we do, we are hiding “bad code.” He uses words like “retarded,” “lame excuse for a preprocessor tag,” etc.
You’d think maybe this guy (gal?) just had a bad day when he wrote this, but no, he goes on to further defend his position in a later post.
And what I don’t understand at all is how his original post ended up on DotNetKicks today. It’s an old post that was written last year but was just kicked up to the front page? I figure someone MUST agree with him or it wouldn’t be there.
So, are Regions really as evil as Casademora argues that they are?
First what is a region?
#region Member Variables private bool _refreshState; private bool _isRefresh; #endregion Member Variables
Why do they exist?
Any code you surround with a region is collapsible, just like your methods and properties are. If you collapse the region above, all you see in your editor is:
Are they hiding “bad” code?
The main argument that Casademora makes is that the use of regions hides “bad” code. He defines bad code as:
Oh… wait! He never defines “Bad” he only defines “Better.”
But I doubt that anyone could argue that declaration of private member variables is “bad” in any sense of the word. In fact this code is from one of the best programmers I know. Here’s a screen grab of the methods in this file:
And the code in those methods is just as pretty. In fact, Mike’s code is consistently well structured, well organized, follows good naming conventions. I wish my code looked as good.
But when you first open the file, this is what you see:
I don’t know about you, but I always feel like I’ve been hit with a breath of fresh air when I open Mike’s code. Instead of being assaulted with 201 lines of code (that’s how many lines are in this file) I see this.
So the question is, would Mike’s code be as organized without the regions? So I asked him.
He says, “I’d try to do it anyway, but the regions make it easier. Like, if I need to add a new private method, I go right to that region. I find it most useful to separate out the properties and event handlers.”
So, contrary to Casademora’s statement, regions actually help Mike structure his code so that it is easy to read. They aren’t hiding “bad” code. If anything they are “hiding” good code.
Hiding Code is Good
When you are working on code with lots of properties, methods, and event handlers. Do you really need to see the member variables at the same time you are working on an Event handler? Most of the time we don’t.
Regions, used correctly, make your code more readable, and hide code you aren’t working on so that you can easily find and work on the code you do need to work on. That’s the whole point.
I’m sure one could find uses of regions that have nothing to do with code structure. Uses that obfuscate the code rather than making it easier to maintain. But if you use the code in a logical way, they can significantly increase the readability of your code.
Other post in c#
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Simple Properties in C# 3.5 - November 22nd, 2007
- CSharp adds the var keyword! - November 27th, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- Computed Columns Using LINQ to SQL Classes - February 12th, 2008
- DataSets, ObjectDataSource and 3-Tier ASP.NET - February 29th, 2008
- Serializable attribute vs Serializable Interface - April 9th, 2008
- Why you can't cast an integer to a string. - June 23rd, 2008
- Unsafe Mode in C# - December 15th, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Upload a File via WebRequest Using CSharp - November 10th, 2009
- Azure CDN Blob Storage Case Sensitive Issue - April 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Decimal vs Float (Single) or Double - September 18th, 2012
- C# Self Executing Anonymous Function - February 28th, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Enum - October 2nd, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- C# Properties Get and Set - November 13th, 2014
- Value Type vs Reference Type - November 27th, 2014
- String and StringBuilder - December 11th, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015