不包装长参数列表的可能理由是什么? (StyleCop SA1115)

发布于 2024-07-09 16:34:48 字数 275 浏览 11 评论 0原文

我陷入了 ReSharper 和 StyleCop 之间的战斗,我想让 ReSharper 获胜,但在这样做之前我想听听支持 StyleCop 的论点。

当我编写长参数列表时,ReSharper 会明智地截断参数列表并在下一行重新启动它。 我发现这更具可读性。

当我对代码运行 StyleCop 时,它希望我将这些行保留得很长。 我不喜欢这样,所以我想忽略 StyleCop 规则 (SA1115)。 我想不出 SC 一开始想要排这么长队的好理由——这只是“我们一直都是这样做的”吗?

I'm stuck in a battle between ReSharper and StyleCop, and I'd like to let ReSharper win, but I want to hear the arguments in favour of StyleCop before I do that.

When I'm writing long argument lists ReSharper sensibly chops the parameter list and restarts it on the next line. I find that much more readable.

When I run StyleCop over the code it wants me to leave those lines really long. I don't like that, so I want to ignore that StyleCop rule (SA1115). I can't think of a good reason why SC would want those long lines in the first place – is it just a case of "we've always done it this way"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁年轻赶紧闹 2024-07-16 16:34:48

StyleCop 不希望您将所有参数放在很长的一行上。 但是,它也不希望您随意插入换行符以将参数列表的一部分向下移动到下一行。 StyleCop 希望您执行以下操作之一:

public void MyMethod(int param1, int param2, int param3)

public void MyMethod(
    int param1, int param2, int param3)

public void MyMethod(
    int param1,
    int param2,
    int param3)

StyleCop doesn't want you to put all your parameters on one really long line. However, it also doesn't want you to just arbitrarily insert a newline to move part of the parameter list down to the next line. StyleCop would like you to do one of the following:

public void MyMethod(int param1, int param2, int param3)

public void MyMethod(
    int param1, int param2, int param3)

public void MyMethod(
    int param1,
    int param2,
    int param3)
夜无邪 2024-07-16 16:34:48

它可能是为了提醒您,您的论点列表太长,应该缩短。

It's probably there to remind you that your argument list is too long and should be shortened.

三人与歌 2024-07-16 16:34:48

在使用此问题,我也通过从 VS IDE 运行 StyleCop 遇到了 SA1115 的问题。 经过一番思考后,StyleCop 觉得还不错的最终结果是:

public static string Format<T>(string pattern, T template)
{
    Dictionary<string, string> cache = new Dictionary<string, string>();

    return RegexExpression.Replace(
        pattern, 
        match =>
    {
        string key = match.Groups[1].Value;
        string value;

        if (!cache.TryGetValue(key, out value))
        {
            var prop = typeof(T).GetProperty(key);

            if (prop == null)
            {
                throw new ArgumentException("Not found: " + key, "pattern");
            }

            value = Convert.ToString(prop.GetValue(template, null));
            cache.Add(key, value);
        }

        return value;
    });
}

只是想分享一下。

Whilst playing about with the code from this question, I also fell foul of SA1115 via running StyleCop from the VS IDE. After some mucking about, here is the end result which StyleCop felt was OK:

public static string Format<T>(string pattern, T template)
{
    Dictionary<string, string> cache = new Dictionary<string, string>();

    return RegexExpression.Replace(
        pattern, 
        match =>
    {
        string key = match.Groups[1].Value;
        string value;

        if (!cache.TryGetValue(key, out value))
        {
            var prop = typeof(T).GetProperty(key);

            if (prop == null)
            {
                throw new ArgumentException("Not found: " + key, "pattern");
            }

            value = Convert.ToString(prop.GetValue(template, null));
            cache.Add(key, value);
        }

        return value;
    });
}

Just thought I'd share it.

烟燃烟灭 2024-07-16 16:34:48

似乎规则从技术上讲是“参数必须遵循逗号”。 如果你问我的话,我是相当挑剔的,但有些人相信用逗号开始续行才能真正显示嘿! 这句话是一个延续! 例如,

void Foo(  int blah
         , string blork
         , ...

无论你的船是什么,就个人而言:)

It seems the rule technically says "parameter must follow comma." Pretty nit-picky if you ask me, but some people believe in starting continuation lines with the commas in order to really show hey! This line is a continuation! E.g.

void Foo(  int blah
         , string blork
         , ...

Whatever floats your boat, personally :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文