a companion discussion area for blog.codinghorror.com

The Problem With Code Folding


Jeff I love your stuff, but of all the lines to draw in the sand on coding practices, in my book this ain’t one of them. The mere presence of a region reflects at the very least an attempt at code organization, and that is not a bad thing.


Whoa, good to hear that someone as well respected as Jeff also can get all the same crap about regions I received on my blog post: http://blog.codedemora.com/2008/02/29/just-say-no-to-c-regions.aspx


And I don’t have to use regexp searches to find things. I just open the class ParseLogicCC33, because I know they the logic is there.

How everyone could know that the logic is in ParseLogicCC33 instead of being in a region? Well, you have a design of the solution. It should have a class diagram. This is the right way of doing things anyway. You design first, then implement. Everyone who has to touch the code should first look at the design, because otherwise its more like guessing or figuring out on your own or with your coworker.

So you have the design and a class diagram and its easy to take a glimpse of that and see where all the logic is. After that no more regexp searches.

Surely hacks work if done right, but I don’t like the idea that most stuff is made by weird hacks that are called solutions that everyone understands when there is even no design documents. One thing leads to another and soon the whole system is full of undocumented hacks.

If regions are used, there should at least be programming conventions for them.


Overused #regions suck. That’s why some people (self included) hate them.

They’re fine when they hide visual noise. They suck when they hide information.


Every time I read a post on this blog and the comments associated with it, I am reminded again how lucky I am to not have to code with Windows anymore. 3 years ago, I switched off Windows + Windows programming after ~12 years doing it for money. Since then, every time I’m exposed to Windows coding, I’m sort of shocked like wow, they keep falling farther and farther behind.

I don’t mean this as a holier-than-thou comment at all. But honestly, I feel very fortunate that I don’t have to deal with any of this stuff. MSFT elected to create a keyword for code folding? Really, when free IDEs do it automatically?

And the platform + tools are still encouraging people to minimize the number of source files they have, like it’s 1990? It makes sense that if Windows can’t efficiently handle lots of files, then you want all your code in a single file. But wow, I had no idea it was still this bad.

I know everyone can’t switch off Windows and still keep the bills paid, but I’m glad I have been able to. Good luck to you guys.


A provocative idea, and after thinking about it, I partially agree. If regions are used to mask dirty code you are ashamed of, then you shouldn’t use regions. You should refactor.
On the other hand, I find regions useful in illustrating what the class is about. I tend to use lots of private helper methods: these are prime candidates to go to a region. Don’t expand the region, and you have the high-level view of what the class is about, without clutter. Expand the region, and you can go into the nitty-gritty of the implementation.
That being said, partial classes are an interesting alternative to regions. If you want to isolate a specific group of behaviors, put it in its own partial class.


I disagree with you on this one. Kudos to you on making your point though. I certainly don’t have to agree for the point to be valid. Anyhow, like anything else regions can be used poorly and that is a shame.



Rather than sounding holier-than-thou as you put it, how about trying to sound intelligent or at the very least informed before commenting? It’s been stated multiple times already that code folding is integrated into the IDE and that regions are optional.


My take on the subject.



Weak post. Regions are fine. Anything used excessively is a pain…


I concur wholeheartedly.

#Regions are just one more M$ attempt at dumbing down the software community. Okay maybe that’s a bit paranoid of me and someone actually thought they would be beneficial, but still. My IDE should help me, like say, trusty ole R2-D2. It should not baby me like Dr. Spock.


Regions are a lazy trick to allow some organization. IMHO the future is in storing everything in binary intermediate language that doesn’t lose anything but always runs rather than is such a fragile thing as a text file. eg. Think Spore procedural creatures stored in .png where the image is the sum of the parts in the creature. You see IDA Pro disassembler and latest movie production software doing something along there lines.

The IDE, language and the result to need become much, much closer together so when you are programming you are editing and seeing the end result - immediately! Think of creating levels in a game editor built into the game engine or painter painting. The moment you write the code and hit, if it’s ok, it takes effect and is immediately in the latest version of the binary. Version control, testing, specification etc is built into the system of course to kill problems errors before they get anywhere.


Code Folding: The first IDE feature necessitated by a bad object declaration design.


C# Regions are a CodeSmell

There is a second use of Regions - hiding portions of a method. To any agile developer, this would immediately tell you that the method is too long. But I see the practice time after time. So much that I wrote about Regions as a CodeSmell.

a href=http://aspadvice.com/blogs/rbirkby/archive/2008/05/12/Code-Smells-in-C_2300_.aspxhttp://aspadvice.com/blogs/rbirkby/archive/2008/05/12/Code-Smells-in-C_2300_.aspx/a


I like regions too, but at the same time I don’t like them. Maybe its like using goto. Its handy, but not recommendable. So people use regions until it is taken away from IDE:s. Period. But is regions really as bad as goto? Well, it depends. If you are not capable of living without regions, you will not see why you shouldn’t be using them and you think everything is just fine.

So I said that I also like regions. Would I use them? In some projects many classes are so big that I would use regions for the sake of clarity, unless I would have time to refactor the classes. Also I might first add regions, because that way I would see what parts need further refactoring and redesign.

But here I see again that thinking programming hits and designing is left back somewhere. So there needs to be more effort put into actually designing and modeling the program so less regions will be needed.


Regions are great. Sometimes when you’re working with awful APIs you can’t avoid excessively long code where often there’s no need to overfragment the code into unneeded functions and files.

I agree with some of your points, but i have the same opinion of comments. I hate how unreadable comments make the code.


I use them for code that shouldn’t be run, due to a change in requirements but may have to run again soon due to the requirements reverting back. I wish people could make their minds up :stuck_out_tongue:


You should write a post about the evils of classes. Who wants their code hidden in a separate class? Someone with something to hide is who. If you are a truly excellent coder you’ll throw information hiding to the wind and just put everything out there in one place. How am I supposed to see it if someone organizes it into manageable sections?


I have two words for you: Baby and Bathwater.

Among other things, you seem to imply that once code is placed inside a region it is no longer accessible! No, you can’t SEE it, but even if it were expanded, you probably wouldn’t bother LOOKING at it unless you thought that’s where a problem was. And the region will automatically expand while in debug mode, so it isn’t like the environment is HIDING anything from you. So why not get it out of the way when you KNOW you don’t need it?

When you clean your house and put things into your closet, you can organize things neatly or shove the things in there with no thought. But should closets be done away with just because some people use them to hide junk? Should organized people have to forgo their use so that messier people will be required to leave their slop sitting out for the world to see?

Anything has the potential to be abused. Regions are no exception, but I code more quickly by using them without any of the detriment you decry…and I’m sure I am far from the only one.



I agree and disagree…