Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDE0008 Use explicit type instead of 'var' #971

Closed
connywesth opened this issue Feb 9, 2020 · 5 comments
Closed

IDE0008 Use explicit type instead of 'var' #971

connywesth opened this issue Feb 9, 2020 · 5 comments

Comments

@connywesth
Copy link

connywesth commented Feb 9, 2020

image

Should it be better to always use specific typing and avoid the var-type at all. That is what is recomended by Microsoft itself.

I only found this issue 23 times in one file of the entire Mock-library, and that is only in the file MatchExpressionFixture.cs in the Mock.Tests(netcoreapp2.2).

This is found in Moq4 version 4.13.1.

@stakx
Copy link
Contributor

stakx commented Feb 9, 2020

[...] always use specific typing and avoid the var-type at all. That is what is recomended by Microsoft itself.

That's a rather questionable recommendation, IMHO. var definitely has its uses, e.g. to avoid repetition (T x = new T(...)), especially when that T is a TypeWithAnImpracticallyLongName, or in situations where the type simply isn't important for understanding.

I think it's fine to decide between var and explicitly named types on a case by case basis.

@connywesth
Copy link
Author

connywesth commented Feb 10, 2020

The var-declaration only occurs in 23 rows in one file only in the entire Mock-project.

It is of cource much clearer to use explicit typing. Microsft recommends explicit typing "when the type matters", wich I suppose it always does in this type of framework/library.

Maybe when you write a short program.cs with one Main-metod and maximum 20 rows of code you might say clear typing don't matter, but in most other cases type do matter , a lot.

I don't like sloppy typing, it becomes a mess when an application grows in size.

@stakx
Copy link
Contributor

stakx commented Feb 11, 2020

I remain unconvinced that Microsoft's recommendation is sound in each and every circumstance. Simple appeal to authority isn't a sufficient argument, and if they really wanted to categorically avoid var, perhaps they shouldn't have added that keyword to the language in the first place, right? 😉 By that, I mean to say that this recommendation obviously doesn't have the character of an eternal truth; it is simply someone's preference.

If Microsoft's goal were to nudge us into explicitly specifying types, we'd also have to do that in many different places, e.g. for lambda parameter lists: x => x + 1 would have to become (int x) => x + 1.

In all honesty, I don't see a problem here. Let's leave var usage as is, those 23 uses are not worth spending much time on discussing, reviewing PRs, etc.

However, thank you for caring about code quality, and for contributing to this project!

@stakx stakx closed this as completed Feb 11, 2020
@stakx
Copy link
Contributor

stakx commented Feb 11, 2020

(Btw. var is used elsewhere, too, not just in MatchExpressionFixture. Makes me wonder whether your code analysis settings are correct; and if so, why it only complains about that one file.)

@connywesth
Copy link
Author

Well, this is not my decisiton, I just reported something that anoys me in code. I follow the VisualStudio recommendations as far it's possible in my code. My life gets somewhat easier then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants