The Problem With Code Folding

Personally, I like regions… I agree that there are occasions when knowing which region to open when looking for a method is a pain. However, there are:

  1. SEARCHES
    a. Ctrl+Shift+F - Find across project / solution files
    b. Ctrl+I- Incremental
    c. Ctrl+F - find within file
  2. The method-name / property drop-down list at the top of each code tab
  3. GOTO DEFINITION
  4. FIND ALL REFERENCES

In fact, the number of times I find myself looking in a file just to browse it is so minimal it can be discounted. I am just about always looking for soem reference that one of the searches above has found.

I also consider regions (when used ‘how I like to use them’ big grin to have a useful side-effect - it makes me think about the intended audience of the code, and how it will be used. If it’s public, I need to consider the caller and interface considerations etc - perhaps CLR interop is a consideration etc., but if its private perhaps I can pass around different types, etc. The region can be a marker to remind us to think about these things.

However, having said all that, where I currently work regions were historically used as an ALTERNATIVE to methods / functions even (you know, the two-thousand line method with twenty regions in) - which I can assure you I do consider to be total and utter misuse of regions :frowning:

Nij

I have to agree and disagree. I personally love to use regions to organize my code according to implementation until I started to use the custom tool used for generating a plugin template for our project which totally abused the use of regions. A region was created for each object declaration (method, fields, properties, namespace, etc…) in the IDE. That is totally sick… i’d have to unfold multiple times for a ‘public bool foo = false’??

Anyway… same with most here, regions, if used properly is undeniably good.

Eric Gunnerson, one of the MSFT engineers who was involved with C# development for a long time, agrees:

http://blogs.msdn.com/ericgu/archive/2008/07/10/region-sliced-bread-or-sliced-worms.aspx

Len Holgate calls it #herebedragons

http://www.lenholgate.com/archives/000400.html

I personally like regions for breaking code files into groups of members by construct type (like chapters in a book) and went so far as to create a tool to automate doing so. NArrange will group members into regions and can also apply sorting by name, accessibility, etc.

http://narrange.sourceforge.net

However, I realize there are many downsides to regions and that not all teams condone their usage. Thus, the most recent version of the tool allows you to apply grouping and sorting of members without region directives. See Daniel Root’s post for more info:

http://blog.lifecycle-solutions.com/NArrange++Take+2.aspx

Also, for those just looking to strip regions out of code, this may be helpful:

http://www.codeproject.com/KB/codegen/narrange.aspx?msg=2579903#xx2579903xx

Jeff, I’m totally with you.
Reading through all the comments I can’t believe how many developers think that #region is OK. It’s (almost) always a substitute for good factoring, and definitely a code smell.
This was my post on the subject a few weeks ago:
http://stuartharris4.blogspot.com/2008/06/c-regions-inline-comments-and-blank.html

folding in general looks like a weak version of literate programming. Which, given your opinion of folding, you’d really hate.

Disagree.

Visual organization goes a long way in my book, and code folding is another tool for doing so. The folded sections give me an overview of the groups of methods (public, private, events, interface impls, etc.) in the module. When I open a code module, I first hit Ctrl-M-O to collapse all regions, scroll to the region I’m interested in, then Ctrl-M-M to expand that region. I can then browse all methods in that region by signature. Additionally, I organize my methods alphabetically by region, so finding a method is even faster.

I’m definitely a fan of code folding and relish the relief from page after page after page of code when I have an idea of what I’m looking for. If I know exactly what I’m looking for, I use the search tool, but otherwise I’m a browser-navigator when it comes to code.

The #region directive is useful when you write big classes: controls or web service classes. In case of these, splitting them in smaller ones would lenghten and/or complicate your code. In classes which are shorter than 2 pages the use of the #region directive is silly.

I have to disagree.

When inheriting legacy code in +1000 lines, and no logic ordering. That’s a headache.

I must agree though, VS 2003 does not expand regions properly and can be a pain at times.

That said, I would rather see the outlining logic before jumping straight into any programming.

a good developer can adapt to any working environment:
http://www.dotmad.net/2008/07/good-team-developers-are-cameleons.html

100 years ago, some physicians said it was impossible for an object more weighted than air to fly. They were SURE it was impossible. Today, we just smile…

When can we you be sure about the right direction of progress? I mean: Regions really can’t fly? Are you absolutely sure? Block diagrams also hide information; however, they can fly. Are regions like elephants trying to reach the sky by waving thear ears?

Whatever will be, will be.

Thanks for shaking our neurons, Jeff

I use them, I like them.

No, I don’t use it to hide anything, just to give myself a cleaner working surface to work on. I’ll typically place public methods in one, private in others, if applicable event handlers in a different one and so on and so forth (precisely as in the sample code shown).

If I am working on redefining a class’ public interface, I may not want to have all the private methods get in the way when I scroll through.

You want to use incremental search… sure… but what if I don’t know what the heck I am looking for?

I think the point you are making has some validity to it as it relates to the tangential points you make (bloat, maybe separation of concerns somewhere in there, etc), but come on… but you have to admit it is highly religious, as in you were high on a horse and had some adrenaline flowing when you wrote this…

Not all opinions are truths, nor all of those absolute. regions have their place, here and there and some people certainly abuse and misuse them, but as much as any other tool they all step in to aid the smith feel more comfortable in the exercise of his trade.

I like them, I don’t love them or swear by them, but I certainly don’t mark them with the scarlet letter like you have done here.

Hi,

After a zillion comments, no-one is probably going to read this one. But I happen to agree with Jeff here. I hate the regions because I can’t see the code and so can’t as easily browse it and get a feeling for it.
(And for seeing an higher level view of the code (which is what most people seem to do with folding), I mostly use a plugin called ModelMaker code explorer, it shows the structure of the class in a separate tree next to my code.)

Wow.

It seems to me that since you ventured out on your new project and the cash might not be flowing as easily as it was before you’ve just been picking the most religious subjects and getting on your soap box about them. If you need the page rank up so that you can get more click throughs that badly Jeff I’m sure someone can sub you $10 til pay day.

It’s starting to border on drivel, it really is.

One more helpful hint, specifically for those who are forced to work on a team enamored of regions (this is cobbled together from a couple of sources online):

Sub CollapseToDefinitionsButExpandAllRegions()
    DTE.ExecuteCommand(Edit.CollapsetoDefinitions)
    DTE.SuppressUI = True
    Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
    objSelection.StartOfDocument()
    Do While objSelection.FindText(#region, vsFindOptions.vsFindOptionsMatchInHiddenText)
    Loop
    objSelection.StartOfDocument()
    DTE.SuppressUI = False
End Sub

Instead of setting Alt-Left to CollapseToDefinitions, you can set Alt-Left to this macro, which will CollapseToDefinitions, then expand all the regions. You get your nicely collapsed code…but now you can see it. The annoying regions will still be there, but at least now they won’t get in your way as much when you’re trying to work.

Enjoy!

I have to agree for the most part with what you’re saying about Code Folding, I hate it and it seems a deceptive practice.

However recently I have been folding one specific section of my programs, the import sections. I normally have roughly 20-30 lines or more of simple import commands, which my IDE (Netbeans) handles automatically

Jeff! Yes, finally, someone with common sense. I AGREE WITH YOU.

Regions are to be banned. Even good coders get tempted, while bad coders, of course, litter their code with it.

I actually propose to #region out the coders who use #region. Inside a nice (dark of course) cube section. The problem would be that with all the regions they would use, they wouldn’t see their desk, their chair and their lamp… Who cares, they can’t see their code.

Max

I have to say I agree with Jeff on this one. When I first discovered #region, I thought ‘oooh, cool feature’, but I soon discovered all the deficiencies he talks about. So I moved on to more productive activities.

But I did find one use for them. Delving into the intricacies of - wait for it - Windows .INF files, specifically TEXTMODE.INF, which you will find on your Windows setup CD, and which is thousands of lines of impenetrable crap. But (and here is the saving grace) it is broken up, somewhat at any rate, into sections, so what I did was pre-process the file with an AWK script and insert #region directives around each section. I could then use the Visual Studio editor as a kind of outlining tool. It helped me a lot, and maybe this post will help someone else out there one day.

Paul Sanders
www.alpinesoft.co.uk

I have to say I agree with Jeff on this one. When I first discovered #region, I thought ‘oooh, cool feature’, but I soon discovered all the deficiencies he talks about. So I moved on to more productive activities.

But I did find one use for them. Delving into the intricacies of - wait for it - Windows .INF files, specifically TEXTMODE.INF, which you will find on your Windows setup CD, and which is thousands of lines of impenetrable crap. But (and here is the saving grace) it is broken up, somewhat at any rate, into sections, so what I did was pre-process the file with an AWK script and insert #region directives around each section. I could then use the Visual Studio editor as a kind of outlining tool. It helped me a lot, and maybe this post will help someone else out there one day.

Paul Sanders
http://www.alpinesoft.co.uk

Which do you prefer?

public void Init()
{
initA();

initZ();
}

public void Init()
{
#region initA
#endregion

#region initZ
#endregion
}