3

I'm using ReSharper and CodeMaid, both have their own "organize" code option but the problem is that i have no idea how to tell them what is an event and what is a method/function that i coded.

If i reorganize code (i want regions) it groups every control event and method together.

Is there a way to tell them what is a method and what is an event? or is there another extension that is specifically better for reorganizing code the way i want?

I want something like this:

private void button1_click(object sender, EventArgs e){}

private void MyMethod1(){}

private void MyMethod2(){}

private void button2_click(object sender, EventArgs e){}

to become:

#region Events
private void button1_click(object sender, EventArgs e){}

private void button2_click(object sender, EventArgs e){}
#endregion Events

#region Methods
private void MyMethod1(){}

private void MyMethod2(){}
#endregion Methods
Hikaros
  • 49
  • 1
  • 4
  • 1
    [Why was my question closed as off topic?](http://meta.programmers.stackexchange.com/a/6487/) – Adam Zuckerman Sep 23 '14 at 00:14
  • http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Usage_Scenarios__Reordering_Type_Members.html – rwong Sep 23 '14 at 00:15
  • 1
    Don't use regions. You can achieve the organization properly by sorting methods alphabetically using resharper. But don't use regions. Regions are evil and annoying and deprecated. – Stephen Sep 23 '14 at 00:23
  • @Stephen: When have they been deprecated? Any source for this? – phresnel Sep 25 '18 at 07:01
  • I don't mean that it's been formally deprecated, but informally it was deprecated from the moment Partial classes were introduced into C#. Regions were created to hide auto-generated code, primarily in Winforms. With the invention of partial classes, all of this was able to be hidden in a separate file. We already have tools to group related code - classes and methods. We already have tools to add comments to code - comments. Partial classes fulfilled the last valid use for the #region directive. They will never be fully removed from the C# spec for compatibility reasons but don't use them. – Stephen Sep 26 '18 at 03:31

2 Answers2

4

Regions shouldn't be used. Never.

If you want to organize methods, properties, fields, events, etc., you may be interested in following StyleCop rules and in making StyleCop checking mandatory during every commit.

Also, if you're currently writing code in a basic text editor, you may be interested in moving to Visual Studio, where methods, properties, fields, events, etc. have distinctive icons in Intellisense which make it very easy to recognize visually. Note that Visual Studio Express is free.

Arseni Mourzenko
  • 134,780
  • 31
  • 343
  • 513
  • Often people use the excuse "sorting into regions makes it easier to get to the methods", but that's what the right hand drop down list above the code area is for. There is no good use case for regions since partial classes have been introduced to .NET. – Stephen Sep 23 '14 at 00:34
  • 1
    @Stephen - "sorting into regions makes it easier to get to the methods" - no, if your methods are hard to get to, your class is too complex. – Telastyn Sep 23 '14 at 00:47
  • I agree completely with you @Telastyn. – Stephen Sep 23 '14 at 00:50
  • Haha the only reason of why i want regions is so they can show up as "headers" in spade when using CodeMaid. – Hikaros Sep 23 '14 at 01:15
  • @Stephen: Partial classes should most definitely **not** be used for grouping. The fact that you'd say they make regions unnecessary is horrific in the extreme. I agree that you should not need regions, because you should keep your classes small, but partial classes should not come into this at **all**. – Magus Sep 23 '14 at 14:44
  • @Magus: I believe Stephen was not saying that OP should use partial classes to group class members, but rather that the only valid purpose of regions (i.e. isolation of generated code from code written by a developer) disappeared since the introduction of partial classes. – Arseni Mourzenko Sep 23 '14 at 14:48
  • @MainMa: Ah, well, at least no one else reading it will mistake that now. That makes far more sense. – Magus Sep 23 '14 at 14:53
  • @Magus MainMa is correct. I would never suggest using partial classes to group anything. Regions were invented to solve the problem that partial classes solves much better - generated code. Now that use case is gone, there is no good reason for regions. – Stephen Sep 23 '14 at 23:36
  • I disagree with this. If you inherit a 2,000 line legacy class, a first step in trying to break it apart can be running StyleCop and adding regions for easier navigation. Even in small, well designed classes, I still may want to collapse 3-4 class variables or the constructors. Using regions seems more a personal preference that may be indicative of bad code design, but is not always. – Ryan Mar 19 '19 at 15:18
  • @Ryan: seems like a case where you would rather want to split the class in two or more classes, instead of hiding the complexity by adding regions. If your *first step* is a temporary one, then it's perfectly fine to use regions, as soon as you remove them before pushing your modifications to the version control. – Arseni Mourzenko Mar 19 '19 at 20:49
  • @ArseniMourzenko in an ideal world I agree, but if my team has estimated a story at X and I come back and say the area needs a lot of work and that I can start the process of cleaning it up in 2X or I can clean up the whole thing in 5-10X, the former will likely be chosen by the team in any case where X is anything other than very small. Regions seem helpful in code bases where cleaning up a legacy class may be done by several developers, in sequence, over the course of months or years. – Ryan Mar 19 '19 at 22:26
  • @Ryan: I'm not sure I see a case where adding regions would be five times easier than splitting classes. Do you have any concrete example? – Arseni Mourzenko Mar 20 '19 at 10:19
  • @ArseniMourzenko It’s not that adding regions is five times easier, it’s that the process of splitting apart a massive class takes more time and requires any functionality that touches it to be retested. Whereas cleaning up the code incrementally allows that work to be broken up into smaller chunks, which makes it easier for teams to add it into their workload. – Ryan Mar 20 '19 at 13:25
  • One good thing I can think of is that it make it possible for IDE to collapse on code for a better view when you have a very big source file. – joe Feb 24 '20 at 05:33
2

Is there a way to sort code properly into regions dividing methods and events?

No.

Is there a way to sort code properly divide methods and events?

Yes. Follow the resharper guidelines, sorting by accessibility and then alphabetically. You can then use ctrl+e, ctrl+c (code cleanup) to use resharper to sort them for you.

Stephen
  • 8,800
  • 3
  • 30
  • 43
  • Makes me sad, i guess i'll go with it and StyleCop as suggested before :c thanks. – Hikaros Sep 23 '14 at 01:41
  • The other advantage of using stylecop is that it'll make your code easier to read for others and will get you used to "generic" c# code (i.e. make other people's code easier to read for you). – Stephen Sep 23 '14 at 02:16