I sometimes feel it useful to fold code at the entity level (class, method) but I really hate the profusion of #region in the c# code
For code browsing it is much easier to use a free-spinning-wheel mouse (like the Logitech VX) to cruise through the code and stop with a fingertip touch when needed.
Iām sure there are sane uses for code folding out there somewhere, but I rarely see them
I agree, iāve rarely seen regions used helpfully, visual studio will let you fold up methods quite happily, in most cases if your class has too many methods to fit, one per line, on the IDE then you have done something very very wrong.
I disagree with this article. Regioning is just another way of organising your code and of limiting the amount of noise your brain has to deal with when looking at a file (noise being defined as anything you arenāt immediately interested in).
When you open a large source file and see slews of XML comments and code everywhere, your brain expends unconscious energy deciding that itās all irrelevant. When you open up to some nicely collapsed regions, that energy is saved and you can quickly find the relevant section of code. Repeat this process 500 times in a day and the needless energy expenditure quickly piles up.
Of course, benefiting from regions depends on their sensible use. Having regions like public methods, static methods, private methods is about as useful as a barrel full of pig shit. Like any other tool it needs to be used well to be beneficial.
Regioning makes files easier to navigate and information easier to partition into bite-sized chunks that your brain can more easily process without getting tired from sorting through a sea of green, black and blue text.
I love #regions, but - just like yourself - think that coders use them to group constructors, methods, properties, etc. should be ritually and publicly shot in the town square immediately. ARGH!
I try to use them as sparingly as possible, but when I do, I love them. One example: real world class that has some feature, implemented with a public method/property and a number of private member variables and/or methods/properties. I then functionally group that functionality into one #region. When the region gets too big, itās a prime suspect for refactoring in a seperate class. Then they really add a lot.
But I agree that putting all your code in useless and often multiple #region subdivisions is about as evil as evil code gets! Too bad of an otherwise potentially great featureā¦
I hate it when you implement the interface in Visual studio and it auto-adds regions around the generated code. And when you add the interface and click it again, you get yet another region. Bad.
I usually agree with Jeff. I also like that he encourages and expects disagreement.
That said, I think this entry is somewhat shrill. I find #regions helpful in organizing code and allowing me to focus on particular sections. Any tool can be used irresponsibly, so I donāt think #regions are necessarily appropriate, but I do like the idea that I can group functions together, that perhaps I donāt wish to factor into a new class.
If youāre using Intellisense, it un-collapses code. Unless Iām in the process of constructing the class, I very rarely go into a file without getting there from Intellisense.
I used to not be a big fan of the regions; but used properly they can save you some time. If nothing else; if you need to scroll back and forth between some sections of code, by hiding code that you donāt need to see it can make it a little easier.
I do agree with you that VS should have the ability to sort/group code based on protection type; similar to how it can sort using statements. On a similar note; I hate that VS canāt create properties if you select more that one private memberā¦ why can it only do one at a time?
I actually like code folding, it keeps un-necessary code out of sight and allows you to focus solely on the section you are interested in. folding on code blocks makes sense but some times say you have a class extending more than one interface (and this happens a lot) I find it really clever to be able to fold up implementations for distinct interfaces in distinct segments. If #regions are a waste of space, i think comments are an even greater waste. and to extend that logic, any coder worth his/her salt should be able to understand code and not need a comment. But of course some like me need it any way.
Sorry - Code Folding is awesome. I wrote a patch for it in Vim.
You have to remember that the mental state of the programmer is the most important thing in programming, and being able to see everything you need on screen at once, and being able to fold away the unimportant parts, is critical.
Maybe how VS does it is crap, I donāt know. But in Vim, my patch would allow you to search for a term (say, foobar-wizwoz) and then fold away all the intervening lines, with a certain number of lines of context around them. This allowed amazingly fast code refactoring, and for complex refactoring operations it allowed you to see all of your other changes all in one glance, which made it both faster and less error-prone.
I think we all have seen the overuse of regions in codebases we maintain. I am in complete agreement that the automatic use of regions in every file is a code smell and should be avoided. Regions should be used just like comments, only when they make the code clearer or easier to read/understand.
For people looking to make their code more readable with regions I suggest reading Robert C. Martin new book Clean Code: A Handbook of Agile Software Craftsmanship. It is in rough cuts on OāReilly Safari.
Errrā¦ I use regions in the VB.NET Express IDE and it does NOT automatically fold them closed every time I open up a file.
Is this perhaps only behavior found in Visual Studio Professional? Or is my computer just charmed to do things the right way?
Oh, and my $0.02 - I use regions, but sparingly, in classes. Itās nice to be able to unfold just the particular section Iām after, and to fold related functions/declarations/etc. out of the way when Iām not using them. Especially when things get really long. The way the IDE shows folded code is visually distinct enough that I wonāt forget to unfold something. So, I find it helpful and useful, though I donāt use it a lot.
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.
Microsoft doesnāt negotiate with programmers! Buy VS 2008ā¦suckers.
I think regions are OK for hiding and grouping code, so long as theyāre not abused to make nonsense groups. If your regions have names like private fields, constructors and public properties, please donāt do that. Such groupings are not logical, and actually preclude any kind of logical grouping. It increases maintenance effort since people now have to add related things in different areas of the file; either that or they donāt bother, making even more of a mess.
You have just forgotten one point. The standard search in Visual Studio does not search in hidden parts, like regions. (o.k. you can set a flag to search in hidden code, but that is not the standard behaviour, and i was very confused when a new programmer had changed my code and yust put regions in it after we worked 2 years completly without regions. I searched for something in my code and didnāt find it in first place. Another 5 Minutes wasted when lookin why i canāt find my code in my class. First looked in our source control system if anyone has changed namesā¦ and only seeing added region en masse.)
Iām going to have to disagree with you on this one Jeff.
In my experience itās been a very useful tool in the VS IDE. Iām not sure if any other editors support it, but it seems like youāre railing against a feature of single (albeit major) IDE.
I think programming should be about elegant, efficient coding, using the syntactical structure of the chosen language. The #region tag is a tool useful for visual studio, but it means nothing to notepad. It does however still provide a visual cue as to what a certain block of code might do. Can it be abused? Sure, but with some discipline I believe it can be used to great effect.
Donāt lose sight of what youāre trying to accomplish and the code youāre trying to write by getting caught up in the features of an IDE that as a whole can quickly become a crutch.