<?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: Timilehin Bakare</title>
    <description>The latest articles on DEV Community by Timilehin Bakare (@plainsight16).</description>
    <link>https://dev.to/plainsight16</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%2F303345%2F1ca1e71e-3506-4534-ade1-95d246dfc1a0.jpg</url>
      <title>DEV Community: Timilehin Bakare</title>
      <link>https://dev.to/plainsight16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/plainsight16"/>
    <language>en</language>
    <item>
      <title>Concurrency in Java: The Dining Philosophers Problem</title>
      <dc:creator>Timilehin Bakare</dc:creator>
      <pubDate>Thu, 24 Feb 2022 11:44:57 +0000</pubDate>
      <link>https://dev.to/plainsight16/concurrency-in-java-the-dining-philosophers-problem-nko</link>
      <guid>https://dev.to/plainsight16/concurrency-in-java-the-dining-philosophers-problem-nko</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Problem Statement&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Philosopher&lt;/li&gt;
&lt;li&gt;Unto The Chopstick&lt;/li&gt;
&lt;li&gt;The Feast&lt;/li&gt;
&lt;li&gt;So then where is the problem&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Solutions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Reset Solution&lt;/li&gt;
&lt;li&gt;The Asymmetric Solution&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Conclusions.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;In Concurrency, resources and the threads that utilize them are at its core. The dining philosophers problem highlights some of the issues that arise when resources are shared among threads and provides the solutions.&lt;/p&gt;

&lt;p&gt;In this article, we would be discussing the problem and the solutions alongside their code implementations in java.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuhgni9ifz9d1uo1red2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuhgni9ifz9d1uo1red2.png" alt="the dining philosophers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Statement. &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The dining philosophers problem states that 5 philosophers in a restaurant sit at a round table for a meal. For the meal, they each require two chopsticks in total 10 chopsticks, but there's a problem, at the table only 5 chopsticks were provided.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;I know I know, just get more chopsticks right?. Bear with me.&lt;/em&gt;)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public class DinningPhilosophers {
      static int no_of_philosophers = 5;
      static Philosopher [] philosophers = new 
      Philosopher[no_of_philosophers];
      static Chopstick[] chopsticks = new 
      Chopstick[no_of_philosophers];


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Each philosopher is represented as a thread and each chopstick, a resource.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Philosopher. &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;A philosopher can only perform 2 actions, to think and to eat. A philosopher is either thinking or eating and nothing in-between.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To Think: A philosopher must put down both chopsticks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To Eat: A philosopher must be in possession of both chopsticks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Initially, all philosophers begin with thinking, each philosopher thinks for a variable time length. When thinking ends, they begin eating. Similarly, eating occurs for a variable time length for each philosopher. &lt;br&gt;
It is important to note that these two actions would continue for an infinite amount of time. The problem assumes that the philosophers never get filled from eating or tired from thinking.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;Disclaimer: The writer would not be involved in any form of payment for the food eaten by these bottomless pits of philosophers.&lt;/em&gt;)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

private static class Philosopher implements Runnable{
      private int number;
      Chopstick leftChopstick;
      Chopstick rightChopstick;
      public Philosopher( int number, Chopstick leftChopstick, 
                        Chopstick rightChopstick){
          this.number = number;
          this.leftChopstick = leftChopstick;
          this.rightChopstick = rightChopstick;
      }
      void performAction(String action){
          try{
          int waitTime = ThreadLocalRandom.current().nextInt(0, 
          1000);
          System.out.println("Philosopher "+ (number + 1) + action 
                            + waitTime +" ms");
          }catch(InterruptedException ex){
              ex.printStackTrace();
          }
      }
      @Override
      public void run(){
           while(true) {
                performAction(" thinks for ");
                leftChopstick.grabChopstick();
                System.out.println("Philosopher " + (number + 1) + 
                                   " picks up leftChopstick");
                rightChopstick.grabChopstick();
                System.out.println("Philosopher " + (number + 1) + 
                                  " picks up rightChopstick");

                performAction(" eats for ");
                leftChopstick.dropChopstick();
                System.out.println("Philosopher " + (number + 1) + 
                                   " drops leftChopstick");
                rightChopstick.dropChopstick();
                System.out.println("Philosopher " + (number + 1) + 
                                   " drops rightChopstick");
            }
      }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The variable &lt;strong&gt;waitTime&lt;/strong&gt; is used to determine the variable time length for each action in milliseconds.&lt;br&gt;
Since both eat and think actions require a variable time length, I have used the &lt;strong&gt;performAction(action)&lt;/strong&gt; function in their stead to perform for both actions by passing in a parameter (&lt;strong&gt;action&lt;/strong&gt;) to differentiate both actions.&lt;br&gt;
Also notice in the &lt;strong&gt;run&lt;/strong&gt; function the philosopher must acquire both chopsticks before performing the eat action and must drop both chopsticks after eat action is completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unto The Chopstick.&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 private static class Chopstick{
        public Semaphore semaphore = new Semaphore(1);
        void grabChopstick(){
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        void dropChopstick(){
            semaphore.release();
        }
 }



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A feature in the Chopstick class is the &lt;a href="https://www.geeksforgeeks.org/semaphores-in-process-synchronization/" rel="noopener noreferrer"&gt;Semaphore&lt;/a&gt;. Here the semaphore is used to secure a chopstick once it has been grabbed by a philosopher. This prevents other philosophers from snatching a chopstick that has already been picked up.&lt;/p&gt;

&lt;p&gt;Now that we have created the Philosopher class and the Chopstick class, we can now dig in.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;Let the feast begin&lt;/em&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  The Feast.&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

     public static void main(String []args){
          for (int i = 0; i &amp;lt; no_of_philosophers; i++){
              chopsticks[i] = new Chopstick();
          }
          ExecutorService executor = 
          Executors.newFixedThreadPool(no_of_philosophers);
          for (int i = 0; i &amp;lt; no_of_philosophers; i++){
               philosophers[i] = new Philosopher(i, chopsticks[i], 
               chopsticks[(i + 1) % no_of_philosophers] );
               executor.execute(philosophers[i]);
          }
     }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;chopsticks[(i + 1) % no_of_philosophers]&lt;/strong&gt; used in the second parameter of Philosopher constructor is to prevent chopsticks from going out of bounds.&lt;br&gt;
for example: i starting from 0,&lt;br&gt;
             At i = 4, i + 1 = 5  (&lt;em&gt;out of bounds&lt;/em&gt;).&lt;br&gt;
             Therefore to prevent that,&lt;br&gt;
             5 % 5 = 0.&lt;br&gt;
Remember that the philosophers are seated at a roundtable so the chopstick available to the 5th philosopher would be the 5th chopstick and the 1st.&lt;/p&gt;

&lt;h3&gt;
  
  
  So then where is the problem. &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We have commenced our feast and due to having varying time lengths for thinking and eating among the philosophers, the chopsticks can be shared despite having just 5 of them.&lt;/p&gt;

&lt;p&gt;However, on a particular iteration Philosopher 1(&lt;strong&gt;p1&lt;/strong&gt;) comes out of thinking and grabs the left-chopstick but before he grabs the right one, p2 comes out of thinking and grabs his left-chopstick which just happens to be p1's right chopstick.&lt;/p&gt;

&lt;p&gt;The struggle begins, &lt;strong&gt;p1&lt;/strong&gt; cannot snatch his right-chopstick from &lt;strong&gt;p2&lt;/strong&gt;(&lt;em&gt;recall the semaphore&lt;/em&gt;) and &lt;strong&gt;p2&lt;/strong&gt; just refuses the drop his chopstick, both sides refusing to succumb.&lt;/p&gt;

&lt;p&gt;To make matters worse &lt;strong&gt;p3&lt;/strong&gt;, &lt;strong&gt;p4&lt;/strong&gt; and &lt;strong&gt;p5&lt;/strong&gt; are in a similar struggle. The whole feast grinds to a halt as no philosopher is able to eat.&lt;br&gt;
This scenario is called a &lt;a href="https://en.wikipedia.org/wiki/Deadlock" rel="noopener noreferrer"&gt;Deadlock&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hal9ht2154s8ktp98qj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hal9ht2154s8ktp98qj.PNG" alt="deadlock"&gt;&lt;/a&gt;&lt;br&gt;
(&lt;em&gt;Ladies and Gents, I believe we've found our problem.&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solutions.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;While there are a number of solutions to the dining philosophers problem, in this article we'll be looking at 2 of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Reset solution. &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;It uses a reset function to force all philosophers drop their chopsticks and restart the iteration.&lt;br&gt;
While this could help resolve the immediate deadlock, there is no assurance that the next iteration would not also lead to a deadlock. &lt;br&gt;
When a deadlock continues to repeat it is called a &lt;a href="https://www.geeksforgeeks.org/deadlock-starvation-and-livelock/" rel="noopener noreferrer"&gt;live-lock&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Asymmetric solution. &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This solution ensures that for every &lt;strong&gt;odd&lt;/strong&gt; number philosopher i.e. &lt;strong&gt;p1&lt;/strong&gt;, &lt;strong&gt;p3&lt;/strong&gt;, &lt;strong&gt;p5&lt;/strong&gt;, the left-chopstick is grabbed first before the right and for every &lt;strong&gt;even&lt;/strong&gt; number philosopher (&lt;strong&gt;p2&lt;/strong&gt;, &lt;strong&gt;p4&lt;/strong&gt;), the right chopstick is picked up before the left.&lt;/p&gt;

&lt;p&gt;This prevents the conflict when two adjacent philosophers come out of thinking at the almost the same time.&lt;/p&gt;

&lt;p&gt;for example: &lt;strong&gt;p1&lt;/strong&gt; comes out of thinking and grabs his left-chopstick.&lt;br&gt;
At the same moment &lt;strong&gt;p2&lt;/strong&gt; comes out of thinking. This time, rather than reaching for his left-chopstick which at that moment p1 would also be reaching for, causing a deadlock. It instead reaches for its right-chopstick, giving &lt;strong&gt;p1&lt;/strong&gt; enough time to reach and acquire his right-chopstick preventing a deadlock.&lt;br&gt;
The same thing occurs in the other adjacent philosophers, where one philosopher takes advantage of the other's re-direction.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

   public static void main(String []args){
        for (int i = 0; i &amp;lt; no_of_philosophers; i++){
            chopsticks[i] = new Chopstick();
        }
        ExecutorService executor = 
        Executors.newFixedThreadPool(no_of_philosophers);
        for (int i = 0; i &amp;lt; no_of_philosophers; i++){
            if( i % 2 == 0)
                philosophers[i] = new Philosopher(i, chopsticks[(i 
                + 1) % no_of_philosophers], chopsticks[i] );
            else
                philosophers[i] = new Philosopher(i, 
                chopsticks[i], chopsticks[(i + 1) % 
                no_of_philosophers] );

            executor.execute(philosophers[i]);
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice the slight change in the implementation, where if &lt;strong&gt;Even&lt;/strong&gt;, the order of acquiring a chopstick is reversed.&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;Now the feast can really begin&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;(&lt;em&gt;A bottomless pit still has a bottom it seems&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;We have come to the end of the feast. I hope this article has cleared any confusions you might have had on the dining philosophers problem.&lt;/p&gt;

&lt;p&gt;You might ask where the concept could be applied. One of its applications is in Operating Systems where every process needs two resources out of which, one has been acquired and the other is acquired by another process. Till the other process frees the resource, this process cannot proceed. Similarly, the other process is dependent on another process for its resource. Since every process is dependent on each other, it forms a circular-wait and the system goes into a deadlock. This deadlock can then be resolved through various techniques of which, some were discussed in this article.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;for the full code implementation&lt;/em&gt;: &lt;a href=""&gt;https://github.com/plainsight16/Java-Concurrency/blob/main/dinningPhilosphers/DinningPhilosophers.java&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>concurrency</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>A Guide to writing to better comments</title>
      <dc:creator>Timilehin Bakare</dc:creator>
      <pubDate>Sun, 16 Jan 2022 23:22:24 +0000</pubDate>
      <link>https://dev.to/plainsight16/a-guide-to-writing-to-better-comments-3pol</link>
      <guid>https://dev.to/plainsight16/a-guide-to-writing-to-better-comments-3pol</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You're probably thinking," &lt;em&gt;what's new with comments?&lt;/em&gt;" and you'll be right to think that.&lt;br&gt;
Often times, not much thought is put into writing a comment. They are mostly ignored and considered secondary(albeit as they should be) but this negligence has led many programmers into writing very awful comments.&lt;br&gt;
for example:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu57obznnve450cconcvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu57obznnve450cconcvv.png" alt="bad comment code snippet"&gt;&lt;/a&gt;&lt;br&gt;
In order to avoid writing comments like this, I have written this article to serve as a guide to writing better comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here are some guides to writing better comments
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Avoid writing redundant comments&lt;/li&gt;
&lt;li&gt;Comments should be updated&lt;/li&gt;
&lt;li&gt;Do not excuse bad code with commenting&lt;/li&gt;
&lt;li&gt;Comments should not reference other codes&lt;/li&gt;
&lt;li&gt;Comments should be informative&lt;/li&gt;
&lt;li&gt;Comments could be used to provide links to original source code&lt;/li&gt;
&lt;li&gt;Comments should not be used for attribution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The rest of this article explains these guides and provides examples to them.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Avoid writing redundant comments
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumtn14gpsu3kd9yeg7ex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fumtn14gpsu3kd9yeg7ex.png" alt="redundant comments"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above is common among early-beginners as they're introduced to code, While this could be very helpful with teaching beginners. Its considered redundant as it adds no information and merely causes visual clutter.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Comments should be updated.
&lt;/h3&gt;

&lt;p&gt;Overtime comments silently degrade. While code is continuously updated, the comments are forgotten. For a code cryptic in nature, readers turn to the comments to provide an explanation, and when that explanation is false it leaves the readers confused or worse mislead.&lt;br&gt;
for example: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yjyud10vmvepgnnwrk3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yjyud10vmvepgnnwrk3.png" alt="regex-pattern comment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The asterisks in the regex pattern show that  it matches a whole lot more than it was stated by the comments which could be very misleading to the readers who do not bother to check the regex pattern.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Do not excuse bad code with commenting
&lt;/h3&gt;

&lt;p&gt;Commenting should be used as a last resort. Every comment serves as a failure to express yourself clearly in code. Always try to clean the code to express yourself better before resulting to commenting. &lt;br&gt;
for example the code snippet below could have been cleaned up rather than using a comment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvmqsphkf4zxz64lu771k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvmqsphkf4zxz64lu771k.png" alt="bad code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After cleaning it up&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsz7950s8v6138x87e8k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsz7950s8v6138x87e8k.png" alt="good code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Comments should not reference other codes
&lt;/h3&gt;

&lt;p&gt;A comment should not reference a code that is not present or local because the reference code could change but the comment wouldn't&lt;br&gt;
for example&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farhaaxo6hj9xwondb10a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farhaaxo6hj9xwondb10a.png" alt="poor reference comment"&gt;&lt;/a&gt;&lt;br&gt;
The comment above references a "default" that is not present in the code block, this confuses the reader as they don't know what default in the comment means.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Comments should be informative
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiomphuwjkwz2sf3ycgay.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiomphuwjkwz2sf3ycgay.png" alt="Informative comment"&gt;&lt;/a&gt;&lt;br&gt;
The comment above informs the reader that SimpleDateFormat is not thread-safe, if the code is to be modified, the programmer would be well informed of its limitations. &lt;/p&gt;

&lt;h3&gt;
  
  
  6. Comments could be used to provide links to original source code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famao4tevyuqdqqx96l49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famao4tevyuqdqqx96l49.png" alt="linked comment"&gt;&lt;/a&gt;&lt;br&gt;
Readers can follow the link provided in the comments to learn more&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Comments should not be used for attribution
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97u0u0j1wh138nx1wjjm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97u0u0j1wh138nx1wjjm.png" alt="Attribution comment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Comments like these are unacceptable especially in production. The source code control systems can always tell who the author was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this article has been helpful in highlighting the importance of writing  proper comments and the guides provided were well understood.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;References&lt;/em&gt;: Clean code(A handbook of Agile Software Craftsmanship)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
      <category>java</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
