Search code examples
c#resharperstylecop

Which is better style, an ambiguous, notated, or renamed parameter?


Stylecop seems inconsistent in naming parameters or perhaps I'm missing something?

I have

    Movement movement;

    public void SetMovement(Movement movement)
    {
        this.movement = movement;
    }

which complains about my parameter hiding a field, which is understandable. Then I try to name the parameter differently

    public void SetMovement(Movement _movement)
    {
        this.movement = _movement;
    }

which will complain about underscores and Hungarian notations, etc.

It seems the only way to satisfy stylecop in this case would be to name the parameter something different like

    public void SetMovement(Movement movementParameter)
    {
        this.movement = movementParameter;
    }

Which of these would be considered the best 'style'?


Solution

  • In this case, I would did (and I actually do) just the same thing you did: calling the parameter just the same as private field.
    That code is pretty clear to me, despite StyleCop judgement 🤨
    In other situations I use different names for parameters and equivalent fields, e.g. when the meaning of the passed parameters (and so its name) changes when it is injected in a method.
    I'm not a StyleCop guy, but I think you can say it to not bother you using an attribute, a special comment or so 😉
    Maybe like this: How to suppress a StyleCop warning?