My life was empty and had no meaning, but then I discovered regions
-
This codebase is a fucking gold mine I tell you.
#region StuffManager public class StuffManager { #region public properties public int Counter { get; set; } public string Message { get; set; } #endregion #region private fields private int innerCounter; #endregion #region constructors public StuffManager() { } public StuffManager(int count) { innerCounter = count; } #endregion #region do work method public void DoWork() { #region do work method body //do work //... //do some more #endregion } #endregion } #endregion
-
Some of those are formats I've seen before. Not what I'd bother doing but I see why people do.
Others appear to be from a misconception that you can only fold regions and not, e.g. method bodies
-
@jaloopa said in My life was empty and had no meaning, but then I discovered regions:
Others appear to be from a misconception that you can only fold regions and not, e.g. method bodies
Was method folding always a thing? I didn't think that was added until VS2010.
-
@jaloopa said in My life was empty and had no meaning, but then I discovered regions:
not, e.g. method bodies
You can fold an individual method body, but not a set of method bodies (with their declarations) into one block.
One thing to be careful of is that ReSharper does not honor regions [or even conditionals]....
I would (generally) prefer regions in many places over the use of distinct files with partial classes [the exception being the separation of manual vs. generated code]
-
@thecpuwizard said in My life was empty and had no meaning, but then I discovered regions:
You can fold an individual method body, but not a set of method bodies (with their declarations) into one block.
Yeah, I was specifically talking about this bit:
public void DoWork() { #region do work method body //do work //... //do some more #endregion }
-
There are also classes like this all over the place:
public class MySpecialService { //.. //huge block of code //.. #region index field private int index; #endregion }
-
@jaloopa We had one developer who very strongly and thoroughly insisted to always have these regions in each .cs or even class:
- using
- Declarations (of member variables and static members)
- Life Cycle Management (constructors, destructor, maybe Init methods mandated by interface)
- Property Getters & Setters
- Public Interface (all public methods)
- Private Methods
Oh, and if that's not obvious enough there should be a comment block before each start of a region like this:
//************************************************************** // Declarations Section //**************************************************************
-
Due to the above, I heartily recommend anyone to install the following Visual Studio extension:
-
@jbert Ah yes, the "A handful of people brazenly abuse a feature, therefore it's the spawn of Satan" logic.
-
@raceprouk That is the only correct sentiment in this situation. The only time regions are helpful is when you're already doing something completely stupid and overcomplicating things.
I could maybe imagine seeing them in tests without being horrified, but honestly, just use files for that.
-
@raceprouk said in My life was empty and had no meaning, but then I discovered regions:
@jbert Ah yes, the "A handful of people brazenly abuse a feature, therefore it's the spawn of Satan" logic.
@raceprouk Did you actually follow that link? For the record, all it does is unfold them by default (you can fold them again manually or by executing the "fold members" action - or however it's called) and make the lines defining a region fade into the background.
The clickbait name is actually a bonus feature.
-
@jbert I prefer having regions (and code blocks) folded by default, and expand only the codeI'm working on.
-
@magus said in My life was empty and had no meaning, but then I discovered regions:
The only time regions are helpful is when you're already doing something completely stupid and overcomplicating things.
Or you're implementing complex interfaces and/or want to hide boilerplate (very useful when using dependency properties in XAML).
-
@jbert said in My life was empty and had no meaning, but then I discovered regions:
thoroughly insisted to always have these regions
Remember with style templates, these can be done automatically so there is not necessarily any manual effort involved.
More than regions per se, I find discussions on the layout [sequence] of elements in a file to be quite interesting....
-
One guy I know puts every single line of code in its own region (at least he did. Not sure if he still does). Each comment line gets their own regions as well.
He names them things like 'If 1', 'For Loop 4', etc.
One file had over 1 billion lines because of that. VS froze for about 10 minutes every time he had to edit it.
-
@brisingraerowing said in My life was empty and had no meaning, but then I discovered regions:
One file had over 1 billion lines because of that. VS froze for about 10 minutes every time he had to edit it.
Funny, I have never gotten VS to load any file over 3.7 million lines...
-
@jbert said in My life was empty and had no meaning, but then I discovered regions:
Oh, and if that's not obvious enough there should be a comment block before each start of a region like this:
Hey, @RaceProUK, can I borrow your cluehammer?
-
@antiquarian said in My life was empty and had no meaning, but then I discovered regions:
@jbert said in My life was empty and had no meaning, but then I discovered regions:
Oh, and if that's not obvious enough there should be a comment block before each start of a region like this:
Hey, @RaceProUK, can I borrow your cluehammer?
Of course! Just make sure you clean it before you return it ;)
-
@jbert there was this rule, in the same workplace a moron complained I used the "+" operator instead of String.concat
-
hmpf. What a little region. A former boss of mine was more capable:
/// <summary> /// some description /// </summary> private void DoSomething() { ... #region do X ... #endregion #region do Y ... #endregion #region do Z ... #endregion #region do A ... #endregion #region do B if (some condition) { #region do C ... #endregion } else { #region do D ... #endregion } #endregion ... #region do E try { switch (some swicth) { #region do F case ... break; #endregion #region do G case ... break; #endregion #region do H case ... break; #endregion #region do I case ... break; #endregion } ... #endregion }
That was a little 320 loc function in the main window of the application which had a mere 13395 lines of code then.
-
@berniethebernie said in My life was empty and had no meaning, but then I discovered regions:
That was a little 320 loc function in the main window of the application which had a mere 13395 lines of code then.
Make up your mind.
-
@pie_flavor said in My life was empty and had no meaning, but then I discovered regions:
@berniethebernie said in My life was empty and had no meaning, but then I discovered regions:
That was a little 320 loc function in the main window of the application which had a mere 13395 lines of code then.
Make up your mind.
The whole program had 13395 loc, but that particular function had only 320 loc.
-
@djls45 It's about using "loc" then "lines of code" in the same sentence, at a guess.
-
CodeMaid has a nice option regarding regions: it can remove all of them.
I used to put everything in regions (or rather, have a plug-in do that) because I felt it looked "cleaner", but I changed my mind on that. If you keep your code nicely sorted (something which CodeMaid can also do) there's no need for regions.
If you need regions to group members of a certain class because said class does multiple things, you've got other problems to tend to.
-
@alexmedia said in My life was empty and had no meaning, but then I discovered regions:
CodeMaid has a nice option regarding regions: it can remove all of them.
I used to put everything in regions (or rather, have a plug-in do that) because I felt it looked "cleaner", but I changed my mind on that. If you keep your code nicely sorted (something which CodeMaid can also do) there's no need for regions.
If you need regions to group members of a certain class because said class does multiple things, you've got other problems to tend to.
Agree with all of the above - but strongly suggest consistency. Doing one in a certain set of files and then doing something different in other (especially related) files or because it is a different day of the week is ...[Censored]
-
@djls45 said in My life was empty and had no meaning, but then I discovered regions:
The whole program had 13395 loc
Oh no. That was the MainWindow. There were some more of such abominations in the code base.
-
@berniethebernie said in My life was empty and had no meaning, but then I discovered regions:
Oh no. That was the MainWindow. There were some more of such abominations in the code base.
I remember such a window in the client program at my first job. I actually brought it up in one of our team meetings. "The Orders form is 15,000 lines of code, and it's basically a big multi-tab interface. It could be made a lot more manageable by refactoring each tab out into a
Frame
."Cue panicky reactions by the manager and the architect. "It works; don't touch it! It works; don't touch it!"