2

Personally I can't stand region tags, but clearly they have wide spread appeal for organizing code, so I want to test the temperature of the water for other MS developer's take on this idea.

My personal feeling is that any sort of silly trick to simplify code only acts to encourage terrible coding behavior, like lack of cohesion, unclear intention and poor or incomplete coding standards.

One programmer told me that code regions helped encourage coding standards by making it clear where another programmer should put his or her contributions.

But, to be blunt, this sounds like a load of horse manure to me. If you have a standard, it is the programmer's job to understand what that standard is... you should't need to define it in every single class file.

And, nothing is more annoying than having all of your code collapsed when you open a file. I know that cntrl + M, L will open everything up, but then you have the hideous "hash region definition" open and closing lines to read.

They're just irritating.

My most stead fast coding philosophy is that all programmer should strive to create clear, concise and cohesive code. Region tags just serve to create noise and redundant intentions.

Region tags would be moot in a well thought out and intentioned class.

The only place they seem to make sense to me, is in automatically generated code, because you should never have to read that outside of personal curiosity.

Nicholas
  • 3,286
  • 5
  • 27
  • 35
  • 2
    This is a blog rant, not a question. – tvanfosson Jan 01 '11 at 14:40
  • Actually, I am asking what other people think about regions. So yes, it is a question – Nicholas Jan 01 '11 at 14:42
  • @tvanfosson I am assuming that once I have gathered enough points, I would be able to contest the premature closing of this discussion. I don't see why open ended questions without black and white responses should be closed. Confrontation and argument could also be phrased as opposition and discussion. This is often the spirit of programming, and presenting arguments for or against ideas that affect our daily work seems like a pretty worthy discussion to have. – Nicholas Jan 01 '11 at 19:56
  • @Nicholas -- have you read the FAQ? There probably is a way to ask this that wouldn't be closed, but this really reads more like an opinion looking for an argument than a question seeking an answer. For example, "What are some good uses of the region directive?" or "Are region directives a code smell?" -- and leave out the opining in the question itself, though you could contribute an answer containing your thoughts. Even so, I don't know that I could promise that it wouldn't be closed, though I probably wouldn't vote to close it if it really didn't pre-suppose an answer. – tvanfosson Jan 01 '11 at 20:23
  • @tvanfosson Hmmm... I suppose I was being a little strong in my opinion. Fair enough... I'll keep it in mind for next time. – Nicholas Jan 01 '11 at 20:32
  • @Nicholas: This is clearly not a proper question. You just want to vent your own opinions and are not prepared to listen to other peoples opinions. Also, you lack proper manners when it comes do conducting a discussion, so there is no point in continuing it. This is not a place for a debate like this, and you need to change your attitude if you want to have it anywhere at all. – Guffa Jan 01 '11 at 20:52
  • @Guffa you have clearly taken my comments too personally. I do not agree with your reasoning, and do not in any way find it rude to say so. If you notice below I actually upvoted someone who does not hold my same opinion, so again, I don't agree with your reasoning. Thanks for the down vote! – Nicholas Jan 01 '11 at 21:38
  • @Nicholas: I'm sorry that you can't realise when you are being rude. – Guffa Jan 01 '11 at 22:09
  • @Guffa your sincerity is very kind! – Nicholas Jan 01 '11 at 22:17
  • [Related](http://staticola.com/Blog#on_code_regions) – Robbie Dee Jul 25 '16 at 22:14
  • @robbiedee beautiful! – Nicholas Jul 27 '16 at 21:07

6 Answers6

8

I like regions, and use them all the time. I use them to group members of the same kind inside classes.

You already have the means to collapse methods, classes and namespaces in the editor. Regions give you an option to create another level that lets you arrange the code in a way that corresponds to what you think is important.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Right, I've heard this argument before, but good intent revealing names should still make regions moot. Why create extra noise in a class that is well constructed and clear. And collapsed default behavior collapses all of your well intentioned grouping... which is irritating when reading someone else's code. – Nicholas Jan 01 '11 at 14:54
  • @Nicholas: The best names don't make the code literally disappear. – Puppy Jan 01 '11 at 15:04
  • @DeadMG That might be true, but why would you want code to artificially disappear? You need to read it to understand it. If it exists, I want to read it, and I don't want those noisy #region tags to get in my way. – Nicholas Jan 01 '11 at 15:10
  • 1
    @Nicholas: Most of the time when maintaining code you only need to see a specific part of it. If you need to see all of the code all the time, then the code is badly designed. – Guffa Jan 01 '11 at 17:21
  • @Guffa This is a silly comment. I am certainly not advocating the sort of design you are implying. I AM saying that your code should be so well thought out and concise, that the need for regions is redundant & even useless. When you have to hide parts of your class to conceptually fit it into your brain, then you are juggling too many concepts. This is the quintessential definition of poor design, lack of cohesion and over-arching responsibility. Clear thoughts and ideas expressed in code do not need to be hidden, nor do the details need to be fully understood when doing maintenance. – Nicholas Jan 01 '11 at 19:49
  • @Nicholas: Sorry, you lost. You don't know how to conduct a civil discussion, so it ends here. – Guffa Jan 01 '11 at 20:50
  • @Guffa There is nothing uncivil about my responses. You are mistaking a disagreement with a personal attack, which is poor form. – Nicholas Jan 01 '11 at 21:42
6

StyleCop doesn't like regions:

SA1124: DoNotUseRegions

Cause

The C# code contains a region.

Rule Description

A violation of this rule occurs whenever a region is placed anywhere within the code. In many editors, including Visual Studio, the region will appear collapsed by default, hiding the code within the region. It is generally a bad practice to hide code, as this can lead to bad decisions as the code is maintained over time.

How to Fix Violations

To fix a violation of this rule, remove the region from the code.

There is some discussion about whether or not this is a reasonable rule.

The consensus seems to be that some people like regions and some people don't - it is up to individual teams to decide. The most important thing is to use a consistent style throughout your project.

One place where regions might be acceptable is to group all the methods that implement a particular interface. It is worth noting that Visual Studio automatically adds a region if you use the code generation feature to provide method stubs for implementing an interface.

The only place they seem to make sense to me, is in automatically generated code, because you should never have to read that outside of personal curiosity.

The partial class feature is better for separating automatically generated code from manually generated code within the same class.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    Consistency is important, but equally important is cohesion, clarity and readability. If your style violates these other virtues, you can't really hide behind consistency. – Nicholas Jan 01 '11 at 14:46
  • I wish more people would vote up this answer – Nicholas Jan 01 '11 at 14:57
4

I think that #region is perfectly fine. I've never used it myself, but if you have a large or complex class, it can help with finding what you're looking for. Imagine if you're implementing ID3D10Device1- that's over a hundred methods to implement. You want to just throw them all in one place?

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • I've never had to implement anything that couldn't be broken up into more discreet chunks of understandable code. In my experience #region has never been a good idea, but I concede that certain types of functionality are impossible to implement in a modular fashion. – Nicholas Jan 01 '11 at 15:09
2

I do use regions to organize bigger constructs in a class, ie. a Dependency Property with callback and methods getting called by this, eg. you have a DP that takes an IEnumerable<T>, and you want to use the weak event pattern to respond to INotifyCollectionChanged. This can take some code, and as I won't be touching it after coding it, I put it in a region.

However, if you resort to regions to structure your logic, this is severe code smell and that's what the StyleCop rule in Mark's post is pointing hat.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • I am still suspicious of this use, but I really appreciate your comment about structuring logic. – Nicholas Jan 01 '11 at 14:58
  • The problem people (and StyleCop) have with regions is quite simple - it can hide design failures. This isn't quite the case with putting a DP into a region. A full DP with accompying methods can take alot of code and considering you won't usually work with this boilerplate code, I don't see a problem with grouping it together with a region. – Femaref Jan 01 '11 at 15:22
1

while I am programming I use regions a lot they help me keeping my code organised and be able to focus only on parts I care. I always add comments on the regions and what are they about .But when I am done I am always removing them. Its a handy tool, that's all.

elasticrash
  • 1,181
  • 1
  • 12
  • 30
  • So you are using them to organize code while you are building it? So I guess you would never commit code to a repository with region tags then? – Nicholas Jan 01 '11 at 14:55
  • yeah I would never commit it with region tags obviously. cause I don't expect others to follow my region logic. But its helpful on a personal level – elasticrash Jan 01 '11 at 15:01
  • OK, well I can't disagree with this. I don't do this personally, because I like to see all of my code open and in front of me. For me, I want to strive to chisel a discreet piece of functionality down to an acceptable level of completion, and I would want to see it's full form at all times. – Nicholas Jan 01 '11 at 15:04
-2

IHMO, if you have #region section in a class, it means that you have two behaviors in your class and so you should split your class in two objects.

Baptiste Pernet
  • 3,318
  • 22
  • 47
  • But what about the case where you are creating regions for private variables, public methods, constructors... etc. etc. – Nicholas Jan 01 '11 at 14:38
  • It's not that simple. Two objects have things like, for e.g., separate lifetimes. And not all multi-behaviour classes can be split up. – Puppy Jan 01 '11 at 14:40
  • 1
    What do you mean that the class has two behaviours? Regions doesn't change how the code works in any way, they are just a means of organising the source code. – Guffa Jan 01 '11 at 14:44