Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# naming and casing conventions

Tags:

c#

I'm a VB guy that used to prefix module level variables with "_".

I'm using FXCop, StyleCop and (I think the built in Code Analysis, or maybe that is pointing to FXCop, not sure) and I am trying to adopt the most accepted naming conventions. How would you name the following module level private, property and param fields to make all of these code analysis tools happy and conform to commonly accepted or MS standards? Note that having a param name the same as a private module level field can be confusing and FXCop is incorrectly telling me to prefix the "sourcefile" param field reference with "this."

Is my approach to use lower case for module level privates acceptable and all I really need to do is rename the param to something unatural like "mySourceFile" or "sourceFileIn?" It feels forced. params should be Camel cased. Is my module level variable missed cased?

    public class Restartability
    {
        private readonly string sourceFile;

        public Restartability(string sourceFile)
        {
            this.sourceFile = sourceFile;
        }

        public string SourceFile
        {
            get { return sourceFile; }
        }  

   }
like image 235
Chad Avatar asked Nov 22 '25 10:11

Chad


1 Answers

I think that private field variables should be prefixed with a '_'.

This would give you:

private readonly string _sourceFile;

However, it can be argued that no prefix should be necessary (from the book Clean Code), since you shouldn't have so many variables in a class that it becomes hard to tell the difference between field scoped varialbes, and non-field ones.

like image 194
Sheldon Warkentin Avatar answered Nov 23 '25 23:11

Sheldon Warkentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!