Regions: Love or Hate
-
I hate them. They are a way of making a file which is too complicated and should be split up appear not to be. If you think you need them, consider whether some of that code is actually necessary at all, or in the right place.
What do you consider the too complicated and need split threshold to be? IMO regions start being useful in keeping stuff organized well before that point.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
What do you consider the too complicated and need split threshold to be? IMO regions start being useful in keeping stuff organized well before that point.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
As soon as you can't navigate the file (in your mind or using something like the tree view which is in most IDEs showing you the structure) any more. That's obviously very subjective (and depends on the domain) but in general somewhere around 500-1000 lines.
-
As soon as you can't navigate the file (in your mind or using something like the tree view which is in most IDEs showing you the structure) any more. That's obviously very subjective (and depends on the domain) but in general somewhere around 500-1000 lines.
I've never used the IDE treeviews for within classes; showing them and the one for files/projects either hogs too much space or requires swapping between them all the time adding more annoyance than it's worth. Also, even with icons globbing everything into a class into a single flat list has never seemed logical to me. A large part of what I use regions for is clumping similar items together (similar to OriginalGriff's setup[^]). With that in place I find classes generally remain manageable until around 1500 lines. The only time this is often an issue is with complex UI forms; where I've yet to find a good balance between classes getting bloated or where a specific feature is in a pile of custom controls only used on a single dialog.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I have a love/hate relationship with regions. Personally I use them for breaking a code file into sections, so Fields, Properties, Public Methods etc... Now that I don't mind as I can still find things quite easily, I'm sure others might disagree but it works for me. What I hate is when developers put them around every method! Even worse is when they only use the method name instead of the whole signature, for example
#region MyMethod
public int MyMethod(int a, int b) { ... }
#endregion
#region MyMethod
public int MyMethod(int a, int b, string c) { ... }
#endregion
Which one is it that I need to look at? And what does this add as I can collapse the methods anyway! Seriously, if you do this then you should have your keyboard taken away from you until you know better!
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
Dave Kerr wrote:
Now I hate them, each member of our team uses them differently.
I think that's the issue right there .. its not regions that's the problem .. its inconsistent use. You could say the same for any tool/practice. I use them to group things away that I don't need to see, like private members etc, and I use them consistently for this reason.
The only thing unpredictable about me is just how predictable I'm going to be.
-
Marcus_2 wrote:
Good, if used with some sense.
You mean, like almost everything else ever done in programming? :)
Currently reading: "Swords and Deviltry", by Fritz Leiber
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I always say : when you use region, just create another class ! It's useful only for generated code (like proxy class for web services)
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I LOVE regions, but only on a structured, limited basis. They are helpful to organize the class, but can get unruly if overused. I stick to several standard regions and alphabetize my methods, properties, etc, so that everything is easy to find. - Catalyst
-
I've seen regions being abused (hiding shitty code to make it look clean), overused (why would you keep a method in a region?) and underused (as in, WTF? why would you hide all your code in a region titled "main process"). So I can safely say "I.HATE.THAT.SH*T!"
Excuse me for my improper grammar and typos. It's because English is my primary language, not my first language. My first languages are C# and Java. VB, ASP, JS, PHP and SQL are my second language. Indonesian came as my third language. My fourth language? I'm still creating it, I'll let you know when it's done! :-D
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I find them useful to separate true regions in the code, i.e., constructors, private properties, public properties, private methods and public methods. They are useful especially in code-behind pages with a lot of code that make for a lot of scrolling. When I collapse the code page to definitions, they are all you see between the class definition and Namespace declarations, making it easy to expand the region I'm looking for to view, edit or add to. Most of the comments I've seen here that oppose them seem to direct their hatred at how they are (mis)used, but don't really say why regions are in themselves bad. That's kind of like hating pencils because they can be used to write bad English. :)
-
Slacker007 wrote:
your team needs to ALL be on the same sheet of music
Oh yes! Nothing worse than one idiot individual who insists on K&R bracket format when everyone else has got an IQ bigger than their shoe size moved on to a more readable style.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
idiots people who like stupid space-wasting, conceptually-disorganized styles like Allman. But I guess the use of inferior editors that are incapable of brace matching, as well as dysfunctional eyes that cannot scan up-/downward to matching indents, are all factors in why they would give up as real developers need to use such a style. That, or they just really, really, really love Microsoft. 1TBS or GTFO
Narf.
-
This is what I call abuse/overuse. It makes my eyes roll back in my head when I see this crap, comments would've been more than sufficient.
foreach (DataRow currentRow in sortedTable.Table.Rows) { #region Load Data and set defaults creditCardNumber = currentRow\["CCNUMBER"\].ToString(); cardHolder = currentRow\["CARDHOLDER"\].ToString(); employeeId = currentRow\["EMPLCODE"\].ToString(); deptNumber = currentRow\["DEPTCODE"\].ToString(); accountNumber = currentRow\["GLCODE"\].ToString(); expElemNumber = currentRow\["EXPELEM"\].ToString(); fullRecord = currentRow\["FULLRECORD"\].ToString(); isValidDept = true; isValidAcct = true; #endregion #region If Company Credit Card, don't process if (creditCardNumber == companyCard) { Trace.WriteLineIf(tron, "\[VERIFICATION\] Skipping Company Credit Card... "); continue; } else { this.statusReport.Text = "Verifying " + creditCardNumber + "..."; this.rtbStatus.Text += "Verifying " + creditCardNumber + " \[" + cardHolder.Trim() + "\]...\\t"; Trace.WriteLineIf(tron, "\[VERIFICATION\] Verifying Credit Card # : " + creditCardNumber); email\_er.Write("Credit Card Number: " + creditCardNumber + " for Card Holder: " + cardHolder.Trim() + "..."); } #endregion #region Address Employee #, Dept #, Account #, and Expense Element if (employeeId.Trim().Length != 4) { employeeId = employeeId.PadLeft(4, '0'); } if (deptNumber.Trim().Length != 4) { deptNumber = deptNumber.PadLeft(4, '0'); } if (accountNumber.Trim().Length != 7) { accountNumber = accountNumber.PadLeft(7, '0'); } if (expElemNumber.Trim().Length != 2) { expElemNumber = expElemNumber.PadLeft(2, '0'); } #endregion #region Verify Deptartment Number from BoA File try { isValidDept = query.isValidDepartment(deptNumber); if (!isValidDept) Trace.WriteLineIf(tron, "\[VERIFICATION\] Bank of America Department is in
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I don't use them. I use Visual Studio's class explorer to navigate to the desired member, so don't need them. I don't mind if anybody else uses them though...
-
I wasn't sure until I started using them to group related items in the same file: Fields Properties Constructors Event handlers Public properties Private properties Now I find it reduces the clutter, and lets you see what you are interested in, without being distracted by irrelevancies. So much so that I modified the VS default class (and so forth) files to include them as standard. There is a Tip/Trick about it here: Adding your items to Visual Studio default files[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
That makes a lot of sense. I might try using them that way.
-
idiots people who like stupid space-wasting, conceptually-disorganized styles like Allman. But I guess the use of inferior editors that are incapable of brace matching, as well as dysfunctional eyes that cannot scan up-/downward to matching indents, are all factors in why they would give up as real developers need to use such a style. That, or they just really, really, really love Microsoft. 1TBS or GTFO
Narf.
Wouldn't know - I use Whitesmiths style. :-D
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Wouldn't know - I use Whitesmiths style. :-D
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
This is what I call abuse/overuse. It makes my eyes roll back in my head when I see this crap, comments would've been more than sufficient.
foreach (DataRow currentRow in sortedTable.Table.Rows) { #region Load Data and set defaults creditCardNumber = currentRow\["CCNUMBER"\].ToString(); cardHolder = currentRow\["CARDHOLDER"\].ToString(); employeeId = currentRow\["EMPLCODE"\].ToString(); deptNumber = currentRow\["DEPTCODE"\].ToString(); accountNumber = currentRow\["GLCODE"\].ToString(); expElemNumber = currentRow\["EXPELEM"\].ToString(); fullRecord = currentRow\["FULLRECORD"\].ToString(); isValidDept = true; isValidAcct = true; #endregion #region If Company Credit Card, don't process if (creditCardNumber == companyCard) { Trace.WriteLineIf(tron, "\[VERIFICATION\] Skipping Company Credit Card... "); continue; } else { this.statusReport.Text = "Verifying " + creditCardNumber + "..."; this.rtbStatus.Text += "Verifying " + creditCardNumber + " \[" + cardHolder.Trim() + "\]...\\t"; Trace.WriteLineIf(tron, "\[VERIFICATION\] Verifying Credit Card # : " + creditCardNumber); email\_er.Write("Credit Card Number: " + creditCardNumber + " for Card Holder: " + cardHolder.Trim() + "..."); } #endregion #region Address Employee #, Dept #, Account #, and Expense Element if (employeeId.Trim().Length != 4) { employeeId = employeeId.PadLeft(4, '0'); } if (deptNumber.Trim().Length != 4) { deptNumber = deptNumber.PadLeft(4, '0'); } if (accountNumber.Trim().Length != 7) { accountNumber = accountNumber.PadLeft(7, '0'); } if (expElemNumber.Trim().Length != 2) { expElemNumber = expElemNumber.PadLeft(2, '0'); } #endregion #region Verify Deptartment Number from BoA File try { isValidDept = query.isValidDepartment(deptNumber); if (!isValidDept) Trace.WriteLineIf(tron, "\[VERIFICATION\] Bank of America Department is in
-
This is what I call abuse/overuse. It makes my eyes roll back in my head when I see this crap, comments would've been more than sufficient.
foreach (DataRow currentRow in sortedTable.Table.Rows) { #region Load Data and set defaults creditCardNumber = currentRow\["CCNUMBER"\].ToString(); cardHolder = currentRow\["CARDHOLDER"\].ToString(); employeeId = currentRow\["EMPLCODE"\].ToString(); deptNumber = currentRow\["DEPTCODE"\].ToString(); accountNumber = currentRow\["GLCODE"\].ToString(); expElemNumber = currentRow\["EXPELEM"\].ToString(); fullRecord = currentRow\["FULLRECORD"\].ToString(); isValidDept = true; isValidAcct = true; #endregion #region If Company Credit Card, don't process if (creditCardNumber == companyCard) { Trace.WriteLineIf(tron, "\[VERIFICATION\] Skipping Company Credit Card... "); continue; } else { this.statusReport.Text = "Verifying " + creditCardNumber + "..."; this.rtbStatus.Text += "Verifying " + creditCardNumber + " \[" + cardHolder.Trim() + "\]...\\t"; Trace.WriteLineIf(tron, "\[VERIFICATION\] Verifying Credit Card # : " + creditCardNumber); email\_er.Write("Credit Card Number: " + creditCardNumber + " for Card Holder: " + cardHolder.Trim() + "..."); } #endregion #region Address Employee #, Dept #, Account #, and Expense Element if (employeeId.Trim().Length != 4) { employeeId = employeeId.PadLeft(4, '0'); } if (deptNumber.Trim().Length != 4) { deptNumber = deptNumber.PadLeft(4, '0'); } if (accountNumber.Trim().Length != 7) { accountNumber = accountNumber.PadLeft(7, '0'); } if (expElemNumber.Trim().Length != 2) { expElemNumber = expElemNumber.PadLeft(2, '0'); } #endregion #region Verify Deptartment Number from BoA File try { isValidDept = query.isValidDepartment(deptNumber); if (!isValidDept) Trace.WriteLineIf(tron, "\[VERIFICATION\] Bank of America Department is in
I think this is a good example of good usage. The advantage a region has over a comment is that a region can signal the end of your logical block. With a comment you would have to asume that the comment is valid until the next comment. And as you know, assumtion makes an ... out of u and me.
-
I used to love regions, stuck them everywhere in my code. Now I hate them, each member of our team uses them differently. What's the public opinion - are regions good or bad? Would they be better if they were REALLY big in Visual Studio? I find that I'm squinting to find the little buggers, if they were much more obvious I might get lost less...
I generally group code blocks in regions according to purpose. The file menu and edit menu each gets its own region, and printing, if the project does any printing. It helps me stay organized and makes various blocks of code easier to find.
XAlan Burkhart