views:

394

answers:

4

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

+2  A: 

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

Paul Tomblin
<- this (root problem = too many arguments for method)
annakata
Ok: specific instance then. I'm doing a Console.WriteLine and printing a bunch of different information (about 10 items) in a formatted string. How do you make that argument list shorter? (Really, I'd love to know!)
Stewart Johnson
A: 

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

J Cooper
If you really like "hey! This line is a continuation!", you should try FORTRAN-IV. :-)
Paul Tomblin
oh man, that's ugly. I see that a lot when people writing SQL queries -- they start each item in the SELECT clause with a new line and a comma at the start. When people start writing stuff like that they've really taken their eye off the ball. :-/
Stewart Johnson
hehe, I don't like the style. Was just pointing out that it is sometimes done, and maybe done by the code analysis tool writers or something, hence the recommendation :P
J Cooper
This is done so you can easily comment out a line of code without having to go delete the comma from the line above. I agree though, it's hideous.
womp
A: 

Hi there.

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.

Cheers. Jas.

Jason Evans
A: 

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)

Jason Allor

related questions