tag:blogger.com,1999:blog-4439386706617871322.post3724570450969566534..comments2022-08-05T09:35:18.913+03:00Comments on ReSharper Horizons: Varification -- Using Implicitly Typed LocalsIlya Ryzhenkovhttp://www.blogger.com/profile/14966746474791511643noreply@blogger.comBlogger52125tag:blogger.com,1999:blog-4439386706617871322.post-11586616745062801462018-03-21T11:35:38.186+03:002018-03-21T11:35:38.186+03:00I very much disagree with this. If this is so then...I very much disagree with this. If this is so then C# should decide about it NOT some tool added on top. C# is saying why var is there and the circumstances when it should be used. Being smart over the language specification is never a good idea.<br />var list = new List() might have some sense especially when declaration type is very long, but dragging it into using var just about everywhere is something I totally disagree. In order to read the type you have to dig into the code. That does not have any sense. If C# wanted that, they would tell you to do that.<br />The problem is that doing it indiscriminately can lead to a serious issues. Take this code<br /><br />var spaceWidth = count == 0 ? (decimal?)a : null;<br /><br />Intention is to have spaceWidth to be decimal? yet this is declared within the code Yes this is exactly what a developer would do if he would see decimal? spaceWidth = since var by Refactor has to be used everywhere.Aleksandarhttps://www.blogger.com/profile/15861656176193246763noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-71001401701077351822015-01-18T22:52:00.967+03:002015-01-18T22:52:00.967+03:00Seems that coding OO is not what it used to be...
...Seems that coding OO is not what it used to be...<br />Maybe an interface to the developer brain to avoid all typing? And linked to the user, no more coding and compiling... Telepathy... :)<br />Now that was unnecessary, I apologisze, so should MS for trying to attract all the web masters that know some javascript, are unable to think in abstractions and to whom writing(literally)/reading code (my precious) is too hard.<br />The problem with giving kids candy is that unless you hide the jar and feed them candy yourself, they don't know when to stop...<br />And yes, I do use var! sensibly, I hope... when I don't know the name of the type and then hoover over the var to get the proper type :D... great tool! (also on using()...)<br />But hey! Don't let me stop you from coding as you feel like it. If there was only one way of coding and it was the right one, we would not have this conversation at all. And testers do need work:)Jose Lealhttps://www.blogger.com/profile/04634111235620004447noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-40058262407699291792013-11-15T19:00:42.953+04:002013-11-15T19:00:42.953+04:00There's good arguments on both sides - however...There's good arguments on both sides - however using var does have the benefit, not listed, of making refactorings change less files.<br /><br />If I do<br />var myStuff = StuffFactory.Get();<br /><br />and then later change the return type of StuffFactory from Stuff to IStuff, the calling code never changes.Mathiesonhttps://www.blogger.com/profile/08384206018238144948noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-82338141645785329122012-02-14T20:58:39.183+04:002012-02-14T20:58:39.183+04:00"Lazy"?
"Poisoning the world"..."Lazy"?<br /><br />"Poisoning the world"?<br /><br />Should we also get off of your lawn?<br /><br />Anyone that thinks this is about saving typing doesn't really understand the discussion.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-89703076220722826032011-10-20T09:16:12.844+04:002011-10-20T09:16:12.844+04:00we are talking about "easy to read". Why...we are talking about "easy to read". Why do we discuss it only for declaration line?<br /><br />var cat = shop.GetAnimal();<br /><br />do I care in this line about type? NO. Because I have absolutely beauty variable name. But it's not good example!<br /><br />Good example to show that we don't care about type at all and focus on our habit to see type on left side... it's way how we using this variable:<br /><br />var cat = shop.GetAnimal();<br />...<br />cat.Meow();<br /><br />Do I really believe that during reading cat.Meow() I care about type? Why do I need type during the reading? <br /><br />"Readiness" is defined not by initialization of the variable, it's defined by using variable.<br /><br />If you eager about using concrete type you MUST write like this:<br /><br />Cat cat = shop.GetAnimal(); (1)<br />...<br />(Cat)cat.Meow(); (2) <--- BE HONEST! :)<br /><br />Here (2) is a type as you like :D Please be honest, it's not logical to use type in (1) and avoid in (2).<br /><br />My conclusion, we want to see code without var due to our habits, rather than to make code readable.Anonymoushttps://www.blogger.com/profile/08243544097137456823noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-29837704643128018282011-09-16T00:47:25.227+04:002011-09-16T00:47:25.227+04:00Stop poisoning the world with this nonsense, on to...Stop poisoning the world with this nonsense, on top of making code less readable, using var everywhere is just plain LAZY - don't like to type - then find another job because I am sick of walking into gig's where people seem to think this crap is the way to go.<br /><br />Just because you can drive a car with your feet doesn't mean you should.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-59401013015833171962011-05-05T01:59:26.003+04:002011-05-05T01:59:26.003+04:00To me, anonymous variables is a retrograde step to...To me, anonymous variables is a retrograde step to object type Variant, when it became so confusing as to what the object contained, Hungarian notation was the preferred method of identifying type, so instead of<br /><br />Int32 count;<br /><br />You would instead have <br /><br />var iCount;<br /><br />It also leads to numerous coding errors where there are now assumptions about what type is being assigned, where you were expecting one type and got another. Explicit casting prevents invalid runtime assignments, immediately in the IDE. Instead, you would have to debug trace through, to realize the point of failure, was assigning your Variant the wrong object 20 steps before an exception was thrown.<br /><br />No. there's a good reason for explicit types and nothing more than lazy coding for anonymous types (Variants)A Boothhttps://www.blogger.com/profile/07254222095322859961noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-46765580379430526282010-10-28T19:42:23.316+04:002010-10-28T19:42:23.316+04:00@Stryder:
"Good thing that this is a discuss...@Stryder:<br /><br />"Good thing that this is a discussion about var in C# then."<br /><br />C# does not exist in a vacuum.<br /><br />What about the rest of my comment?Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-66865743408062480442010-10-27T19:24:31.955+04:002010-10-27T19:24:31.955+04:00"Anyone that dislikes var is going to be comp..."Anyone that dislikes var is going to be completely unable to approach F#. "<br /><br />Good thing that this is a discussion about var in C# then.Strydernoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-26722034494323620102010-08-09T06:05:12.693+04:002010-08-09T06:05:12.693+04:00"C# documentation generally uses var only whe..."C# documentation generally uses var only when it is required"<br />This is a bit obsolete approach intended for people who are completely new and unaware of ideas of functional programming, type inference, etc. After reading some books on F#, Scala, etc. (one of my favorites is <a href="http://www.functional-programming.net/" rel="nofollow">http://www.functional-programming.net/</a>) it becomes clear when to use and when not to use var. With constructors and type casting operators it doesn't make sense to use explicit types - just makes code less readable. Var also makes sense to use in all cases when type is clear from a context, when type is automatically generated and/or very complicated.Владимирhttps://www.blogger.com/profile/00331194583266302228noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-66605579941336277192010-08-05T17:46:53.029+04:002010-08-05T17:46:53.029+04:00var should be used for all complex types, and subj...var should be used for all complex types, and subjectively used for primitive types.<br /><br />The quote that Alexander posted “Overuse of var can make source code less readable for others. It is recommended to use var only when it is necessary, that is, when the variable will be used to store an anonymous type or a collection of anonymous types.” appears to have been redacted and replaced by a statement that is vastly different. "However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required." Can be found on http://msdn.microsoft.com/en-us/library/bb384061.aspx Even this argument is mostly dubious but does make sense for a documentation site like the MSDN.<br /><br />If the var makes your code less readable it is YOUR FAULT for BAD NAMING of variables.Chris Marisichttp://www.marisic.netnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-40598270027009123492009-12-11T23:12:45.424+03:002009-12-11T23:12:45.424+03:00Appeals to popularity aside, I assume anyone with ...Appeals to popularity aside, I assume anyone with a problem with var explicitly casts all of their expressions and method params to the appropriate data type, too, so they can clearly see the type. It'd be critical since they've all memorized the entire (current) API, rather than typing a simple dot when they need to access a type member and shopping for it with IntelliSense.<br /><br />Anyone that dislikes var is going to be completely unable to approach F#.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-24486968206897426652009-12-11T12:05:39.028+03:002009-12-11T12:05:39.028+03:00I hardly ever agree with MS on anything related to...I hardly ever agree with MS on anything related to development, ... but I have to agree with them on the overuse of var. Why must I hover over variables? I use a laptop most times without a mouse. It's easier for me to read IInterface interface = api.apiMethod() than var interface = api.apiMethod() and judging by the debate all over the net, I'm far from being the only one who prefers it that way.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-7446954007356025882009-12-10T20:53:18.483+03:002009-12-10T20:53:18.483+03:00We do a lot of code review, it is required. I als...We do a lot of code review, it is required. I also work on a lot of code that is not my own.<br /><br />To me, I find the use of "var" makes code <b>much</b> more readable and reviewable. I can focus on the behavior of the code better.<br /><br />I use it just about everywhere.<br /><br />I personally think the argument of it obfuscating the code doesn't really hold.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-51614895458304835242009-12-07T04:26:29.423+03:002009-12-07T04:26:29.423+03:00I vote for Microsoft recomendation.
"var&quo...I vote for Microsoft recomendation.<br /><br />"var" makes code worse readable.aDphttps://www.blogger.com/profile/10060342037970346674noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-41826170751417670482009-03-19T00:16:00.000+03:002009-03-19T00:16:00.000+03:00"How do you know if the message is an IMesssage, M...<I>"How do you know if the message is an IMesssage, Message of even just a string?"</I><BR/><BR/>You hover over the variable name. But you probably don't even need to do that, unless you have memorized the entire API/library, which would be a waste of time. In reality, you type the variable name, then a dot, then you shop for the property or method you need, just like everything else.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-18133599248016290792009-03-18T21:15:00.000+03:002009-03-18T21:15:00.000+03:00Ok, let say that you have something like this.var ...Ok, let say that you have something like this.<BR/><BR/>var message = Inbox.GetMessage();<BR/><BR/>How do you know if the message is an IMesssage, Message of even just a string?<BR/><BR/>You say that I should rename my method....do I have to call it Inbox.GetIMessage() or even worst...Inbox.GetStringMessage()???<BR/><BR/>And by the way, maybe I dno't even have access to this code..maybe iI' using a dll from another provider. You should NEVER change your coding convention because of that.<BR/><BR/>Naming all your method depending on the return type is totally stupid. That's exactly why Microsoft says to not overuse the var keyword. I agree that var should (could) be used when declaring long named type objects like in this example.<BR/><BR/>//this is long for nothing<BR/>MessageProvider messageProvider = new MessageProvider();<BR/><BR/>//this is ok<BR/>var messageProvider = new MessageProvider();<BR/><BR/>It should also be used with LINQ. But THATS IT! Don't use it with method. Don't use it inside loops. Stop using var just because you can. If you don't like strongly typed language, go with Ruby or Python and stop blogging about C#.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-25297267660602553132008-09-29T18:44:00.000+04:002008-09-29T18:44:00.000+04:00"I guess you've never heard of code reviews."Sure,..."I guess you've never heard of code reviews."<BR/><BR/>Sure, we do them too, just not on stone tablets.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-83307684790327917352008-09-29T06:12:00.000+04:002008-09-29T06:12:00.000+04:00"If that's a primary design concern, I worry about..."If that's a primary design concern, I worry about your development process."<BR/><BR/>I guess you've never heard of code reviews.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-24479904084316550412008-09-17T23:27:00.000+04:002008-09-17T23:27:00.000+04:00"What if you're reading code on paper, on a web pa..."What if you're reading code on paper, on a web page, pdf or plain text editor?"<BR/><BR/>If that's a primary design concern, I worry about your development process.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-6727520417123849292008-09-15T08:20:00.000+04:002008-09-15T08:20:00.000+04:00"Yet again, I have to ask why people are scrolling..."Yet again, I have to ask why people are scrolling to the point of variable declaration to determine the type, rather than just hovering over the variable."<BR/><BR/>That's supported in Visual Studio only. What if you're reading code on paper, on a web page, pdf or plain text editor?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-92080104940622983222008-07-08T18:57:00.000+04:002008-07-08T18:57:00.000+04:00"Variable type declaration is not equal to variabl..."Variable type declaration is not equal to variable instantiation."<BR/><BR/>If this were true, the compiler would be unable to infer the type.<BR/><BR/>"I've fought far to many battles in classic ASP and vbscript where the type I thought was going into the variable wasn't the type I ended up with."<BR/><BR/>Comparing a dynamic language with a strongly-typed one isn't a great way to develop best practices.<BR/><BR/>Yet again, I have to ask why people are scrolling to the point of variable declaration to determine the type, rather than just hovering over the variable.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-39312136228548675812008-07-07T21:15:00.000+04:002008-07-07T21:15:00.000+04:00I've turned off this recommendation and am telling...I've turned off this recommendation and am telling my developers to do the same. Variable type declaration is not equal to variable instantiation. Therefore, you should only use var when you must. I've fought far to many battles in classic ASP and vbscript where the type I thought was going into the variable wasn't the type I ended up with.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-64121153976506032572008-05-18T01:17:00.000+04:002008-05-18T01:17:00.000+04:00Seriously, why are you navigating back to the vari...Seriously, why are you navigating back to the variable definition to determine the type of variable?<BR/><BR/>No one seems willing or able to answer this critical question, which is the entire basis of the "type name adds meaning" argument.Brianhttps://www.blogger.com/profile/01186394530871885873noreply@blogger.comtag:blogger.com,1999:blog-4439386706617871322.post-87279813982638920942008-05-17T12:35:00.000+04:002008-05-17T12:35:00.000+04:00From the points made in the main article it seems ...From the points made in the main article it seems this type of refactoring is unique amongst all the great recommendations that resharper makes. This rule makes your code worse until you can manually refactor or rename existing code to add in some meaning that has been removed. <BR/><BR/>Reshaper is a great tool because it simply lets me refactor code to be better with a few mouse clicks. I don't think resharper would be as great if it had a bunch of rules that only took you halfway to the goal.<BR/><BR/>I think the decision to keep this feature is a philosophical decision for the Resharper dev team. Are these halfway rules what you want to include into your otherwise concise and complete rule set.<BR/><BR/>Personally i don't like this rule. If a member of my team used a "var" where it wasn't necessary i would ask them to change it. I would actually like a reverse rule for this. "Don't use unnecessary implicitly typed locals"Anonymousnoreply@blogger.com