{"id":812,"date":"2010-05-05T09:19:21","date_gmt":"2010-05-05T16:19:21","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=812"},"modified":"2016-07-14T15:08:34","modified_gmt":"2016-07-14T19:08:34","slug":"do-you-have-a-case-of-var-guilt","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/","title":{"rendered":"Do You Have a Case of var Guilt?"},"content":{"rendered":"<p>Ever since <em>var <\/em>was announced as a keyword for C# 3.0, I have had issues with it.\\n\\nI know that it is strongly typed, but something just felt wrong about it. \u00a0It felt like some kind of an abomination.\\n\\nPerhaps I just like to repeat myself = new Perhaps I just like to repeat myself.\\n\\nFinally, I started using the\u00a0<em>var<\/em> keyword\u00a0because I don&#8217;t know what the heck a LINQ query returns. \u00a0I just know that if I put var at my variable declaration before my LINQ query, it just works. \u00a0Then if I put var before my variable in my foreach loop, that just works too.\\n\\nReluctantly, I started using <em>var<\/em>\u00a0but every time I did I would start to feel guilty. \u00a0Like I did something wrong. \u00a0Like I was a bad person. \u00a0Like I was writing strongly typed VB code. \u00a0I felt dirty.\\n\\n<\/p>\n<h2>Be clean my sons! \u00a0Throw off your shackles!<\/h2>\n<p>\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/hands-in-shackles.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-818\" title=\"hands-in-shackles\" alt=\"\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/hands-in-shackles.jpg\" width=\"295\" height=\"310\" \/><\/a>\\n\\nI contemplated the issue for a good long while. \u00a0It has been months that this var keyword issue has been eating me, slowly leaking into my code. \u00a0<strong>Then, I had an epiphany!<\/strong>\\n\\n<em>var <\/em>is just an abstraction. \u00a0I don&#8217;t need to know the type of variables as long as the compiler does.\\n\\nThere is no need to feel guilty when using <em>var<\/em>. \u00a0It is preventing the reader of your code from focusing on the types of the variables and instead focusing on the purpose of the variables.\\n\\nI rewrote some code using the var keyword wherever possible to see if it decreased readability at all. (I took it to the extreme)\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/12ceb153d73c9c0c5a90a844b79e86fd.js\"><\/script>\\n\\nAdmittedly, it looks a little strange. \u00a0But honestly, is it any more difficult to read than if I had put all the types in there instead of var? \u00a0Does that actually give you any useful information that helps you understand this code? \u00a0As long as my variable names are good I would venture to say that the readability is improved because anything that adds information that is not important to the code is a distraction.\\n\\n<\/p>\n<h2>Bonus: decoupling<\/h2>\n<p>\\n\\nAs I was contemplating this issue, I realized something more than\u00a0aesthetics pushed me to the <em>var <\/em>side of the force. \u00a0<em>var<\/em> reduces coupling in my code when I use it.\\n\\nIf I call a method that returns some object in my code but I hold the reference to it with a variable I declared using <em>var<\/em>,<em> <\/em>if that method changes to return something else with similar usage, I won&#8217;t have to change the source code. \u00a0(I will have to recompile though.) In many instances the\u00a0<em>var <\/em>keyword\u00a0can serve as a bonus to slightly decouple your application in places where methods return concrete types. \u00a0Every little bit helps.\\n\\nAnything that reduces your dependency on concrete types and makes your code more flexible while still catching issues at compile time instead of runtime is good in my book.\\n\\n<\/p>\n<h2>Hinging on the IDE<\/h2>\n<p>\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-816 alignnone\" title=\"var\" alt=\"\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png\" width=\"392\" height=\"291\" srcset=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png 392w, https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var-300x223.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/a>\\n\\nThere is one caveat here. \u00a0This all depends on the IDE. \u00a0You have to have intellisense and auto-complete, otherwise this whole thing falls apart. \u00a0I don&#8217;t need to know the underlying type because I can just hit &#8220;.&#8221; on my keyboard and Visual Studio tells me what my options are. \u00a0I can go back and figure out what the type is by just hovering over the <em>var <\/em>declaration in Visual Studio.\\n\\nWithout those abilities the\u00a0<em>var <\/em>keyword\u00a0would not be as good. \u00a0Without those capabilities you would have to go back to where the variable was declared and possibly trace up a method call to figure out the return type to know what type the <em>var <\/em>variable will be initialized to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever since var was announced as a keyword for C# 3.0, I have had issues with it.\\n\\nI know that it is strongly typed, but something just felt wrong about it. \u00a0It felt like some kind of an abomination.\\n\\nPerhaps I just like to repeat myself = new Perhaps I just like to repeat myself.\\n\\nFinally, I started&#8230;<\/p>\n","protected":false},"author":2,"featured_media":816,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[2185,2426],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-practices","category-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Do You Have a Case of var keyword Guilt?<\/title>\n<meta name=\"description\" content=\"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Do You Have a Case of var keyword Guilt?\" \/>\n<meta property=\"og:description\" content=\"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-05-05T16:19:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-07-14T19:08:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png\" \/>\n\t<meta property=\"og:image:width\" content=\"392\" \/>\n\t<meta property=\"og:image:height\" content=\"291\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Do You Have a Case of var Guilt?\",\"datePublished\":\"2010-05-05T16:19:21+00:00\",\"dateModified\":\"2016-07-14T19:08:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/\"},\"wordCount\":637,\"commentCount\":9,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/05\\\/var.png\",\"articleSection\":[\"Best Practices\",\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/\",\"name\":\"Do You Have a Case of var keyword Guilt?\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/05\\\/var.png\",\"datePublished\":\"2010-05-05T16:19:21+00:00\",\"dateModified\":\"2016-07-14T19:08:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/05\\\/var.png\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/05\\\/var.png\",\"width\":392,\"height\":291},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/do-you-have-a-case-of-var-guilt\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Do You Have a Case of var Guilt?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Do You Have a Case of var keyword Guilt?","description":"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/","og_locale":"en_US","og_type":"article","og_title":"Do You Have a Case of var keyword Guilt?","og_description":"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.","og_url":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/","og_site_name":"Simple Programmer","article_published_time":"2010-05-05T16:19:21+00:00","article_modified_time":"2016-07-14T19:08:34+00:00","og_image":[{"width":392,"height":291,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png","type":"image\/png"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Do You Have a Case of var Guilt?","datePublished":"2010-05-05T16:19:21+00:00","dateModified":"2016-07-14T19:08:34+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/"},"wordCount":637,"commentCount":9,"image":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png","articleSection":["Best Practices","C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/","url":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/","name":"Do You Have a Case of var keyword Guilt?","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png","datePublished":"2010-05-05T16:19:21+00:00","dateModified":"2016-07-14T19:08:34+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"An explanation of how the var keyword is an abstraction that makes code more readable and slightly reduces decoupling where methods return concrete types.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/05\/var.png","width":392,"height":291},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/do-you-have-a-case-of-var-guilt\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Do You Have a Case of var Guilt?"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/816"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}