Do you use regions with long ads switching / listing?

Recently, I needed (yes, I need) to define absurdly long switch and enum expressions in C # code, but I wonder what people think is the best way to break them down into logical subparts. In my situation, both enumeration values ​​and cases (which are based on enumeration values) have fairly clear groupings, but I'm not sure how to reflect this in the code.

Please note that in my code I have about 5 groups of 10 and 30 values ​​/ enumeration cases each.

Three vaguely reasonable options that I can foresee are as follows:

  • Define #region blocks all logical groups of case / enum values ​​in the declaration (optionally separated by blank lines).
  • Comment on each group with this name with an empty line before commenting on the group name.
  • Do nothing - just leave the / enum switch as a huge list of cases / values.

What do you prefer? Could you separately consider the listings and switches? (This would seem a little strange to me.) Now I would not say that there is a right / wrong answer to this question, although I would still be very interested in learning what the general concept of views is.

Note 1: This situation, when I can potentially have an extremely long declaration of an enumeration of 50/100 + values, is unfortunately inevitable (and similarly with a switch), as I try to write a tokeniser, and this seems to be the most reasonable way approach for several reasons.

Note 2: I am fully aware that there are already several recurring questions about whether to use regions in a common code (for structuring classes mainly), but I feel that my question here is much more specific and has not been addressed yet.

+6
enums c # switch-statement
source share
6 answers

Of course, talk about it. They probably don't change much, and when they do, you can expand the region, make changes, collapse it, and go to the rest of the file.

They are for some reason, use them to their advantage.

+3
source share

You can also have a dictionary <[your_enum_type], Action> (or Func instead of Action) or something like that (given that your functions have a similar signature). Then you can instead of using the switch instead of:

  switch (item) { case Enum1: func1(par1, par2) break; case Enum2: func2(par1, par2) break; } 

you might have something like:

 public class MyClass { Dictionary<int, Action<int, int>> myDictionary; //These could have only static methods also Group1Object myObject1; Group2Object myObject2; public MyClass() { //Again, you wouldn't have to initialize if the functions in them were static myObject1 = new Group1Object(); myObject2 = new Group2Object(); BuildMyDictionary(); } private Dictionary<int, Action<int, int>> BuildMyDictionary() { InsertGroup1Functions(); InsertGroup2Functions(); //... } private void InsertGroup2Functions() { myDictionary.Add(1, group2.AnAction2); myDictionary.Add(2, group2.AnotherAction2); } private void InsertGroup1Functions() { myDictionary.Add(3, group1.AnAction1); myDictionary.Add(4, group1.AnotherAction1); } public void DoStuff() { int t = 3; //Get it from wherever //instead of switch myDictionary[t](arg1, arg2); } } 
+3
source share

I would leave it as a huge list of cases / values.

+1
source share

If there are cases that have the same code block, using the Strategy strategy template, you can remove the switch block. This can create many classes for you, but it will show how complex it is and separate the logic in smaller classes.

+1
source share

Get rid of enumerations and turn them into objects. Then you can call methods on your objects and leave the code shared, supported, not a nightmare.

There are very few cases where you really need to use an enumeration instead of an object, and no one likes long switch statements.

0
source share

This is a good label for people who use regions.

I switched between Eclipse and Visual Studio when I tried to switch to full screen in VS by clicking

  Ctrl-MM 

and now, the area is closed and expanded!

0
source share

All Articles