<?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>DEV Community: Tim Eriksen</title>
    <description>The latest articles on DEV Community by Tim Eriksen (@timeriksen).</description>
    <link>https://dev.to/timeriksen</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%2F130363%2F084acb33-17c3-4c67-8e2c-fd9a16d97a99.jpeg</url>
      <title>DEV Community: Tim Eriksen</title>
      <link>https://dev.to/timeriksen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timeriksen"/>
    <language>en</language>
    <item>
      <title>Writing Comments First To Increase Productivity</title>
      <dc:creator>Tim Eriksen</dc:creator>
      <pubDate>Tue, 21 Jan 2020 09:21:03 +0000</pubDate>
      <link>https://dev.to/timeriksen/writing-comments-first-to-increase-productivity-79g</link>
      <guid>https://dev.to/timeriksen/writing-comments-first-to-increase-productivity-79g</guid>
      <description>&lt;p&gt;&lt;em&gt;The code in this article is written in Python but this method applies to what ever language you prefer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can all probably agree that commenting your code is generally a good practice. If you have followed a coding tutorial or attended a programming class, then you have likely heard some variation of the line «comment your code» before, and for good reason. Making sense of a project that you haven’t touched in a few months, can be pretty time consuming without proper comments.&lt;/p&gt;

&lt;p&gt;But did you know that comments can be useful for more than just inline documentation? Here is a little tip that can help you actually remember to write comments, and increase your productivity at the same time! If you are familiar with writing "TO DO" comments or pseudo code, then this method should feel familiar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining an example task
&lt;/h2&gt;

&lt;p&gt;Let's say you are working on a project and your next task is to add a simple function that will find and read a query stored in a file. The function is required to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accept a query name/file name as an argument&lt;/li&gt;
&lt;li&gt;Read the query from the file&lt;/li&gt;
&lt;li&gt;Return the query&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;Alright, so you know what the task is. You may even have a rough idea of how you would like to approach the task. After taking some time to research any parts of the task you may be unfamiliar with, it is time to write some code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aaand that’s enough code for now. How come? Well, that’s what this is all about: you write the comments before you write the code. Why would you do that? Because it gives you a chance to &lt;em&gt;really think through how you want to solve the problem&lt;/em&gt; before you have to start thinking about syntax, error handling, naming variables, remembering what that one method was called, and so on. Think of it like drawing a quick sketch before painting the final picture.&lt;/p&gt;

&lt;p&gt;So how do you get started with this «comments first» approach? It is rather simple. You figure out the major actions that the function will need to perform, and then you write those actions down as comments in your function. Sort of like a bulleted list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Add file ending to query name if not included
&lt;/span&gt;    &lt;span class="c1"&gt;# Get path to the file
&lt;/span&gt;    &lt;span class="c1"&gt;# Open file, read contents and close file
&lt;/span&gt;    &lt;span class="c1"&gt;# Handle file error
&lt;/span&gt;    &lt;span class="c1"&gt;# Return the query
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Even tough this function dosen't really &lt;em&gt;do&lt;/em&gt; anything yet, it is already easy to grasp what the function will do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the code
&lt;/h2&gt;

&lt;p&gt;Here comes another benefit of this method: you now have sort of a "to do list" of the functionality you have to write. To start writing your code you simply add a new line under det first comment and write code to match the comment. Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Add file ending to query name if not included
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;'.graphql'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;query_name&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;'.graphql'&lt;/span&gt;

    &lt;span class="c1"&gt;# Get path to the file
&lt;/span&gt;    &lt;span class="c1"&gt;# Open file, read contents and close file
&lt;/span&gt;    &lt;span class="c1"&gt;# Handle file error
&lt;/span&gt;    &lt;span class="c1"&gt;# Return the query
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you just keep on going, comment by comment. Does new things come to mind as you implement the function? Write them down as comments! If you get distracted by coworkers, leave for lunch, or your energy is low and it is hard to keep focus, &lt;em&gt;the outline for your solution is already written down&lt;/em&gt;. Picking up where you left off is no problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Add file ending to query name if not included
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s"&gt;'.graphql'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;query_name&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;'.graphql'&lt;/span&gt;

    &lt;span class="c1"&gt;# Get path to the file
&lt;/span&gt;    &lt;span class="n"&gt;query_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__file__&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'queries/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Open file, read contents and close file
&lt;/span&gt;    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;query_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;query_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;query_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Handle file error
&lt;/span&gt;    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;FileNotFoundError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'Query "&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;query_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;" not found'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

    &lt;span class="c1"&gt;# Return the query
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There we go. The task is finished. The code is commented so that it requires less effort to understand. Developers reading this code in the future (including yourself) will be thankful!&lt;/p&gt;

&lt;p&gt;The "comments first" method is even more useful when writing larger and more complex pieces of code than what we did in this example. It can also be a good early step in planning the implementation of a project.&lt;/p&gt;

&lt;p&gt;I hope you find this method useful and that you try it out at some point!&lt;/p&gt;

</description>
      <category>comments</category>
      <category>productivity</category>
      <category>programming</category>
      <category>python</category>
    </item>
  </channel>
</rss>
