How to deal with Conflicting coding conventions?
Generally we use various static code analysis tools to analyze our code fo开发者_如何学编程r validation. But I've seen some conflicting scenarios.
As an example if we use class variables, the StyleCop will suggest us to use
this.Name = myName
instead of,
Name = myName
But this will pop up a Resharper error, "Redundant qualifier" and will suggest to not to use "this." notation.
So in such scenarios I need to check a more consistent reference to choose what is correct/Best. Is there any such resource that "defines" the correct conventions?
There is no correct convention, you adopt the one you prefer and that is your baseline/reference.
if you use both ReSharper and StyleCop you should set them up to work together meaning to accept and validate code in the same way.
That's a subjective question, so here's my subjective answer: I agree with Resharper and think that this
is redundant. Personally I prefix field names with underscore:
public class Foo
{
private readonly string _name;
public Foo(string name)
{
_name = name;
}
}
Then I configure the static analysis tools to obey the conventions I use.
Different tools suggest different things. I would suggest making your own coding guideline document and share that with your team (do start from an existing coding convention). A good starting point however is the book "Framework design guidelines" ISBN: 978-0321545619
You could also configure resharper to give the warnings/errors you want and have all team members use the same resharper config so they will get the same errors/warnings.
Don't do just everything a coding guideline wants, based on the situation it's sometimes better to not stick with it, but if you don't follow the guideline make sure to comment why.
They're more like guidelines anyway savvy ;).
I think using FxCop is more useful because it is being provided by Microsoft, so what can be more authentic then Microsoft. http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx
I need to check a more consistent reference to choose what is correct/Best. Is there any such resource that "defines" the correct conventions?
These questions cannot be answered. References cannot help you "choose what is correct". And conventions by their very nature do not have a 'correctness' property - we use conventions to arbitrarily decide a consistent approach to those questions that do not have a correct answer.
If you want to follow StyleCop's guideline here, you can configure ReSharper to stop complaining about the use of this
. This is in ReSharper | Options | Code Inspection | Inspection Severity
- or, I believe, there is a specific 'StyleCop for ReSharper' plugin which will handle this for you.
精彩评论