What's a possible justification for not wrapping long argument lists? (StyleCop SA1115)

StackOverflow https://stackoverflow.com/questions/352821

  •  20-08-2019
  •  | 
  •  

Question

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"?

Was it helpful?

Solution

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

OTHER TIPS

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)

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top