views:

662

answers:

4

I have lengthy functions in VB.NET (VS2008) and I'd like to shrink them down. I know about the #region directives, but they don't work inside functions.

I was wondering if anyone knew of any plugins for visual studio 2008 that would allow me to fold if statements, loops and try catch statements. I've found a couple of plugins for Visual Studio 2005 but none for Visual Studio 2008.

+11  A: 

You may want to read Martin Fowler's book called Refactoring: Improving the Design of Existing Code, as well as Code Complete: 2nd Edition.

refactoring bookcode complete book

If you're having functions and subroutines that long, it means there are larger complexity forces at work that need refactoring.

Update:

I just finished the book Clean Code by "Uncle" Bob Martin, and I have to say that it belongs right next to the other two in the 'Must Read' category.

Clean Code

George Stocker
agreed. there's definitely a code smell here..
jinsungy
I think that the unwritten rule is try to keep functions to approximately one screen of text, anything beyond that and portions could be factored out to improve readbility
benPearce
+3  A: 

"i'm writing some lengthy functions in VB.net 2008" == Code Smell

Don't treat the symptom, fix the cause and componentize your methods.

Joel Martinez
"Don't treat the symptom, fix the cause" - that's what I like to hear!
Mitch Wheat
much as i'd love to i've got a problem to fix and i need to do it quickly so rewriting the code isn't really an option
zeocrash
AKA "Now you have two problems."http://regex.info/blog/2006-09-15/247
Carl Manaster
+2  A: 

For many, regions are a subjective thing.

I actively dislike anything that hides meaning. If you find you are "writing lengthy functions" try to refactor functionality into separate objects and methods. In the long run, it makes code easier to understand and to maintain.

Hiding code in regions is a bad idea, IMO.

Mitch Wheat
I couldn't agree more. I have a macro (ctrl+m+m) that expands all regions in a file.
Binary Worrier
I have to disagree, strenuously. I have a lot of code that can be easily broken up into chunks by what it does, so I #region/#endregion them and then collapse them when I'm not working with those chunks. For the sake of an example, a main GUI can be broken up into a chunk that deals with the menu and toolbar, a chunk that deals with managing objects related to the GUI, a chunk that deals with start up of the Form, etc. When I'm working on the toolbars, I usually don't care to see or work with the start up code, so I collapse it.
jasonh
Isn't that what 'subroutines' were invented for??
Mitch Wheat
+1  A: 

Ok so i finally found a tool to fold code inside subs and functions. it's not ideal as you have to specify the regions to be folded but it joes the job. the piece of software is called smartoutline. once you get past the broken installer.

http://submain.com/products/smartoutline.aspx

the installer has some issues with admin privileges, so to install it you need to create a shortcut to windows installer specifying the msi as a parameter and then run it as administrator

zeocrash