-
Notifications
You must be signed in to change notification settings - Fork 272
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
Rephrased the use of var #250
Conversation
Are you really sure you want to advise against using tooling for I'm not at all looking forward to playing police officer and adding review feedback on Modern IDEs show the inferred type in a hint anyway on their interpretation of non-obvious, which makes the chosen style even less important (does not apply for PRs of course). Furthermore, it prevents developers from always writing Of course, we all have our own personal preference on when I agree that the industry has evolved since the introduction of |
I was expecting you to respond ;-) But honestly, I don't care about the tooling here. Just as I explain in my masterclasses on writing maintainable code, the point is to make sure the intend of the code is clear to anybody who needs to understand what your code is doing and why. Using |
The industry seems to disagree with that: implicit/explicit typing is a stylistic thing (examples here, here and here).
That is achieved by choosing proper variable names. If the type is important to understand the code (depending on the context, usually it is not), the name should reflect that (for example: Popular languages that rely on static typing which were recently designed (rust, swift) don't even have explicit type syntax. Type inference in compilers has evolved now. Even modern C++ and Java now have type inference. Spelling out types is becoming less and less common, which is something we (old-school devs) probably need to get used to (this is unrelated to dynamic languages such as JavaScript). Note that I'm not recommending "use var everywhere" or "never use var", just to pick something that doesn't fight against the system. I once met someone that wanted to forbid lambda expressions and require to use anonymous functions, because then the types are visible. Following your reasoning you'd have to agree with that, right? |
On the first point: From a compiler perspective, there's syntax (what the user typed) and semantic meaning. In that sense, var usage is stylistic because it results in the same semantic meaning. However that distinction is irrelevant here, because our audience are humans. And for humans, style does matter. |
It doesn't matter. Since the original discussion started in #203, almost two years ago, I've been trying to push myself in the direction of |
I agree with bkoelman obviously. And our whole team switched to var for all our new code, and we haven't looked back since. The guidelines I proved in the original PR proved to be accurate and desirable, and as I mentioned in the original PR, it is also really not just a style thing. Maintainability greatly improves when the returning type is an implementation detail. I have countless examples for this already from the last two years, where we replaced a dto with a state object, or an interface, etc. A much nicer way to deal with this would have been to collect the examples where you felt you did need to use an explicit variable, because then the guideline could have been more specific and informative on what situations would be best suitable for what instance. So I am going to continue to disagree here and simply state that it should be "use var unless there is no other option (like if you need to force a certain interface, then it makes sense, and then var being default is useful because it is immediately clear also that you needed to force it)." |
@niwrA I suspect you misunderstand my point of view here. Because I agree with @dennisdoomen that My example on With "fight against the system" I meant to not have a rule that goes against the ability to automate |
After discussion elsewhere, the merge of this PR was reverted and is superseded by #252. |
Replaces #1520