views:

288

answers:

11

I really appreciate the possibility to define regions in your code, as it improves the readability insanely.

Anyways, I'd like to have everyone using the same convention in all classes (with the predefined order of all regions) like:

  • Private Fields
  • Constructors
  • Class Properties
  • Event Handlers
  • etc...

Do you have any proposition how this division could look like (What regions have sense and what names should they have) and in which order should they be defined ?

+7  A: 

Someone once said that having a convention like the one above:

  • Private Fields
  • Constructors
  • Class Properties
  • Event Handlers
  • etc...

is like setting a table where all the plates are together, all the spoons are together, all the knives are together and all the forks are together.

My take on the #region issue is to put related methods, event definitions, and properties together in one region. However, having to do this at all would indicate a code smell (either your class is too big or does too many things) but this is a good first step into refactoring it into a better class.

Jon Limjap
Which would make a lot of sense if you were going to eat the code.
Daniel Earwicker
Yes, I hope the code is delicious.
Jon Limjap
However... despite my suspicion of metaphors in this kind of debate, I have to say that whoever you were quoting, they had a good point!
Daniel Earwicker
+22  A: 

My convention is not to use them.

If you find your class getting too big such that you need to hide vast parts of it through regions I propose your class is too complex and should be broken apart.

Michael Shimmins
mxmissile
OK but let's presume that we have a simple WinForm which has about 20 EventHandlers in its code behind. Is really hiding them in this case pointless?
PaN1C_Showt1Me
@PaN1C_Showt1Me Well, that's what partial classes are for.
Jon Limjap
@PaN1C_Showt1Me - yes. You shouldn't have much else in your WinForm's code behind except event handlers and method calls within them to the actual workers. That shouldn't be too big.
Michael Shimmins
+1  A: 

Thats controversial , some people say that regions are a bad style...

chriszero
+1  A: 

You might be interested in this do you say no to c# regions.

I think that so long as you're consistent accross your project it doesn't matter too much which order you write them in. Also be very very wary of overusing them (hence the initial link!).

There's nothing worse than finding a closed constructor region which is hiding only one line of code.

I think in the end it's down to personal taste. As I've said, consistency is the key!

Zeus
Well I'd like to react on the post.. they say the good-designed code does not need regions. Anyways.. if you have 5 overloaded constructors - wouldn't it be easier for anyone opening that file.cs to see just 1 line 'Constructors' in place of 5 real constructors?
PaN1C_Showt1Me
I think the case you mention is certainly a good place to justify their use. I'm just wary of them as I've seen this carried from one place where they're useful into other places where they obscure code.
Zeus
+1  A: 

Think of them as another form of comments: additional information mixed in with your code, which has no formal checking performed on it. Hence it will likely drift out of date with the code.

So NEVER duplicate in comments or region directives that which is already stated in the code.

Only add extra information.

In particular, using regions to restate the fact that certain members are properties, events, etc. is completely pointless. The most common problem there is that you create a region for "private methods", and then you edit one of them to make it public. Now you have to move it, which means that in a diff with the old version, the simple change is much harder to discern.

Daniel Earwicker
A: 
#region Lotsa boring code and lookup tables

I use it to save screen real estate, nothing else :)

leppie
+4  A: 

Whenever I see regions I think that the code is either generated or in need of re-factoring.

Avoid using them and when you feel the need for them, re-examine what you're doing and try to split your class in to smaller ones. Ultimately this will help with the readability of the application more than using regions will.

Keith Bloom
A: 

I wrote my own region code snippet for VS 2008 which I always use:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>#class region</Title>
        <Shortcut>#classregion</Shortcut>
        <Description>Code snippet for #region in classes</Description>
        <Author>Simon Linder</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>name</ID>
                <ToolTip>Region name</ToolTip>
                <Default>MyRegion</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp">
            <![CDATA[#region Variables
                    $selected$ $end$
                #endregion

            #region Construction/Destruction
                    $selected$ $end$
                #endregion

            #region Properties
                    $selected$ $end$
                #endregion

            #region Public Methods 
                    $selected$ $end$
                #endregion

            #region Private/Proteced Methods
                    $selected$ $end$
                #endregion]]>
        </Code>
    </Snippet>
</CodeSnippet>

As you can see I do use regions for Variables, Construction/Destruction, Properties, Public and Private methods. I often add another sub-region into the private region called events. The order of regions also works fine with StyleCop.

Simon Linder
And next to the line `n++;` do you put `// increment n`
Daniel Earwicker
Sure... Always!
Simon Linder
+3  A: 

Personally I wouldn't recommend making code regions part of your code convention. The main reason being that regions hide code, which could potentially lead to issues like:

  • Developers might miss some important part of the source code
  • The average amount of LOC in the same file tends to increase

If you are interested in enforcing a coding style convention in your team, have a look at Microsoft StyleCop. Note that the tool currently only works for C#.

Enrico Campidoglio
A: 

I use the following regions:

Private Member Variables
Constructor
Public Properties
Private Methods
Public Methods
Events

The reason is because of better organization of code.
I work with files that may have over 2000 lines of code and it is very difficult to maintain the code without regions.

šljaker
"I work with files that may have over 2000 lines of code and it is very difficult to maintain the code without regions." Here's a suggestion. Don't work with files that have over 2000 lines of code.
Daniel Earwicker
May I know why I got downvote :)
šljaker
@Danel Earwicker, I work in a team that writes the information system for insurance companies. The project is very complicated and complex. I can not say "I will not work on this project until the files are not to be under 500 lines of code". :) There is a project manager who decides on everything, not me.I just gave an example.
šljaker
@šljaker - If you have a project manager who makes all your decisions for you, I guess my comment is for them to read.
Daniel Earwicker
+2  A: 

I think there is no need in regions. Them are not legible. If you need (think, do you realy need?) an amount code in your class, you can use 'partial' class to split the class logic units.

Stremlenye