1

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.

Tim
  • 5,405
  • 7
  • 48
  • 84
  • 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](https://codereview.stackexchange.com/) answers and B) Read [Joel Spolsky's seminal article on Hungarian Notation, Making Code Look Wrong](https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/) – RubberDuck Apr 13 '17 at 23:43

1 Answers1

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

Ewan
  • 70,664
  • 5
  • 76
  • 161
  • 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 04:04
  • it is kinda nice to see the difference. maybe they could just make it a different colour instead – Ewan Apr 14 '17 at 04: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 04:10
  • unless you try to new one up – Ewan Apr 14 '17 at 04:11
  • Which you shouldn't be doing in a client anyway. Stop thinking it's ok to do it just because you can. – candied_orange Apr 14 '17 at 04:13
  • what about my complicated reflection scenario of super cleverness??!? you are sapping all the fun from programming – Ewan Apr 14 '17 at 04:15
  • Well now I need a few minutes to have a good long cry. – candied_orange Apr 14 '17 at 04:16
  • .CryAsyncRememberToAwait() otherwise I might get confused – Ewan Apr 14 '17 at 04:18
  • Ow ow ow ow! (but seriously we should be doing this in chat) – candied_orange Apr 14 '17 at 04:19
  • I'm gonna jump on the way too chatty bandwagon and say that @CandiedOrange is right here. I'm love C#, but I don't use the `IFoo` convention unless I'm working on a team. – RubberDuck Apr 14 '17 at 09:54
  • 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
  • Ow ow ow! The problem is people don't take the time to think of better names than MyClass. Use the name to tell me something I should care about. – candied_orange Apr 16 '17 at 19:57
  • top tip: append a number so you know what they class is for. eg MyClass_23 if it start getting too long you can use 'hexidecimal' oe MyClass_56b – Ewan Apr 17 '17 at 00:20
  • I should be able to tell that you're just messing with me. Unfortunately I have both graded student code and converted VB 6 code to .NET. My threshold of disbelief in bad code rivals the worst of [The Daily WTF](http://thedailywtf.com/). So please, I beg you, say it aint so? – candied_orange Apr 17 '17 at 22:46