try-catch-FAIL - Please stop over-using the var keywordFailure is inevitablehttp://trycatchfail.com/blog/
http://www.rssboard.org/rss-specificationBlogEngine.NET 1.6.1.0en-UShttp://trycatchfail.com/blog/opml.axdhttp://www.dotnetblogengine.net/syndication.axdMatt Honeycutttry-catch-FAIL0.0000000.000000Will on Please stop over-using the var keywordI agree, for the most part. <br /><br />But, I think there's still some cases for using var when assigning a return value from some methods. <br /><br />For instance: <br /><br />var product = model.GetSingleProduct(1); <br />var products = model.GetAllMatchingProducts(p =&gt; p.WidgetType = &quot;foo&quot;); <br /><br /><br />...both are reasonably obvious, even when you've scrolled past the declaration. <br /><br />As a general rule though, I try to avoid using var except when doing so hurts readability (as per your complex generics example), or when I'm using anonymous types. <br /><br />http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_e29fd849-43e5-4243-8aaa-2172ba8eaedf
Willhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_e29fd849-43e5-4243-8aaa-2172ba8eaedfWed, 21 Oct 2009 18:37:56 -1300Willhttp://trycatchfail.com/blog/pingback.axdhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_e29fd849-43e5-4243-8aaa-2172ba8eaedfhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_e29fd849-43e5-4243-8aaa-2172ba8eaedf#commenthttp://trycatchfail.com/blog/syndication.axd?post=e29fd849-43e5-4243-8aaa-2172ba8eaedfStephen Redd on Please stop over-using the var keyword<br />My first rule is &quot;don't use var if you are aren't sure you should or not&quot;. Otherwise I advocate using var when:<br /><br />- The right side of the assignment makes it rather obvious what the type will be<br />- The type is anonymous (duh!)<br />- The type is clearly irrelevant to the code block where the variable is created.<br />- When you are working with code that was designed to be type agnostic using reflection or convention, which are techniques common within the MVC framework. <br /><br />On that last... basically if you are just setting up variables that will be passed into methods where the parameters are of &quot;System.Object&quot;, then you usually don't need to be concerned with the type in the calling code either. In those cases I find that using var is quite appropriate. <br /><br />By using var, you are also saying to other developers that may read or maintain your code, &quot;you don't need to concern yourself with the type here -- the type is not important&quot;. http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_ee3bc10d-adc1-4637-a67b-6866ec0b9d2d
Stephen Reddhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_ee3bc10d-adc1-4637-a67b-6866ec0b9d2dWed, 21 Oct 2009 19:40:59 -1300Stephen Reddhttp://trycatchfail.com/blog/pingback.axdhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_ee3bc10d-adc1-4637-a67b-6866ec0b9d2dhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_ee3bc10d-adc1-4637-a67b-6866ec0b9d2d#commenthttp://trycatchfail.com/blog/syndication.axd?post=ee3bc10d-adc1-4637-a67b-6866ec0b9d2dbhjg on Please stop over-using the var keywordit is exactly as you said. fortunately r# provides means to convert between the two. unfortunately it takes almost as long to wait for the r# light bulb to appear as it would take me to substitute the type manually.http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_a5d72eee-7993-494a-a4c5-0cd9b8e7b917
bhjghttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_a5d72eee-7993-494a-a4c5-0cd9b8e7b917Thu, 22 Oct 2009 04:08:48 -1300bhjghttp://trycatchfail.com/blog/pingback.axdhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_a5d72eee-7993-494a-a4c5-0cd9b8e7b917http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_a5d72eee-7993-494a-a4c5-0cd9b8e7b917#commenthttp://trycatchfail.com/blog/syndication.axd?post=a5d72eee-7993-494a-a4c5-0cd9b8e7b917Thomas Eyde on Please stop over-using the var keywordIt seems to be we are in agreement that the use of var is inappropriate when the type is important. I prefer to use var whenever I can, that force me to use meaningful names in addition to leave out a lot of noise. <br /><br />The type is important when var evaluates to object or the wrong type, which it does in foreach loops over untyped collections. Or when it's meaningless, like declaring uninitiated variables.<br /><br />Other than that, when is the type important to us, the readers?http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_36720ebe-a953-44b6-9eec-b8f0c71ca8b0
Thomas Eydehttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_36720ebe-a953-44b6-9eec-b8f0c71ca8b0Sat, 24 Oct 2009 04:30:58 -1300Thomas Eydehttp://trycatchfail.com/blog/pingback.axdhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_36720ebe-a953-44b6-9eec-b8f0c71ca8b0http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_36720ebe-a953-44b6-9eec-b8f0c71ca8b0#commenthttp://trycatchfail.com/blog/syndication.axd?post=36720ebe-a953-44b6-9eec-b8f0c71ca8b0Matt on Please stop over-using the var keyword@Thomas,<br /><br />I still think the type is often important, even with very readable code. Take this for example:<br /><br />var productView = repository.GetProductAndManufacturer();<br /><br />While I can be pretty sure that whatever &quot;productView&quot; is likely contains information about a product and manufacturer, it's still not very informative. If I know the type (and am familiar with the type), then I know exactly what productView contains. http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_cfa33ee8-51cc-49f0-8263-47641be0c023
Matthttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_cfa33ee8-51cc-49f0-8263-47641be0c023Sat, 24 Oct 2009 10:14:51 -1300Matthttp://trycatchfail.com/blog/pingback.axdhttp://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_cfa33ee8-51cc-49f0-8263-47641be0c023http://trycatchfail.com/blog/post/Please-stop-over-using-the-var-keyword.aspx#id_cfa33ee8-51cc-49f0-8263-47641be0c023#commenthttp://trycatchfail.com/blog/syndication.axd?post=cfa33ee8-51cc-49f0-8263-47641be0c023