2

I have a method that goes through loads of file types like so:

case ".jpg": 
    res = "image/jpeg"; 
    break;
case ".pdf": 
    res = "application/pdf"; 
    break;
case ".doc": 
    res = "application/msword"; 
    break;

..this goes on for a few hundred lines. And I have run into a StyleCop error: SP2101: Method body must not contain more than 120 code lines

I have searched around and can't find anything on this, let alone the suppression. Does anyone know how to suppress this message?

Edit: I think this is a StyleCop+ error and cannot be solved using the FxCop program to copy the suppression.

DevDave
  • 6,700
  • 12
  • 65
  • 99
  • 1
    Does this help? http://stackoverflow.com/questions/3287656/how-to-suppress-a-stylecop-warning – StingyJack Apr 11 '12 at 15:53
  • It appears to be a "StyleCop+" rule. See [link](http://stylecopplus.codeplex.com/documentation). Not sure how you would suppress it but perhaps the author could help. – Andrew Stephens Apr 11 '12 at 15:54
  • Why do you need to suppress the warning? Does your boss see it? Why can't you just ignore it? – Robert Harvey Apr 11 '12 at 18:08
  • We treat all warnings as errors so it will not compile without the suppression. There are a lot of good alternatives that have come up in the answers and I will remember that maybe in the future. But the code I already have and a suppression will do fine for now – DevDave Apr 12 '12 at 08:29
  • Why not use a dictionary with the content type as the key and the file extension as the value. Then your code will be a lot easier to understand. – Jonathan Parker May 28 '13 at 00:14

3 Answers3

8

You could refactor this to a Dictionary<string,string>:

var mimeTypesPerFileType = new Dictionary<string,string>();
mimeTypesPerFileType.Add(".jpg", "image/jpeg");
mimeTypesPerFileType.Add(".pdf", "application/pdf");
...

That would cut the number of lines in the method by about a third (though this could be in a separate method/field so you don't need to repopulate the dictionary every time).

And the method then just changes to:

return mimeTypesPerFileType[fileType];

Update:

Seeing as you have around 400 odd cases (around 1300 lines in the method), you really should load this map from a file or database. This will certainly reduce the number of lines.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
6

I am the author of StyleCop+.

One of the rule it adds to original StyleCop functionality is indeed SP2101 (MethodMustNotContainMoreLinesThan) which checks the size of your method.

Like any other StyleCop warning, it could be:

  • disabled, in that case it means you just don't need it
  • suppressed for some particular place in code (when you still need it generally, but not here)

Moreover, it is configurable. If you want to use this rule, you can put any number instead of 120 for your own configuration. 120 is just some default.

The point of this rule is around maintainability. It could be very important to check that you don't fall into spaghetti code. StyleCop is just a tool giving you an ability to get control over that. StyleCop+ offers some more rules to check. So if you wish to use them - go ahead. If you don't - just disable it. It is kind of a tool that really needs to be configured before the usage.

Let me know if you need any help with configuration.

Oleg Shuruev
  • 1,339
  • 8
  • 10
  • 5
    The suppression line for this rule would be [SuppressMessage("StyleCopPlus.StyleCopPlusRules", "SP2101:MethodMustNotContainMoreLinesThan", Justification = "Some justification")] – Oleg Shuruev Apr 11 '12 at 20:37
  • Thanks Oleg, didn't realise I could configure it, will have to take a look at it – DevDave Apr 12 '12 at 08:25
4

I don't use StyleCop, but one suggestion might be to change your approach. You could store all of these value pairs in an XML file, etc, and use a List(Of T) to organize them all. Your method could be reduced to a couple of lines.

J...
  • 30,968
  • 6
  • 66
  • 143
  • thanks for answers, although with 1300 lines I'm really hoping I can just suppress this right now! – DevDave Apr 11 '12 at 15:56
  • 4
    @Tyler: In that case, I would keep the violation until it's fixed. If you suppress it now, odds are you'll never fix it. – Austin Salonen Apr 11 '12 at 16:00
  • 1
    Really, it's a five minute job to write a script to build a formatted list from what you already have. It's another five minute job to refactor this into a three line method. Why not do it? – J... Apr 11 '12 at 18:13
  • Plus hours/days/weeks of regression testing :) – Jonathan Parker May 28 '13 at 00:17
  • @JonathanParker - testing 1300 simple cases? I think an automated test would take two minutes to write and about one second to execute. – J... May 28 '13 at 00:20