#region == Failcode

imageThis is a very old subject. But thanks to a way to motivated workmate, who used to drop “regions” in every code, we talked about this subject again. Are #regions good or not?

For all of you who don´t know what the hell Im talking about: #region on MSDN.

Let´s take a look on an example:

image

“…” stands for countless other stuff.

First impression:

Nice and tidy.

But serious…

Im interested in the Code not in any kind of blocks. With the use of #region the only thing you do is to shroud the code. When I open the user.cs I want to see the code on the first sight.

Yes, there is a hot key to make the code visual but why should I use this #region blocks?

Code Smell

Often I find some #regions in classes where the code consists of endless lines to give it a little bit more structure.

For example you open a method and find this:

image

I have the problem that I even so want to see the code. So I click to open it.

image

Fascinating…. but wait… what happens on Y?

image

Every good thing is three…. What´s written behind Z?

image

Fantastic isn´t it? Of course the code in my example is nonsense but in fact there could be kilometres of code behind X,Y and Z.

Why I think #regions is useless sense of order:

First fact: there is not a really advantage. Okay it looks nice on the first sight but in the end there is one possible conclusion:

There are too many responsibilities in this class and somebody try to hush this up. Better: Split it! And with this I didn´t mean into “Partial Classes” but into separate function units.

Interface Implementation:

Another example: Visual Studio always put interface implementations in #regions. I don´t think that´s nice (and its possible to stop it ;) ). Is it really helpful for structure? In my opinion there are way better opportunities since VS2010. So I don´t need any #regions.

In addition…

If you are creating “On-the-fly” codes (e.g. with the TDD Manier), Visual studio used to place the code somewhere as well. That proofs that the system of #regions is to be doomed to failure anyway.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

2 Responses

  1. If you’re using regions inside methods. Then your code fails because it’s obviously doing too much.

    But theres nothing wrong with using regions outside of methods to group stuff.

    Unless you group into ‘…’ which fails.

    Reply
  2. Theyre not useless, they can just be abused just like any other language feature.

    Consider the following code:

    public static readonly string NamePropertyName = “Name”;
    private string _name;
    public string Name{
    get {return _name;}
    set {_name = value; RaisePropertyChanged(NamePropertyName):}
    }

    now, would you prefer seeing that code repeated over and over again for each property or would you prefer to see a region wrapping it?

    Reply

Comment on this post

Recent Posts

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us