1

This question already has an answer here:

This is a naming convention question.

In C#, someone suggested that variables of type List<T> should be named as listBlah instead of just blah. Similarly for variables of type Dictionary<keyT, valueT>. I think the reason is because we can guess the type of a variable by just looking at its name, without having to find where it is declared.

But when you create an object of a class with name "MyClass", what is some good practice to name the object (after the class' name)?

When you create a variable of primitive types such as int, char, string, double, what is some good practice to name it?

I guess this question is not just for C#, but also for other programming languages.

marked as duplicate by Ben Cottrell, RubberDuck, Telastyn, Robert Harvey, candied_orange Apr 14 '17 at 4:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    As a reader of somebody else's code, I don't want to see names which describe how the code works - that is not helpful at all to me. I need names which describe the intent of the person who wrote the code. such as what requirement they're trying to satisfy, what behaviour or functionality they're implementing, and what something represents which can relate back to the requirements or behaviour. I've never met a programmer who had difficulty figuring out how code worked just from looking at what it does, so there's no need for names and/or comments which state obvious information. – Ben Cottrell Apr 13 '17 at 23:16
  • If you do this, would you rename it when you switch from a List to an IEnumerable? Doubtful... I'd recommend 2 things. A) Go read a bunch of Code Review answers and B) Read Joel Spolsky's seminal article on Hungarian Notation, Making Code Look Wrong – RubberDuck Apr 13 '17 at 23:43
3

This naming style has fallen out of favour, except in the case of Interfaces, which by convention in c# start with 'I'

Other related exceptions are things like Views, Controllers, ViewModels where the appropriate word is sometimes appended to the class name.

Edit : Oh and 'Async' for async Methods is popular

  • 3
    The I for interface convention currently only infests C#. It'd be nice if it didn't spread. – candied_orange Apr 14 '17 at 4:04
  • it is kinda nice to see the difference. maybe they could just make it a different colour instead – Ewan Apr 14 '17 at 4:07
  • 1
    Interface, abstract, or concrete there is no reason the client using this dependency needs to know. Stop making me look at details I don't care about. Don't know. Don't want to know. – candied_orange Apr 14 '17 at 4:10
  • 1
    I think the reason for the I is so you can have IMyClass and MyClass without having the think of a new name for one or the other – Ewan Apr 14 '17 at 14:24
  • 1
    maybe we should have MyClass as the interface and IMyClass as the _I_MPLEMENTATION! – Ewan Apr 14 '17 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.