5

I think SA1402 is a great rule but I have problem with generics. I've got a class that uses the Func delegate, so the names roughly parallel that signature. That is, I have classes named Operation<TType>, Operation<T, TType>, Operation<T1, T2, TType> and so on. According to SA1402, I need to put all these small classes in separate files and come up with some strange decoration for the file name. Additionally, if I need to change one of these items I'll typically need to make changes to the rest. This seems less supportable than a single module.

Does it make sense for SA1402 to allow generics of the same basic class (as well as partials) to reside in one file? In this case all permutations of the class Operation would reside in 'Operation.cs'.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Quark Soup
  • 4,272
  • 3
  • 42
  • 74
  • Several suggestions here: [Convention for Filenames of Generic Classes][1] [1]: http://stackoverflow.com/questions/804036/convention-for-filenames-of-generic-classes – Benoit Blanchon Aug 31 '13 at 16:31

3 Answers3

4

They are several nameing convension. It seems the most popular is

Operation[TType].cs

Operation[T,TType].cs

Operation[T1, T2, TType].cs

But you can also use something more classic, like

Operation`1.cs

Operation`2.cs

Operation`3.cs

(see Convention for Filenames of Generic Classes)

Community
  • 1
  • 1
Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
4

I tend to agree with you, in general, SA1402 is a good idea. But in this specific case, and others like it (i have a generic and non-generic implementation of the same class). Like many others I think having more than one class per file is totally fine if it makes sense. I choose to ignore this warning at a very specific Scope and Target level:

[assembly: SuppressMessage(
    "StyleCop.CSharp.MaintainabilityRules",
    "SA1402:FileMayOnlyContainASingleType",
    Justification = "How else would we name generic and non generic (https://stackoverflow.com/a/4063061/516433).",
    Scope = "type",
    Target = "~T:Pastdev.Cli.AppResources`1<!!0>")]
Lucas
  • 14,227
  • 9
  • 74
  • 124
  • You have specified the Target on a generic class with one type param - do you have any reference on how to notate generic classes with more than one type param? – Yoshiya Jan 15 '19 at 16:02
  • I have no reference, but when using Visual Studio to generate it, it appears: `[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "", Scope = "type", Target = "~T:Mitre.CliApp.IO.CircularBuffer\`2")]`. Also, probably worth noting that I don't think you can have multiple targets on the same line. – Lucas Jan 15 '19 at 17:14
-1

Using the Operation<TType>.cs approach will work if you have overridden the StyleCop's fileNamingConvention setting (File naming conventions).

Otherwise, it works automatically if you name your file like: Operation{TType}.cs, etc. (Note the curly braces)

crgolden
  • 4,332
  • 1
  • 22
  • 40
  • Yes, I know how to override Stylecop settings. The whole idea is to change your code so that you pass without changing the settings. – Quark Soup Jan 31 '20 at 02:04