1

Recently, I had a debate with one of my friends on using type inference in C# (var keyword). His argument was that we should stick to using the explicit type names because "even with the meaningful variable names, explicit type names enhance readability of the code".

I do agree that it enhances readability but any modern IDE (like Visual Studio and Rider in this case) comes with type hints which show the type names of the type inferred variables which makes no difference between using explicit type names and implicitly typed variables from reader's point of view unless he is using something like Notepad or hardcopies of the code.

But his counterargument was that we should not depend on IDE/Text editor features when choosing a coding style because "We code for the humans to read, so we should not depend on one or two "tools we are using" when deciding our coding styles and conventions."

Is his point correct? I do not have a problem between choosing implicit or explicit typing or maintaining consistency of our coding practices, but not considering the "tools" we use when deciding the coding styles sounds too much.

6
  • 2
    This is like saying you shouldn't keep pet fish because they'll die if their oxygen pump malfunctions. Disaster preparedness is fine, but you shouldn't subjugate your normal way of life to it. Commented Aug 19 at 13:57
  • 6
    If you do pull requests, your diff viewer (eg in Github or Azure DevOps) probably doesn't have type hinting.
    – Andrew
    Commented Aug 19 at 15:53
  • 1
    Does your team write automated tests around the code? If so, then that's your readability and maintainability taken care of (and if not, then you have a problem which cannot be solved by coding style). Debates like this are among the worst kind of Bikeshedding and a complete waste of time. The IDE and the compiler (via Roslyn Analysers and VS auto-format) can enforce a style via .editorconfig. Any time wasted beyond that in some form of debate is better spent ensuring the tooling is taking care of it, and also writing tests. Commented Aug 19 at 18:24
  • 1
    @BenCottrell: is is possible to configure VS so it can distinguish between good and bad usage of var? Where do I get this, I will install it immediately.
    – Doc Brown
    Commented Aug 19 at 19:07
  • 1
    We've been slowly removing the use of var in our code base. We use VS2022 and yeah, you get the hints, but that slows things down when reading a code base, especially if it's new to you (I've been working on this code for 3 years and there are still areas I've never looked at). Commented Aug 20 at 10:01

3 Answers 3

7

"Type hints" are a very weak argument for "var", because they appear only when one places the mouse cursor over the var keyword. But when reading code, any professional developer processes whole lines and sections of code at once, without hovering the cursor from one var to another to find out the type (and remember it when the mouse is moved away). Hence I disagree with your idea that it makes no difference ... from reader's point of view - usage or non-usage of var has a significant influence on the readability, even in a powerful IDE like Visual Studio.

Does that mean one should not use var, like your friend suggested? Well, this debate has already taken place years ago, on this site and elsewhere. The gross consensus is, there are several cases where usage of the "var" keyword does not make the code less readable, and several cases where it can actually enhance readability (of course, wrong usage of var can also reduce readability, that is also true).

Look into older Q&A threads about this topic, like this one, or this similar Q&A about C++ and the auto keyword. None of the arguments in the accepted answers (and several other answers) mentions the IDE's capabilities as an argument for var. So if you are going to argue for usage of "var", there are way better arguments than the availability of type hints in the IDE.

0

They might have a point if your code is a whole bunch of:

var i = 1.0m

but:

var docs = docRepo.GetDocuments();

vs

IEnumerable<Document> docs = docRepo.GetDocuments();

Is questionable.

Also consider you are actually programming in notepad, do you want to type out your long class names without intellisense? or keep it short with var? What kind of mad max programming future where you can't trust the machines are we hedging against?

1
  • In your second example, I don’t really care what the type of docs is. I write code to iterate through docs and it iterates goving me the kind of thing that GetDocuments() returns. That’s all I care about.
    – gnasher729
    Commented Aug 19 at 19:02
0

Coding style should reflect the actual usage by people on the team

If everyone on your team uses Visual Studio, then it makes sense to select a coding style that makes things easier for Visual Studio users and doesn't duplicate information which Visual Studio already provides.

Your team's practices are more diverse than you think they are

That said, your team probably does not always use Visual Studio exclusively, and even if you are right now, you probably will not be in the future.

Chances are, you are using some kind of web-based source control service like GitHub which handles pull requests through a web interface, and that web interface is not going to have all of Visual Studio's context. (While Visual Studio does have pull request functionality, I've found that context lookups often don't work when reviewing PRs because it loads the diffs as a "Miscellaneous File" and not part of the solution.) Even outside of PRs, there will be times when someone wants to look up how something is done without having their IDE open, and will use a web interface's source code browser for it if such is available.

Additionally, if you have CI/CD then the automated builds will sometimes produce build errors. The emails and other messages produced by such a system will be read in an email client, not in Visual Studio.

A good coding style should consider all of the tools and processes during which people might have to read your code. Unless your team is exceptionally invested in a particular tool, this usually means that the plain-text of your code should be as readable as possible, because that's only thing that every tool will have in common.

This does not mean you should not var

As mentioned by other answers, var often improves the readability of a piece of code. Using var is a statement that the type of a particular variable doesn't matter - and if your code is well architected (not tightly coupled, doesn't expose concretions you shouldn't be depending on, etc.), those statements of intent can be accurate.

1
  • 1
    Everyone in my team uses Visual Studio. There are a few influences on our coding style, I am sure (default indentation of 4 characters, documentation comments, no fear against using long function + variable names etc.) But preferring "var" over explicit types was definitely not among these points. Quite the opposite: if one is a lazy typer, they can enter var and then use VS to replace that keyword by the explicit type - before the code is saved and put under version control.
    – Doc Brown
    Commented Aug 20 at 12:35

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