The Problem With Code Folding

I think regions’re fine if they’re used properly.

What’s all the fuss about? If you don’t like it why… then you don’t have to :slight_smile:

Have to agree with people that in certain cases it makes code easier to maintain/read especially when you’re dropping into code you didn’t write yourself to fix bugs/add features.

If there is something that makes code unreadable/unmaintainable then it is partial classes! At least with #region you have all your code in the SAME FILE.

And the foldings are open by default; you have to manually close them to get the code to disappear.

In the base configuration, but you can set your IDE to auto-collapse by code block type (file headers, imports, javadoc comments, method bodies, annotations, getterssetters, inner classes, anonymous classes and xml tags)

If you’re constantly editing one small area of a giant file, why isn’t the part that changes frequently in a different file?

If your changing one aspect all of the time, yeah you should put it in a different file.

But if you’re adding new features, to, lets say, a game. You’ll go into an input object to add something. Or you’ll go into a graphics object to add something.

These are files that you would likely be modifying to add new features that wouldn’t necessarily warrant a completely new class. Having those regions would save some strain on the fingers and the eyeballs.

Use CTRL+I incremental search. You only see what you find as you type.

I admit. I do need to use this more.

Even though I strongly disagree, I feel these are great to organize code…it’s nice to hear a differing view and we should be cautious so we don’t abuse them as in your example above.

I tend to find them nice, to organise like minded methods in the one section, especially with how we’re doing unit tests at the moments against a repository pattern.

I tend to take the three or so tests for each method in that repository, and wrap them in #regions, specifically so the codebase remains small and navigatable without needing to be split into multiple files/classes/partial classes.

Plus, as you said, if it was a big issue there’s Ctrl+M, Ctrl+L and all code blocks are expanded and you get the whole code again.

That said, VS also provides automatic blocks for you to collapse methods etc as need be.

shrugs I just find it nicer to group methods/tests that way and so I use it, YMMV as they say.

Yea, if you don’t use them correctly than they’re horrible.
I find the following use very nice though (as long as it is consistent across the source)

-Static Impl

-Fields

-Ctor

-Properties

-Public API

-Private Impl

It means you can easily burrow down to the kind of information you are looking for. All protected stuff ends up in Private Impl (which isn’t pendantically correct but its purpose is to separate out the public (not subclass) entry points).

Regions are just a hack to try to make up for Visual Studios ongoing complete lack of a decent code visualization tool.

By this I mean a dynamically updated (tree) view of the current source file that allows sorting, filtering and highlighting of code constructs such as namespaces, classes, methods, comments, fields properties etc.

I have tried a VS power toy that makes an attempt at this but it does not go far enough.

Microsoft continue to overlook basic programmer productivity features in favour of cool new headline features such as Linq, XAML designers etc. How about decent multi-monitor support in Visual Studio Microsoft?. It is 2008.

Sharp Develop seems to be improving all the time, maybe it will be the answer?

Oh and:

  • Nested types

(helper objects) at the bottom too. That usually covers everything.

Sverrir:
What’s all the fuss about? If you don’t like it why… then you don’t have to :slight_smile:

I think part of the fuss is inheriting it or working with a group where others do it - it’s not always a case of just not doing it, because someone else in your group will just do it :wink:

dude, whats with the comment about terrorists?

Let me start by acknowledging that you started this discussion by stating this is a preference not a fact and that you are entitled to your opinion.

That said, I believe the region directive is quite useful. All of your points can be easily addressed and some of them actually can be viewed as supporting rather than detracting from its use.

Immediately I have a problem: I can’t see anything!

  • So? When you enter a source file, how often do you really need to see everything, all at once. Can you really take all of that in? Besides, there is nothing to stop you from turning this off in your IDE like you said, but no amount of IDE configuration (currently) will add regions in for those who would like them.

Here’s the really sick part: once you expand the above log4net code there’s literally three pages worth of code there!

  • This is a weak argument against regions and a strong one against cleaning up the Log4Net code. Showing an example of how a particular feature can be abused doesn’t detract from it’s usefulness, it simply demonstrates the need for some sanity and perhaps some Best Practices in its application.

I daresay being able to see the damn code is more important than having it meticulously segmented into six pointless little named buckets, … It’s as if they’ve forgotten what the scroll bar – and incremental search – is for.

  • Again, you can turn it off to have it all there if you wish and you can still use incremental search. Regions are not preventing you from using any of your favorite navigation methods. As you are well aware, there is more than one way to do things, and while you may favor one approach, others will have theirs.

I strongly urge you to think about how you’re using code folding, because as I see it, there are a lot of downsides:

  • Now we are getting into something more useful. Enumerating the ways this feature can be abused and working towards some best practices that the community at large can use to wisely apply it.

It’s a bug. The editor should automatically offer to fold up these common structural blocks for you!

  • I absolutely agree here. The editor should be more intelligent in providing these capabilities for you, negating most arguments in favor of #regions. However, saying that we shouldn’t use the current workaround to provide the organization we want and should channel our energy into demanding a smarter editor is foolhearty. How many bugs, shortcomings, missing features, etc. are out there in the world in every piece of hardware, software, and everything-in-the-middle ware? If we demand perfection before moving forward, we would get no where.

I urge developers to write code that doesn’t need folding to be readable, clear, and concise. I’m sure there are sane uses for code folding out there somewhere, but I rarely see them.

  • Again, I agree. Now that you have stated your views with the team and made it known that you do not negotiate with terrorists =], may I suggest a future article discussing best practices and common pitfalls of the region directive?

Thank you for the post (and the blog!). I believe these controversial ones are some of the most beneficial. What concerns me is when I see people blindly saying oh this is bad? Thanks Jeff! and running off without understanding the issues at hand and putting some thought of their own into it, but I digress…

I didn’t read all those commentsthinsp;mdash;thinsp;but I can tell you that Visual Studio (from 2005 at least) HAS code folding for namespaces/methods/classes etc. Those protesting it’s lack thereof should try actually using the program.

In other news, regions ARE good for hiding large lists of accessors. I hate accessors (properties/getters settersthinsp;mdash;thinsp;whatever you want to call them). Other than I don’t use them a hell of a lot. Or any type of cold folding for that matter.

Years ago I red a good rant about proper coding style that was written by a maintenance programmer. One of his peeves was that we are still programming with pretty much basic text editors, when it would be possible to create something better. One of his examples was that it would be much easier to notice errors in complex equations, if you could see them as they are usually written for humans to read instead of a single line of variables and operators.

I think folding is an attempt to move to that direction. Programmers really should be able to control what is visible/hidden, but instead of hiding parts of a text file, that momentarily unimportant stuff shouldn’t be there in the first place. For instance, instead of text files, IDE’s could show code in a zooming based interface. We mostly (I think) visualize program structures as some kind of paths or groups of interacting objects. Then why the hell do we use tools that assume we write them as page by page novels.

I’m sick of scrolling and jumping from file to file. I’m not saying that programming should be done by clicking pretty buttons or that source code should be rich text. I’m just saying that programming tools could use some usability innovation.

Regions are good for automatically generated code, not for much else.

Jeff Atwood If you’re constantly editing one small area of a giant file, why isn’t the part that changes frequently in a different file?

Placing part of the code into a different file has the same downsides as using ‘#region’. The only advantage one might gain is more granular and easier version control.

I think regions are not about hiding the code, they are about structuring your code. An IDE won’t show you logical parts of your class.

Try jEdit. It’s got some drawbacks but its code folding is far better – it’s on indents, not these awful regions

The editor should automatically offer to fold up these common structural blocks for you! I’m continually amazed that programmers spend time doing this scutwork when they could be writing useful code. Or at least demanding a smarter code editor.

Is there any way to make this work in Visual Studio? Which editor does that exactly, because I’ve never heard of an editor like that.

LOL! Since when do you make such sweeping generalizations Jeff. Regions are good for some cases and bad for others. You take one case of where Regions are bad and apply it to everything. For Shame. Writing immaculate code is like the immaculate birth. IT NEVER HAPPENED!

The trouble of incremental search is that it’s only useful when you know exactly what, and the name of what are you looking for. Very often, I don’t. Usually, I keep a visual idea of what the class looks like and what it does in my head. My mental representation of it isn’t too far from what the Class Diagram in Visual Studio shows. When I want it to do something else, I open the file and browse through it to find what I want. Folding the code down helps tremendously in this.

Of course, you can question this approach to programming altogether. I’m guess I’m just not quite as hardcore as others. :slight_smile:

Another reason for folding is because the summary XML Comments are tremendously verbose. For each one line it takes to declare a field, it takes three lines to describe it, at least in the OOB behavior. You end up with tons of useful clutter on screen which you can’t chalk down to bad code; just to bad visualization.

Yet another example for folding, when you’re trying to adhere to Microsoft’s component model, you can often end up with something like this…

public event EventHandler SomethingChanged;

protected virtual void OnSomethingChanged( EventArgs e )
{
if ( Something != null )
Something( this, e );
}

Repeated 50 times for every OnPropertyChanged that you have on there. Even if you stick it at the end of the file, it’s still mental overhead you can easily get rid of just by folding it down.