The Problem With Code Folding

Wow!
You sure know how to make a big deal out of anything.

Personnaly I don't user coding folder because they are no use to me, but when working in a team each member must make small consecession on coding style in order to work.

I work once with a team that as for stantard to use code folder to group class members by type. Their claims was that it help organize the code base. I have enought dicipline to organize my code with out it, but I understand that other on the team might need a little push. So in modify the item and project template under the IDE folder the that the #region  where  added automaticaly whenever I create a file.

I totally agree with the fact that we should ask for better tools, but I think that we must first learn how to make the most of what we have until they comply. For those of you who don't already know it already VS have 3 navigations tools that allow you to group by member type, member access and alphabetical. The object viewer, the class diagram and the class view. My favorite is the class view because you can easily dock it on the right with the solution explorer, that way I can see the code can use the class view to navigate.

Their was an argument about sweep code under the rug. That might be true, but if experiences thought me anything is that even if the bad code is visible for everyone to see it doesn't make much a difference. Let face it when you are in some else code you are must likely to doing some maintenance work and all you want is get in, do your thing and get the hell out. Their is always the code review / refactoring and if you lucky the managements actually give time for that but with time to market kind of pressures that is unlikely.

Anyway it have been fun to read you Jeff.

Some comments above about how other IDEs already have automatically-generated views which group and sort in various ways.

Well, now Visual Studio 2008 has the same thing:

http://www.codeplex.com/ora

(Free, open-source add-in)

This may be useful to some people:

#region .+\n:Wh+#endregion

Should find empty regions in your code. What you do with them is your choice, but when VS is adding them and they are empty it seems to me that thereā€™s a lot of things you could replace them with that would be more useful. Empty space, for example.

I use grouping to ā€¦ group code that work. I donā€™t use i a lot but some of the code are familiar stuff and can be tugged away when it works i.e. the code I have for docking a form. In my tiny mainform there are 8 methods that handle the task of docking forms. It works and need no further attention so it is grouped. It furthermore tells my colleagues what the grouped code does. In that respect I find grouping a good thing.

what a load of crap. Cold folding is very useful for keeping code you donā€™t need at that time hidden, and organising related code into sections.

cold* xD

code* I mean ā€¦ embarrassed now

I use VIM. At this point REGION CODE FOLDING ROX - since some languages does not allow splitting code into parts (files or anything like it). What is the best is that it is configurable how it works.

I have to maintain unreadable some code made by other people. The code (C#) has regions. It has other cool features like huge (3000 lines) files, meaningless names (variables tmp1,temp2, methods doStuff,doOtherStuff), nested IFā€™s (I need to use horizontal scroll-bar frequently), hard-coding, empty catch blocks, and many more. REGION CODE FOLDING SUX.

I couldnā€™t have put it better myself Jeff. Everyone who disagrees with what you said is WRONG! There are so many unintelligent povā€™s in the comment here it makes me sick! These people might as well just print up sheets of there code and piss all over them before handing it off to their coworkers.

I completely agree with you post. Regions are ugly/obfuscating constructs which should be considered a code smell. Interestingly, its the senior devs (not by age, but by competence) in our team who hate them and the juniors who love themā€¦

I was googling implement code folding and run into this post. Cā€™mon guys, code folding is useful. what if your file is 200pg long?! I want to customize code fording for my latex writingā€”that is, non-programmatic writing. thatā€™s how useful it is to me.

One more vote against regions.

In the real world, most source files are more like 200 lines than 200 pages. When I open a source file, I usually am interested in three things:

  1. The classā€™s public interface.
  2. The class private members (to see what state it manages)
  3. The actual code.

I used to be able to see this at a glance. In my former company, I tried to explain to my colleages that the regions were in the way. One guy responded: Oh, so your problem is that the editor hides the regions. I hope for you Visual Studio gets better at regions in the future. But as Jeff pointed out: The only reason to put them in is for the editor. Go figure.

Have you ever seen a surgeon work? When a surgeon works they cover up a majority of the area and leave what they have just been working on.

To me regions are the same thing. I want to focus on the method or methods I am working on, I donā€™t need to see the properties or the constructors or the event handlers.

Itā€™s true: if code requires code folding, that code is evil.

Even though I fully agree with Jeff I realize weā€™re not writing all the code weā€™re navigating through. For example, take all those open-source projects an a significant subset of them that is not exactly a walk in a part when in comes to walking through the code.

With that in mind, when navigating other peopleā€™s code (e.g. open-source projects) sometimes itā€™s good to be able to fold control structures as well. However, Eclipse (3.2-3.5) does not support it. Iā€™ve found a plugin for the job:
http://blog.shonzilla.com/post/121086982/extended-code-folding-in-eclipse

Cheers!
Shonzilla

Those region discussions are really amusing. Itā€™s as if there is a law to use or not use them. I myself am using regions in all classes. Even if they are only a couple of lines in them. I group them into a declaration part, constructors, properties, methods, event handling, overridden methods and interface implementations. and maybe some other things if neccessary. And I use sub-regions inside the regions to group things that logically belong together. I think my code is very well structured, and if I find my class does get too long and should be refactored, the regions usually help me because the parts that can be separated usually are in the same region. I do not see an advantage of seeing all the code when I open a file except for that it looks more complex and ā€œimpressiveā€. If thatā€™s what you need, you really have a problem. If you are too lazy to click once to open a regionsā€¦then actually I donā€™t understand why you are not too lazy to use the scroll wheel maybe even a couple of times. (btw. if youā€™re using vs then you should read the manual because you can switch on/off region folding when opening a file in the preferences). I have seen a lot of smelly code by people who tried to avoid regionsā€¦well around 50% of code was smelly with or without them, but people claimed it was all fine, in some cases people used hundreds of partial classes which did not make the code any better than with regions, but made it harder to see the code of a single class because you had to browse several files to do so, and in some other cases I got crazy because people insisted on making hundreds of classes to make their code shorter, and seperated things that actually belonged together, with the result that you had to call a.b.c.d.e.f.g to get the value of a property Not to mention how horrible it is to browse to the class that finally contains the property you want (go to definitionā€¦go to definitionā€¦go to definitionā€¦sic!).
Oh, and a big bunch of people are simply too lazy to structure their code and use the smelly code argument as an excuse.
Rubbish, because itā€™s all in your hand if you are ill-using regions to hide bad code. Itā€™s like not using cupboards to put your clothes in because some peopleā€™s cupboards are messy.
But regions help you structuring you code, and group properties and constructures and methods and all together, and easily jump to that region to add a new one. Sure you can do that without regions tooā€¦you can also write your code with notepad if you want.
Tools like StyleCop or such that group your code? Hmmm but if you need them then isnā€™t your code smelly too? Because you claim you do need to hide anything. I do not need any tools to find my way around my code because regions do it all for meā€¦

If you do not want to change the region code on your Mac according to different region DVD, the best choice would be having an external driver. So your copy software must support external driver.
As I know, Mac DVD Copy Pro support external driver.
http://www.imediacreator.com/mac-dvd-copy-pro.html#131

I too disagree. Like most newer language features, if used properly they are indispensable for code organization. However, when used for the reasons you mention, they are certainly dangerous. Perhaps you just need to work with better codersā€¦ :slight_smile:

For example, who needs to see all of the repetitive property accessors all the time. Just roll them up in a region and worry about looking at the non-trivial code.

Gotta disagree with you here. Code folding was (and still can be) a good thing.

Back In The Day, when partial classes werenā€™t allowed and C# didnā€™t have syntactical sugar for declaring get-only properties (oh, waitā€¦) classes wound up with buttloads of code in them that was really just clutter and hid most of what was going on. #region was a good way of keeping all of that stuff out of my way.

Itā€™s not there for the compiler, itā€™s there for ME.

I agree that #regions are a bad thing when overused, and especially hard to navigate when there are nested #regions. But for very simple code organization, I like them very much. My typical #regions are fairly high-level, such as for properties and event handlers.

@Adam, I donā€™t know about peacefully. Itā€™s been something of a jihad in our office to get people to quit hiding their code. Our team makes everything public too which killed our credibility with the smarter guys. [lol] We just value Rhino Mocks and testing more!