Move Action

I used to always name a list followed by the word List, but then I was asked by other coworkers not to. If you have:

List<GridPosition> validGridPositionList = new List<GridPosition>();

And then at some point the List is changed to a LinkedList, HashSet or something else, now your name is incorrect and you have to change the name of your variable. However if instead of List you just use the plural:

List<GridPosition> validGridPositions = new List<GridPosition>();

Now it can be changed without having to refactor the name (which believe me, will get missed :slight_smile: )
I’d also like to point out that Visual Studio & Rider both suggest to use implicitly typed lists like:

var validGridPositions = new List<GridPosition>(); (Rider)
List<GridPosition> validGridPositions = new(); (Visual Studio)

Total preference, but I see most people implicitly type their lists. (mostly because they’re lazy like me and don’t want to type it twice). But again, if you change from a list of strings to a list of floats, you have to change it in two places.

Thanks Code Monkey!

Julian

Naming is a matter of taste. I tend to mix and match, depending on the situation. For example, while I seldom use the List suffix, I often use the Dict suffix.

Implicit type is something that can be difficult for some. There is an inherent advantage to using var, when it comes to refactoring. Often times, you can change the underlying type in one spot and the var will implicitly change all of the derived variables for you. The double edge sword for this is readability. In fact, at the beginner level courses (this isn’t), in the past we’ve tended to avoid var except to mention it in passing because beginning students may have trouble inferring what the type is when looking at the code later.
I tend to use var in prototyping, and then when the code is more or less settling in, that point where you know “you know what, this is always going to be a List of Units”, then I’ll change it to List<Unit>. Does it change anything? No, just makes it a little easier for me to read six months from now when I read the code and try to remember what it does.

So there are two different sets of circumstances that determine which coding conventions you follow:

  1. your personal projects, it’s up to you, you are the captain of your code, you dictate the terms and determine the best practices for your code.
  2. You work for a company: You do whatever their coding style guidelines require. If they want to name all ints with a “WholeNumber” suffix, you name all its with a “WholeNumber” suffix. (Ok, that example is oddly specific and extreme, but coding by committee requires that everbody plays by the same rules).
3 Likes

For me, I did start by writing code like that, always using plural for lists.
But I was constantly having errors because of accidentally using the wrong name and the code was really difficult to read because the only difference between the list and the individual type was one tiny “s”
So at a certain point I just made it a personal rule to never use plurals and just end the name with the type instead.

Visually, for me, it is much much easier to see the difference between “validGridPosition” and “validGridPositionList”, as opposed to “validGridPosition” and “validGridPositions”

What you say about changing type and making the name incorrect is indeed a valid concern, but for me the solution is simple, if I change the type, I rename the variable.
Modern IDEs all have smart rename capabilities so it’s really simple to just change the name of all references.

And as for var, personally I dislike it for the same code readability reasons. I want my code to be as explicit as possible so I’m 100% sure I know what exactly I’m writing and what exactly the code is doing.
When you do var validGridPositionList = new List<GridPosition>(); then yes in that case adding var works, it’s pretty easy to tell what type it will be.
But when doing var validGridPositions = GetValidGridPositions(); what is the type that it returns? I have no clue, I have to mouse over the function to understand what the code is doing.
Whereas if I’m explicit in my types then I have no doubts over what type I’m working with, spending 0.1 seconds writing the type isn’t really something I’m concerned with, code is always read much more often then it is written.
So for me var usually results in less readable code and as a personal rule I pretty much never use it.

But as usual when it comes to naming conventions it’s very much a personal preference type of thing, the important thing is to be consistent so if you find that using plurals doesn’t cause you problems and using var makes it readable to you, then by all mean use it, just be consisten.

6 Likes

That makes sense! I really only meant to use var when the type is on the both the left and the right. I see no point in having the type on both sides. However, I always use the type on one side. So I wouldn’t do:

var value = “stringValue”;

I would only do:

string value = “stringValue”

However, I never do:

List<string> values = new List<string>();

Anyway, I just wanted to point out to others that when defining lists, we can be a bit more lazy and still be strongly typed and easy to see what type.

Thanks Code Monkey!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms