Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
natefinch committed Nov 18, 2017
1 parent ef628a5 commit 285094b
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 8 deletions.
47 changes: 45 additions & 2 deletions 2017/11/comments/index.html
Expand Up @@ -181,7 +181,7 @@ <h2 id="code-sucks">Code Sucks</h2>
<p>But of course, what is clean and obvious and well-written to you, today, while
the entire project and problem space are fully loaded in your brain&hellip; might not
be obvious to you, six months from now, or to the poor schmuck that has to debug
your code with their manager breathing down their neck beacuse the CTO just ran
your code with their manager breathing down their neck because the CTO just ran
into a critical bug in prod.</p>

<p>Learning to look at a piece of code that you understand, and trying to figure out
Expand All @@ -203,7 +203,7 @@ <h2 id="avoiding-comments-often-makes-your-code-worse">Avoiding Comments Often M
because you have to make your code clearer to compensate. I call BS on this as
well, because I don&rsquo;t think anyone is realistically writing sub-par code and
then excusing it by slapping a comment on it (aside from <code>// TODO: this is a
temporary hack, I'll fix it later</code>). We all write the best code we know howm,
temporary hack, I'll fix it later</code>). We all write the best code we know how,
given the various external constraints (usually time).</p>

<p>The problem with refactoring your code to avoid needing comments is that
Expand Down Expand Up @@ -320,6 +320,49 @@ <h2 id="any-comment-is-better-than-no-comment">Any comment is better than no com
<em>did</em> still round the duration down to the nearest second, so the comment was
not incorrect per se, it was just misleading.</p>

<h2 id="why">Why?</h2>

<p>The most important comments are the <em>why</em> comments. Why is the code doing what
it&rsquo;s doing? Why must the ID be less than 24 characters? Why are we hiding this
option on Linux? etc. The reason these are important is that you can&rsquo;t figure
out the why by looking at the code. They document lessons learned by the devs,
outside constraints imposed by the business, other systems, etc. These comments
are invaluable, and almost impossible to capture in other ways (e.g. function
names should document what the function does, not why).</p>

<p>Comments that document <em>what</em> the code is doing are less useful, because you can
generally always figure out what the code is doing, given enough time and
effort. The code tells you what it is doing, by definition. Which is not to
say that you should never write <em>what</em> comments. Definitely strive to write the
clearest code you can, but comments are free, so if you think someone might
misunderstand some code or otherwise have difficulty knowing what&rsquo;s going on,
throw in a comment. At least, it may save them a half hour of puzzling through
your code, at best it may save them from changing it or using it in incorrect
ways that cause bugs.</p>

<h2 id="tests">Tests</h2>

<p>Some people think that tests serve as documentation for functions. And, in a
way, this is true. But they&rsquo;re generally very low on my list of effective
documentation. Why? Well, because they have to be incredibly precise, and thus
they are verbose, and cover a narrow strip of functionality. Every test tests
exactly one specific input and one specific output. For anything other than the
most simple function, you probably need a bunch of code to set up the inputs and
construct the outputs.</p>

<p>For much of programming, it&rsquo;s easier to describe briefly what a function does
than to write code to test what it does. Often times my tests will be multiple
times as many lines of code as the function itself&hellip; whereas the doc comment on
it may only be a few sentences.</p>

<p>In addition, tests only explain the <em>what</em> of a function. What is it supposed to
do? They don&rsquo;t explain why, and why is often more important, as stated above.</p>

<p>You should definitely test your code, and tests can be useful in figuring out
the expected behavior of code in some edge cases&hellip; but if I have to read tests
to understand your code in general, then that&rsquo;s red flag that you really need to
write more/better comments.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I feel like the line between what&rsquo;s a useful comment and what&rsquo;s not is difficult
Expand Down
47 changes: 45 additions & 2 deletions blog/index.xml
Expand Up @@ -37,7 +37,7 @@ better, written better, it wouldn&amp;rsquo;t need that comment.&lt;/p&gt;
&lt;p&gt;But of course, what is clean and obvious and well-written to you, today, while
the entire project and problem space are fully loaded in your brain&amp;hellip; might not
be obvious to you, six months from now, or to the poor schmuck that has to debug
your code with their manager breathing down their neck beacuse the CTO just ran
your code with their manager breathing down their neck because the CTO just ran
into a critical bug in prod.&lt;/p&gt;

&lt;p&gt;Learning to look at a piece of code that you understand, and trying to figure out
Expand All @@ -59,7 +59,7 @@ thing. Sometimes it can be a huge help.&lt;/p&gt;
because you have to make your code clearer to compensate. I call BS on this as
well, because I don&amp;rsquo;t think anyone is realistically writing sub-par code and
then excusing it by slapping a comment on it (aside from &lt;code&gt;// TODO: this is a
temporary hack, I&#39;ll fix it later&lt;/code&gt;). We all write the best code we know howm,
temporary hack, I&#39;ll fix it later&lt;/code&gt;). We all write the best code we know how,
given the various external constraints (usually time).&lt;/p&gt;

&lt;p&gt;The problem with refactoring your code to avoid needing comments is that
Expand Down Expand Up @@ -176,6 +176,49 @@ updated the function to take a Duration rather than an Int. Interestingly, it
&lt;em&gt;did&lt;/em&gt; still round the duration down to the nearest second, so the comment was
not incorrect per se, it was just misleading.&lt;/p&gt;

&lt;h2 id=&#34;why&#34;&gt;Why?&lt;/h2&gt;

&lt;p&gt;The most important comments are the &lt;em&gt;why&lt;/em&gt; comments. Why is the code doing what
it&amp;rsquo;s doing? Why must the ID be less than 24 characters? Why are we hiding this
option on Linux? etc. The reason these are important is that you can&amp;rsquo;t figure
out the why by looking at the code. They document lessons learned by the devs,
outside constraints imposed by the business, other systems, etc. These comments
are invaluable, and almost impossible to capture in other ways (e.g. function
names should document what the function does, not why).&lt;/p&gt;

&lt;p&gt;Comments that document &lt;em&gt;what&lt;/em&gt; the code is doing are less useful, because you can
generally always figure out what the code is doing, given enough time and
effort. The code tells you what it is doing, by definition. Which is not to
say that you should never write &lt;em&gt;what&lt;/em&gt; comments. Definitely strive to write the
clearest code you can, but comments are free, so if you think someone might
misunderstand some code or otherwise have difficulty knowing what&amp;rsquo;s going on,
throw in a comment. At least, it may save them a half hour of puzzling through
your code, at best it may save them from changing it or using it in incorrect
ways that cause bugs.&lt;/p&gt;

&lt;h2 id=&#34;tests&#34;&gt;Tests&lt;/h2&gt;

&lt;p&gt;Some people think that tests serve as documentation for functions. And, in a
way, this is true. But they&amp;rsquo;re generally very low on my list of effective
documentation. Why? Well, because they have to be incredibly precise, and thus
they are verbose, and cover a narrow strip of functionality. Every test tests
exactly one specific input and one specific output. For anything other than the
most simple function, you probably need a bunch of code to set up the inputs and
construct the outputs.&lt;/p&gt;

&lt;p&gt;For much of programming, it&amp;rsquo;s easier to describe briefly what a function does
than to write code to test what it does. Often times my tests will be multiple
times as many lines of code as the function itself&amp;hellip; whereas the doc comment on
it may only be a few sentences.&lt;/p&gt;

&lt;p&gt;In addition, tests only explain the &lt;em&gt;what&lt;/em&gt; of a function. What is it supposed to
do? They don&amp;rsquo;t explain why, and why is often more important, as stated above.&lt;/p&gt;

&lt;p&gt;You should definitely test your code, and tests can be useful in figuring out
the expected behavior of code in some edge cases&amp;hellip; but if I have to read tests
to understand your code in general, then that&amp;rsquo;s red flag that you really need to
write more/better comments.&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I feel like the line between what&amp;rsquo;s a useful comment and what&amp;rsquo;s not is difficult
Expand Down
47 changes: 45 additions & 2 deletions index.html
Expand Up @@ -194,7 +194,7 @@ <h2 id="code-sucks">Code Sucks</h2>
<p>But of course, what is clean and obvious and well-written to you, today, while
the entire project and problem space are fully loaded in your brain&hellip; might not
be obvious to you, six months from now, or to the poor schmuck that has to debug
your code with their manager breathing down their neck beacuse the CTO just ran
your code with their manager breathing down their neck because the CTO just ran
into a critical bug in prod.</p>

<p>Learning to look at a piece of code that you understand, and trying to figure out
Expand All @@ -216,7 +216,7 @@ <h2 id="avoiding-comments-often-makes-your-code-worse">Avoiding Comments Often M
because you have to make your code clearer to compensate. I call BS on this as
well, because I don&rsquo;t think anyone is realistically writing sub-par code and
then excusing it by slapping a comment on it (aside from <code>// TODO: this is a
temporary hack, I'll fix it later</code>). We all write the best code we know howm,
temporary hack, I'll fix it later</code>). We all write the best code we know how,
given the various external constraints (usually time).</p>

<p>The problem with refactoring your code to avoid needing comments is that
Expand Down Expand Up @@ -333,6 +333,49 @@ <h2 id="any-comment-is-better-than-no-comment">Any comment is better than no com
<em>did</em> still round the duration down to the nearest second, so the comment was
not incorrect per se, it was just misleading.</p>

<h2 id="why">Why?</h2>

<p>The most important comments are the <em>why</em> comments. Why is the code doing what
it&rsquo;s doing? Why must the ID be less than 24 characters? Why are we hiding this
option on Linux? etc. The reason these are important is that you can&rsquo;t figure
out the why by looking at the code. They document lessons learned by the devs,
outside constraints imposed by the business, other systems, etc. These comments
are invaluable, and almost impossible to capture in other ways (e.g. function
names should document what the function does, not why).</p>

<p>Comments that document <em>what</em> the code is doing are less useful, because you can
generally always figure out what the code is doing, given enough time and
effort. The code tells you what it is doing, by definition. Which is not to
say that you should never write <em>what</em> comments. Definitely strive to write the
clearest code you can, but comments are free, so if you think someone might
misunderstand some code or otherwise have difficulty knowing what&rsquo;s going on,
throw in a comment. At least, it may save them a half hour of puzzling through
your code, at best it may save them from changing it or using it in incorrect
ways that cause bugs.</p>

<h2 id="tests">Tests</h2>

<p>Some people think that tests serve as documentation for functions. And, in a
way, this is true. But they&rsquo;re generally very low on my list of effective
documentation. Why? Well, because they have to be incredibly precise, and thus
they are verbose, and cover a narrow strip of functionality. Every test tests
exactly one specific input and one specific output. For anything other than the
most simple function, you probably need a bunch of code to set up the inputs and
construct the outputs.</p>

<p>For much of programming, it&rsquo;s easier to describe briefly what a function does
than to write code to test what it does. Often times my tests will be multiple
times as many lines of code as the function itself&hellip; whereas the doc comment on
it may only be a few sentences.</p>

<p>In addition, tests only explain the <em>what</em> of a function. What is it supposed to
do? They don&rsquo;t explain why, and why is often more important, as stated above.</p>

<p>You should definitely test your code, and tests can be useful in figuring out
the expected behavior of code in some edge cases&hellip; but if I have to read tests
to understand your code in general, then that&rsquo;s red flag that you really need to
write more/better comments.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I feel like the line between what&rsquo;s a useful comment and what&rsquo;s not is difficult
Expand Down
47 changes: 45 additions & 2 deletions index.xml
Expand Up @@ -37,7 +37,7 @@ better, written better, it wouldn&amp;rsquo;t need that comment.&lt;/p&gt;
&lt;p&gt;But of course, what is clean and obvious and well-written to you, today, while
the entire project and problem space are fully loaded in your brain&amp;hellip; might not
be obvious to you, six months from now, or to the poor schmuck that has to debug
your code with their manager breathing down their neck beacuse the CTO just ran
your code with their manager breathing down their neck because the CTO just ran
into a critical bug in prod.&lt;/p&gt;

&lt;p&gt;Learning to look at a piece of code that you understand, and trying to figure out
Expand All @@ -59,7 +59,7 @@ thing. Sometimes it can be a huge help.&lt;/p&gt;
because you have to make your code clearer to compensate. I call BS on this as
well, because I don&amp;rsquo;t think anyone is realistically writing sub-par code and
then excusing it by slapping a comment on it (aside from &lt;code&gt;// TODO: this is a
temporary hack, I&#39;ll fix it later&lt;/code&gt;). We all write the best code we know howm,
temporary hack, I&#39;ll fix it later&lt;/code&gt;). We all write the best code we know how,
given the various external constraints (usually time).&lt;/p&gt;

&lt;p&gt;The problem with refactoring your code to avoid needing comments is that
Expand Down Expand Up @@ -176,6 +176,49 @@ updated the function to take a Duration rather than an Int. Interestingly, it
&lt;em&gt;did&lt;/em&gt; still round the duration down to the nearest second, so the comment was
not incorrect per se, it was just misleading.&lt;/p&gt;

&lt;h2 id=&#34;why&#34;&gt;Why?&lt;/h2&gt;

&lt;p&gt;The most important comments are the &lt;em&gt;why&lt;/em&gt; comments. Why is the code doing what
it&amp;rsquo;s doing? Why must the ID be less than 24 characters? Why are we hiding this
option on Linux? etc. The reason these are important is that you can&amp;rsquo;t figure
out the why by looking at the code. They document lessons learned by the devs,
outside constraints imposed by the business, other systems, etc. These comments
are invaluable, and almost impossible to capture in other ways (e.g. function
names should document what the function does, not why).&lt;/p&gt;

&lt;p&gt;Comments that document &lt;em&gt;what&lt;/em&gt; the code is doing are less useful, because you can
generally always figure out what the code is doing, given enough time and
effort. The code tells you what it is doing, by definition. Which is not to
say that you should never write &lt;em&gt;what&lt;/em&gt; comments. Definitely strive to write the
clearest code you can, but comments are free, so if you think someone might
misunderstand some code or otherwise have difficulty knowing what&amp;rsquo;s going on,
throw in a comment. At least, it may save them a half hour of puzzling through
your code, at best it may save them from changing it or using it in incorrect
ways that cause bugs.&lt;/p&gt;

&lt;h2 id=&#34;tests&#34;&gt;Tests&lt;/h2&gt;

&lt;p&gt;Some people think that tests serve as documentation for functions. And, in a
way, this is true. But they&amp;rsquo;re generally very low on my list of effective
documentation. Why? Well, because they have to be incredibly precise, and thus
they are verbose, and cover a narrow strip of functionality. Every test tests
exactly one specific input and one specific output. For anything other than the
most simple function, you probably need a bunch of code to set up the inputs and
construct the outputs.&lt;/p&gt;

&lt;p&gt;For much of programming, it&amp;rsquo;s easier to describe briefly what a function does
than to write code to test what it does. Often times my tests will be multiple
times as many lines of code as the function itself&amp;hellip; whereas the doc comment on
it may only be a few sentences.&lt;/p&gt;

&lt;p&gt;In addition, tests only explain the &lt;em&gt;what&lt;/em&gt; of a function. What is it supposed to
do? They don&amp;rsquo;t explain why, and why is often more important, as stated above.&lt;/p&gt;

&lt;p&gt;You should definitely test your code, and tests can be useful in figuring out
the expected behavior of code in some edge cases&amp;hellip; but if I have to read tests
to understand your code in general, then that&amp;rsquo;s red flag that you really need to
write more/better comments.&lt;/p&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I feel like the line between what&amp;rsquo;s a useful comment and what&amp;rsquo;s not is difficult
Expand Down

0 comments on commit 285094b

Please sign in to comment.