<?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: Francois MUGOROZI</title>
    <description>The latest articles on DEV Community by Francois MUGOROZI (@francoismugorozi).</description>
    <link>https://dev.to/francoismugorozi</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%2F376441%2Fb871bfc6-e293-4791-abea-71152f26a28c.jpeg</url>
      <title>DEV Community: Francois MUGOROZI</title>
      <link>https://dev.to/francoismugorozi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/francoismugorozi"/>
    <language>en</language>
    <item>
      <title>Debugging and Refactoring JavaScript codes for robustness</title>
      <dc:creator>Francois MUGOROZI</dc:creator>
      <pubDate>Sat, 16 May 2020 19:49:20 +0000</pubDate>
      <link>https://dev.to/francoismugorozi/debugging-and-refactoring-javascript-codes-for-robustness-kgd</link>
      <guid>https://dev.to/francoismugorozi/debugging-and-refactoring-javascript-codes-for-robustness-kgd</guid>
      <description>&lt;p&gt;In this world of technology, programming has became necessary in every field. People are changing carriers to becoming programmers. However, knowing how to code is not enough. You need to know how to write robust codes and be able to fix bugs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why debugging?
&lt;/h3&gt;

&lt;p&gt;Debugging is a programming concept used to identify bugs and errors in code and fix them.&lt;/p&gt;

&lt;p&gt;Now you can understand that every programmer must be able to debug his code.&lt;br&gt;
Let's talk about it.&lt;/p&gt;

&lt;p&gt;You probably know that you can write any program using just notepad or any note writer you have. That's true. However, it will be hard for you to figure out the problems when your code breaks or does not give what you expected. The solution is to use a code editor designed specifically for programming like &lt;a href="https://code.visualstudio.com/"&gt;VS Code&lt;/a&gt;, and &lt;a href="https://www.sublimetext.com/"&gt;Sublime Text&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function power(base, exponent) {
  var result = 1;
  for (var count = ; count &amp;lt; exponent; count++)
    result *= base;
  return result;
}

power(12,'h');&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can identify some errors just with code editor together with some linting extensions like &lt;a href="https://eslint.org/"&gt;ESLint&lt;/a&gt; and from the above code, we can see the errors now&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gct3MvTH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/di2dtvljstvtn0cuku9a.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gct3MvTH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/di2dtvljstvtn0cuku9a.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What about errors not found here like &lt;strong&gt;Run time errors&lt;/strong&gt; as the one where we passed 'h' instead of a number. Well, you need to debug. &lt;br&gt;
Here you simply try to execute your code line by line to check whether your expectations are met.&lt;br&gt;
For this purpose, you can use dev tools found in the browser (chrome) or your code editor and set a breakpoint where you want the execution to stop and take control. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WRumQdbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ur4atv90hzl0zmuhy9z.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WRumQdbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7ur4atv90hzl0zmuhy9z.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you feel at ease because your codes are free of errors. Think about it, what if someone comes a year later and change your code? &lt;br&gt;
The solution to this is to protect your code by writing tests so that anyone who changes your code will have to face the tests. &lt;a href="https://javascript.info/testing-mocha"&gt;More about Testing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From here, we are good to go. One thing more to make your code clean is &lt;strong&gt;Refactoring&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Refactoring?
&lt;/h3&gt;

&lt;p&gt;Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.&lt;br&gt;
There are a lot of tools that can help you refactor the code like &lt;a href="https://marketplace.visualstudio.com/items?itemName=cmstead.jsrefactor"&gt;JS Refactor&lt;/a&gt;. However, they don't do anything for you.&lt;/p&gt;

&lt;p&gt;Looking at the code snippet below, is there a better way to write it? The answer is yes. here I use a simple example of changing names to capital letters.&lt;/p&gt;

&lt;p&gt;Before Refactoring:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var names = ["JOhn","Mark","Andre","Alice","Munana"];
var newNames = [];
 for (var i= 0;i&amp;lt; names.length;i++){
   var capitalized = names[i].toUpperCase();
   newNames.push(capitalized);
 }

 console.log('Capitalized',newNames,'Original',names);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;After Refactoring:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var names = ["JOhn","Mark","Andre","Alice","Munana"];
var newNames = names.map(name=&amp;gt;name.toUpperCase());
console.log('Capitalized',newNames,'Original',names);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Look that, only three lines. Much better.&lt;/p&gt;

&lt;p&gt;There are a lot of technics for refactoring codes like &lt;strong&gt;Composing Methods Refactoring&lt;/strong&gt;, &lt;strong&gt;Simplifying Conditional Expressions Refactoring&lt;/strong&gt;, and &lt;a href="https://dzone.com/articles/code-refactoring-techniques"&gt;Others&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;It's essential to know how to debug and fix codes. Refactoring should be added to your skills to make clean, robust, and understandable code&lt;/p&gt;

</description>
      <category>javscript</category>
      <category>debugging</category>
      <category>refactoring</category>
      <category>bestpractices</category>
    </item>
    <item>
      <title>Test-Driven Development (TDDF) For API</title>
      <dc:creator>Francois MUGOROZI</dc:creator>
      <pubDate>Thu, 30 Apr 2020 00:20:00 +0000</pubDate>
      <link>https://dev.to/francoismugorozi/test-driven-development-tddf-for-api-207</link>
      <guid>https://dev.to/francoismugorozi/test-driven-development-tddf-for-api-207</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GITvu4l8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/doznen6q8l5k9emb0eiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GITvu4l8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/doznen6q8l5k9emb0eiw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  TDD
&lt;/h3&gt;

&lt;p&gt;Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the code is improved so that the tests pass. This is opposed to software development that allows code to be added that is not proven to meet requirements.&lt;a href="https://en.wikipedia.org/wiki/Test-driven_development"&gt;Wkipedia&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why TDD
&lt;/h3&gt;

&lt;p&gt;You can't imagine how TDD is very useful in software development industries. Most individuals programmers usually do not use this approach. However, the trusted and scalable product has to be tested for any expected cases. Hare are some of the benefits of TDD:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test-driven development offers more than just simple validation of correctness, but can also drive the design of a program&lt;/li&gt;
&lt;li&gt;It allows a programmer to focus on the task at hand as the first goal is to make the test pass&lt;/li&gt;
&lt;li&gt;TDD can lead to more modularized, flexible, and extensible code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  You can not talk about TDD without talking about CI/CD
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wn1rOSLQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3xz6uw1o33nz3m14em9s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wn1rOSLQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3xz6uw1o33nz3m14em9s.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
CI is a coding philosophy and set of practices that drive development teams to implement small changes and check-in code to version control repositories frequently. Because most modern applications require developing code in different platforms and tools, the team needs a mechanism to integrate and validate its changes.&lt;a href="https://www.infoworld.com/article/3271126/what-is-cicd-continuous-integration-and-continuous-delivery-explained.html"&gt;Read more&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without talking to much, let see this in practice. For this demo, I am going to create a simple contacts book API.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites:

&lt;ul&gt;
&lt;li&gt;Node js and Express&lt;/li&gt;
&lt;li&gt;Creating API.&lt;/li&gt;
&lt;li&gt;Git and Github&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now I will start&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Step 1: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Repository on Github mine is "tdd-demo-ass" and clone it to your computer.&lt;/li&gt;
&lt;li&gt;Create an account on Travis and connect your repository with Travis, after connecting it you have to copy the markdown and add it to your README file.&lt;a href="https://docs.travis-ci.com/user/languages/javascript-with-nodejs/"&gt;Read more here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Create account on coveralls.io and connect your repo.&lt;a href="https://docs.coveralls.io/"&gt;Coveralls&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2:&lt;br&gt;&lt;br&gt;
Now that we have Travis, it will help us for CI and then we need where to deploy our app after a successful build, here we can use &lt;a href="//www.heroku.com"&gt;Heroku&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
To be able to perform in continuous delivery, we create pipelines which help to deploy our codes to the codebase automatically when all test pass.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Go to Heroku, create an account, and then create a new pipeline and connect it with your GitHub repo. From there, you can have a review app (where you can test every pull request before merging into the base branch), staging app which you can use whole testing and production app where the final app gets deployed when everything is ready.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 3:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we have everything set up, we will start by writing our test cases, which by now will fail.&lt;br&gt;
This is the structure of mocha and chai testing&lt;/p&gt;

&lt;pre&gt;


```
const chai = require('chai');
const { expect } = require('chai');
const { describe, it } = require('mocha');
const chaiHttp = require('chai-http');
const { app } = require('../app');

chai.should();
chai.use(chaiHttp);

describe('Route test', () =&amp;gt; {
  it('Should return 404 not found route', (done) =&amp;gt; {
    chai
      .request(app)
      .get('/api/notfound')
      .end((err, res) =&amp;gt; {
        expect(res).to.have.status(404);
        done();
      });
  });

  it('Should return 200 on / request route', (done) =&amp;gt; {
    chai
      .request(app)
      .get('/')
      .end((err, res) =&amp;gt; {
        expect(res).to.have.status(200);
        done();
      });
  });
});
```


&lt;/pre&gt;

&lt;p&gt;Alt Text](&lt;a href="https://dev-to-uploads.s3.amazonaws.com/i/cuyh9bbwpbcemekfxcs4.JPG"&gt;https://dev-to-uploads.s3.amazonaws.com/i/cuyh9bbwpbcemekfxcs4.JPG&lt;/a&gt;)&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jV26gxz2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djrlevnbp4e14thw5s6h.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jV26gxz2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/djrlevnbp4e14thw5s6h.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
From here when we create a pull request of our first branch to merge it into our base branch, the Travis tests fail and the code can not be deployed&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--faSZ_2fX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/guvuhybdg4y4yklh6uur.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--faSZ_2fX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/guvuhybdg4y4yklh6uur.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It may sound reckless to start with writing tests, but believe me this will save us from a lot of troubles and also keep our codes maintainable so that none can mess with it in the future.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 4:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No, it's time to write codes that pass the test. I will create contacts book API with basic CRUD operations and authentication. when I am done, this is what I get when I run the test again!&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YDe6xL3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/knwouaccsyam1taep22r.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YDe6xL3b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/knwouaccsyam1taep22r.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
And now when the PR is merged, my app automatically get deployed through the pipeline we created on Heroku. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pUqRftk4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e5inr0sm75s1atzwe69n.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pUqRftk4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e5inr0sm75s1atzwe69n.JPG" alt="Alt Text"&gt;&lt;/a&gt; and Travis and Coverage get 100% &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ly28o8Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4zge1stfbvuz89f09b69.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ly28o8Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4zge1stfbvuz89f09b69.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Now that we have an idea of what happens in CI/CD and TDD development, I believe that this is a required skill for every developer for creating effective and maintainable codes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Remember, every API needs documentation, you can get this demo API doc &lt;a href="https://documenter.getpostman.com/view/10965967/SzmY8Lw7?version=latest"&gt;Here&lt;/a&gt;.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  You can also get the codes for this demo on &lt;a href="https://github.com/Francois-MUGOROZI/tdd-demo-ass"&gt;GitHub&lt;/a&gt;.
&lt;/h3&gt;

</description>
      <category>tdd</category>
      <category>node</category>
      <category>mocha</category>
      <category>express</category>
    </item>
  </channel>
</rss>
