<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nick Rogers</title>
    <description>The latest articles on Forem by Nick Rogers (@snickroger).</description>
    <link>https://forem.com/snickroger</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F186533%2F98cac68c-37d8-4449-b9c5-fddcc93e8171.jpg</url>
      <title>Forem: Nick Rogers</title>
      <link>https://forem.com/snickroger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/snickroger"/>
    <language>en</language>
    <item>
      <title>Comment Why, Not What</title>
      <dc:creator>Nick Rogers</dc:creator>
      <pubDate>Fri, 10 Jul 2020 11:42:07 +0000</pubDate>
      <link>https://forem.com/dealeron/comment-why-not-what-22ll</link>
      <guid>https://forem.com/dealeron/comment-why-not-what-22ll</guid>
      <description>&lt;p&gt;I've been involved with interviews and the hiring process at DealerOn for a number of years. Part of this process is to administer a code test, where we give the candidate their choice of a few non-strenuous coding challenges, and they take a crack at solving the prompt (at their leisure) and sending us their code that they wrote. There are some patterns that we commonly notice on junior candidates fresh out of college, most notably in the comments in their code.&lt;/p&gt;

&lt;p&gt;Students often have it hammered into them by their computer science professors to leave lots of comments in their code, sometimes to the point of a student being penalized if the grader feels there aren't enough of them in his or her submitted work. The problem is, a high quantity of low-quality comments can often make the code more confusing than if there were none at all. &lt;/p&gt;

&lt;p&gt;Take a look at this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// This method gets the sales targets for &lt;/span&gt;
&lt;span class="c1"&gt;/// each day of the week from the database.&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;param name="employeeName"&amp;gt;The employee's name&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A Dictionary&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTargets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// create a new array&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt; 
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeeName&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;"Charlie"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// check if employee is Charlie&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// employee is not Charlie&lt;/span&gt;
    &lt;span class="c1"&gt;// add Monday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Tuesday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Wednesday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Thursday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Friday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// employee is Charlie&lt;/span&gt;
    &lt;span class="c1"&gt;// add Tuesday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Wednesday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Thursday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Friday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="c1"&gt;// add Friday to the array&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saturday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saturday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// return the array&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you spot all the problems with those comments? For starters, the variable we're adding the days of the week to is a Dictionary, not an array, and why is it called &lt;code&gt;employees&lt;/code&gt; if it's working with the days of the week? Also, comments like &lt;strong&gt;create a new array&lt;/strong&gt;, &lt;strong&gt;add to the array&lt;/strong&gt;, and &lt;strong&gt;return the array&lt;/strong&gt; are unnecessary because any programmer can look at the syntax of those lines and clearly understand what is happening, even if the language itself is unfamiliar. The main problem with this code is, &lt;strong&gt;these comments are answering "What?" when they should be answering "Why?"&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  But why?
&lt;/h2&gt;

&lt;p&gt;You probably found yourself saying "but why?" at several points reading the above code. The code has comments, but all they are doing is explaining what the code is doing at that moment; they do a poor job of explaining the things that actually need clarification. Imagine a new developer tasked with making a slight modification to this code. They may be struck with questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For what purpose are we retrieving this data?&lt;/li&gt;
&lt;li&gt;Shouldn't Saturday and Sunday always be included in the returned value?&lt;/li&gt;
&lt;li&gt;Why is Charlie a special case?&lt;/li&gt;
&lt;li&gt;What format must the employee's name be in for the parameter? Will it work if the employee's last name is given as well?&lt;/li&gt;
&lt;li&gt;What service will be using this data?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these questions are answerable with the code as it currently is, leaving our poor developer to scratch his or her head for a while and then ask for help. Let's rewrite this method to be cleaner and to answer the above questions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// This method gets the sales targets for a given employee for &lt;/span&gt;
&lt;span class="c1"&gt;/// each day of this employee's work week, formatted for displaying &lt;/span&gt;
&lt;span class="c1"&gt;/// on the company's sales dashboard.&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;param name="employeeFirstName"&amp;gt;The employee's first name, as recorded &lt;/span&gt;
&lt;span class="c1"&gt;/// in this company's LDAP server.&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A Dictionary mapping the employee's work days to their &lt;/span&gt;
&lt;span class="c1"&gt;/// sales goal for that individual day of the week.&amp;lt;/returns&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetDailySalesTargetsForEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;employeeFirstName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;salesTargets&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;normalSchedule&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Monday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Friday"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;modifiedSchedule&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Tuesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Wednesday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Thursday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Friday"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Saturday"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="nf"&gt;EmployeeWorksModifiedSchedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeeFirstName&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;normalSchedule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;salesTargets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeFirstName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;modifiedSchedule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;salesTargets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;GetSalesTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employeeFirstName&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;salesTargets&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can, of course, be condensed further by using a ternary expression or some LINQ. Regardless, this version of the method is much more readable, and a new developer would have a better idea of what's going on, saving everyone some time. It's amazing how much more clear the code can be with a modest refactor and by using more specific naming. This code answers most questions an otherwise unfamiliar developer would have, all without a single comment in the method body. &lt;/p&gt;

&lt;p&gt;Comments can become out of date due to inattentive developers. Did you notice in the first example, in addition to the array/Dictionary mistakes, there's a copy/paste error, probably by someone that copied the original method in order to support Saturdays, and forgot the change the final comment. This is a tame example, but there is a phenomenon that, when doing any rote task such as copying and pasting a line of code over and over, &lt;strong&gt;more errors are made on the final iteration than any other&lt;/strong&gt;. This is the so-called "&lt;a href="https://medium.com/@Code_Analysis/the-last-line-effect-7b1cb7f0d2a1"&gt;Last-Line Effect&lt;/a&gt;". Comments become invisible when you're in the code for too long, but it happens to the best of us. At DealerOn we have a pretty infamous method still in our codebase which has comments stating that the method checks a string for the word "checked" and returns a Boolean, when it actually checks a string for a different word entirely and returns 0 or 1. This code isn't referenced anymore, but it still exists as a cautionary tale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comment Where Necessary
&lt;/h2&gt;

&lt;p&gt;To be clear, I'm not saying there is no place for comments in modern development, nor do we at DealerOn judge code test submissions harshly for using too many comments. We suggest candidates do the code prompt like they would a school assignment, and if they are accustomed to verbose comments, that's perfectly fine. Speaking for myself however, if I'm unsure whether or not to leave a comment, I ask myself if what the code is doing is unclear, &lt;strong&gt;and if it can't be made more clear with a refactor&lt;/strong&gt;. That last part is important: if the code itself can be made easier to understand, do it!&lt;/p&gt;

&lt;p&gt;If a refactor isn't in scope for the unit of work, I use comments when something &lt;em&gt;looks&lt;/em&gt; counter-intuitive, like when a method needs to account for an edge-case but it's unclear why. They're mostly "Trust me, I know what I'm doing" comments. There's a lot to be said about &lt;strong&gt;self-documenting code&lt;/strong&gt;, code which is clear and easy to read, without resorting to method body comments.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Resolving Dependencies at Runtime in ASP.NET Core</title>
      <dc:creator>Nick Rogers</dc:creator>
      <pubDate>Fri, 01 Nov 2019 17:32:16 +0000</pubDate>
      <link>https://forem.com/dealeron/resolving-dependencies-at-runtime-in-asp-net-core-4k90</link>
      <guid>https://forem.com/dealeron/resolving-dependencies-at-runtime-in-asp-net-core-4k90</guid>
      <description>&lt;p&gt;ASP.NET Core supports &lt;a href="https://en.wikipedia.org/wiki/Dependency_injection"&gt;&lt;em&gt;dependency injection&lt;/em&gt;&lt;/a&gt; (DI), a great pattern for decoupling classes which would normally perform multiple functions inside the same region of the application. The dependency injection engine of ASP.NET Core, known as a &lt;em&gt;service container&lt;/em&gt;, is a functional, albeit basic, way to create a well-factored application with separated concerns. With little additional code, developers can easily select dependencies to add to the container when the conditions are known at application startup, such as whether the app is running from a Pre-Prod or a Production environment. &lt;/p&gt;

&lt;p&gt;But what if we want to use this cool thing on a condition that we'd only know at runtime, after the Startup code is long gone? For example, what if we wanted to use different implementations of an interface based on a user's type of account or the user's location? There are additional third-party service containers that augment Microsoft's stock service container with advanced functionality, and in fact, Microsoft's own docs suggest &lt;a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0#default-service-container-replacement"&gt;a number of solutions&lt;/a&gt; if theirs doesn't suit your needs. But come on, do you really want to have &lt;em&gt;two&lt;/em&gt; of these in your app? There's an easy way to resolve dependencies at runtime with our old friend, the factory pattern.&lt;/p&gt;




&lt;p&gt;Let's say we are writing a simple app to determine the weather for a location, determined by a postal code the user enters into a text box. One of the requirements is to support both United States ZIP codes and Canadian postal codes, which are formatted differently. Let's also say that, unfortunately, each location requires the use of a different API between the US and Canada, so we can't pick a single implementation at startup and register it. Not to fear, there is an overload on the &lt;code&gt;.Add()&lt;/code&gt; methods for the service container that accepts a &lt;code&gt;Func&amp;lt;IServiceProvider, TService&amp;gt;&lt;/code&gt; parameter. This means that instead of specifying a concrete implementation for the &lt;code&gt;TService&lt;/code&gt; type, we can instead inject a runtime function that takes any number of parameters and returns an object of type &lt;code&gt;TService&lt;/code&gt;! Here's our &lt;a href="https://gist.github.com/snickroger/637c6a352e412c3341565b201f5288d0"&gt;first crack&lt;/a&gt; at taking this approach:&lt;/p&gt;

&lt;p&gt;First up, we have our central service. The most important thing to take note of here is the &lt;code&gt;IWeatherService&lt;/code&gt; being injected into the class constructor on line 22.  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next we have the &lt;code&gt;WeatherService&lt;/code&gt; implementation, just wrapping out calls to &lt;code&gt;WeatherServiceApi&lt;/code&gt; and &lt;code&gt;WeatherServiceParser&lt;/code&gt; factory classes. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And finally, we have our central player, the &lt;code&gt;Program.cs&lt;/code&gt; where the &lt;code&gt;IWeatherServiceApi&lt;/code&gt; and &lt;code&gt;IWeatherServiceParser&lt;/code&gt; factories are configured with a singleton registration function on app start up. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;p&gt;When we run this app, a function taking a string as a parameter to determine which API to use is injected into the &lt;code&gt;IWeatherService&lt;/code&gt; implementation, and it runs the appropriate API based on the postal code that the user entered. This works fine, but there's a bit of a gotcha here. Notice in our injected function we are returning a new implementation each time the factory function is called. This is fine if it's only called a small number of times, but if it runs in a long loop or if the returned service has dependencies of its own that need to be injected, we could be in trouble.&lt;/p&gt;

&lt;p&gt;So what can we do? Well, did you notice that we also have access to the service container through the &lt;code&gt;service&lt;/code&gt; variable inside the function in the example above? What we can do is query the container for all services implementing a certain type and then pick the one appropriate for the runtime condition using some LINQ. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Using the service container in this way allows us to take full advantage of the power of dependency injection. Instead of returning a new object, the container returns a reference to the concrete implementation for the requested type, as defined at application startup. Clear and well thought-out strategies for dependency injection like this one can help improve reliability and testability for many apps, small and large.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;"&lt;a href="https://www.flickr.com/photos/relatedfluidpower/25711744061/"&gt;Factory&lt;/a&gt;" by &lt;a href="https://www.flickr.com/photos/relatedfluidpower/"&gt;Related Fluid Power&lt;/a&gt; is licensed under &lt;a href="https://creativecommons.org/licenses/by/2.0/"&gt;CC BY 2.0&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dependencyinjection</category>
      <category>patterns</category>
      <category>aspnetcore</category>
    </item>
  </channel>
</rss>
