English 中文(简体)
SP2101:方法学机构不得包含120条以上的编码线——我如何加以压制?
原标题:SP2101: Method body must not contain more than 120 code lines - How can I suppress this?

我有这样的方法:

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

数百条。 我的风格错误是:SP2101: 方法机构不得包含120条以上的代码项目

我一直在寻找,可以找到这方面的任何东西,更不用说镇压。 是否有任何人知道如何压制这一信息?

Edit:我认为这是一个的错误,不能使用FxCop方案复制镇压。

最佳回答

我是“普惠制+”的作者。

它对原样式Cop功能的一种补充规则是SP2101(MethodMust NotContainMoreLinesThan),其中检查了贵方方法的规模。

如同任何其他风格的警示一样,可以:

  • 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)

此外,这是可以想象的。 如果你想使用这一规则,你可以把任何数字改为120,供你自己的组合使用。 <>120只是一些缺省。

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.

让我知道,你们是否需要帮助组合。

问题回答

<could> recent this to a Dictionary<string,string>:

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

这将使该方法中的线数减少约三分之一(尽管这可能是一种单独的方法/地点,因此,你不需要每次重新接纳字典)。

因此,这种方法只是修改了:

return mimeTypesPerFileType[fileType];

最新情况:

如你有大约400个奇案(方法上约有1300条线),你确实应从档案或数据库中装载这一地图。 这将减少线路数量。

我不使用风格,但一项建议可能是改变你的做法。 您可在XML文档中储存所有这些数值,并使用<代码>List(Of T)将其全部整理。 你的方法可以分为两条。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签