<?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: CED</title>
    <description>The latest articles on DEV Community by CED (@theghostyced).</description>
    <link>https://dev.to/theghostyced</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%2F170119%2F8caee005-5bd4-4ec0-8a03-49868c3b5197.jpg</url>
      <title>DEV Community: CED</title>
      <link>https://dev.to/theghostyced</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/theghostyced"/>
    <language>en</language>
    <item>
      <title>How To Be A Better Developer</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Sun, 25 Aug 2019 15:04:41 +0000</pubDate>
      <link>https://dev.to/theghostyced/how-to-be-a-better-developer-578g</link>
      <guid>https://dev.to/theghostyced/how-to-be-a-better-developer-578g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;So, if you reading this article then I can assume you are looking for ways to become a better developer and with this article, I will try my utmost to guide you on that path.&lt;/p&gt;




&lt;h2&gt;
  
  
  Favour Solutions over Coding Skills 👨🏿‍🔧
&lt;/h2&gt;

&lt;p&gt;Gone are the days when developers knew the true meaning of being called a &lt;code&gt;Developer&lt;/code&gt;. A developer used to be known as an individual who tries to solve real-world problems with technology. These days, a developer is simply someone who can write code.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566730894281%2F_ABO51hCJ.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566730894281%2F_ABO51hCJ.gif" alt="solve.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nowadays, few people are trying to solve problems with their code others are simply trying to learn and level up on the latest invoke stack so they don't get left behind. &lt;/p&gt;

&lt;p&gt;Now am not saying this is necessarily a bad thing, it becomes bad when it is prioritized over the problem you want to solve. Have literally seen startups argue for hours over the stack they would use to build a software and giving little thought as to how they would go about solving it.&lt;/p&gt;

&lt;p&gt;In short, to become a better developer you need to understand that the problem you solve is far more important than the code you write.&lt;/p&gt;

&lt;h2&gt;
  
  
  Strengthen Your Weakness 💪
&lt;/h2&gt;

&lt;p&gt;This seems to be the most obvious right 🤷‍♂️? If you want to be a better developer then obviously the thing to do is work on your weaknesses. Well, tell that to people who call themselves &lt;code&gt;Backend-only&lt;/code&gt; developers. It marvels me whenever someone tells me he is a backend/frontend only developer and that they have nothing to do with the UI / Database.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566730725239%2FuFPSYUaCr.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566730725239%2FuFPSYUaCr.gif" alt="giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now am not saying to become a better developer then you need to go crazy and master both frontend and backend, all am saying is don't neglect any one of them. There is nothing like a &lt;strong&gt;backend-only&lt;/strong&gt; or a &lt;strong&gt;frontend-only&lt;/strong&gt; developer, there is only a developer. &lt;/p&gt;

&lt;p&gt;The good news is that you have figured where your strengths lie so all you have to do is work on the area you are bad at. If it is frontend, then you can learn how to layout a page and if it is backend, learn about the MVC architecture. &lt;strong&gt;Don't Neglect Your Weakness&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hone Your Foundations 🏋️‍♀️
&lt;/h2&gt;

&lt;p&gt;When I say foundations, I mean understand and honing your programming fundamental. This involves solving algorithmic questions and learning about other types of data structures.&lt;/p&gt;

&lt;p&gt;A way I like to do this is by making use of &lt;a href="https://hackerrank.com" rel="noopener noreferrer"&gt;Hackerrank&lt;/a&gt;. If you don't know about them(which would be unlikely), they help you improve your coding skills by providing you with various coding challenges.&lt;/p&gt;

&lt;p&gt;When you perfect your programming fundamental, solving a complex problem would be a lot easier for you.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566743027737%2FQEzGoziAj.gif" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1566743027737%2FQEzGoziAj.gif" alt="ezgif-5-c401fd73493a.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  That's All Folks  🧠
&lt;/h1&gt;

&lt;p&gt;So that's all we have for now, I tried to make this as short and straight forward as possible. If you have better suggestions do put them in the comments below so others can learn.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>motivation</category>
      <category>career</category>
    </item>
    <item>
      <title>Setting Up A Node Typescript Project in Under 4 minutes</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Mon, 15 Jul 2019 22:00:31 +0000</pubDate>
      <link>https://dev.to/theghostyced/setting-up-a-node-typescript-project-in-under-4-minutes-4gk2</link>
      <guid>https://dev.to/theghostyced/setting-up-a-node-typescript-project-in-under-4-minutes-4gk2</guid>
      <description>&lt;p&gt;In recent years, &lt;a href="https://www.typescriptlang.org" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; has gained popularity and one of the main reason for its sudden growth has to be attributed to its &lt;a href="https://www.sitepoint.com/typing-versus-dynamic-typing/" rel="noopener noreferrer"&gt;strong static typing&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So in this blog post, we will be setting up a TSNode(Typescript + Node 🤓) application in less than 4 minutes(&lt;em&gt;excluding external factors like internet connection, etc&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Table Of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Project Setup&lt;/li&gt;
&lt;li&gt;Installing Dependencies&lt;/li&gt;
&lt;li&gt;Setup Required JSON file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NB&lt;/strong&gt; - Am assuming you already have node installed on your machine, if not then I recommend you &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;download and install it here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Setup
&lt;/h3&gt;

&lt;p&gt;So now that you have Node installed on your machine, let's create our project directory and initialize a new node project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;desired-folder

&lt;span class="nb"&gt;mkdir &lt;/span&gt;tsnode-project

npm init &lt;span class="nt"&gt;-y&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NB&lt;/strong&gt; - Supplying the -y flag i.e &lt;em&gt;npm init -y&lt;/em&gt; lets npm use its default answers when initializing a new project.&lt;/p&gt;

&lt;p&gt;Next, we need to set up our folder structure, which would look a little like this later on&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsnode-project
│   package.json
│   tsconfig.json    
│
└───src
│   │   index.ts
│   │
│   
└───node_modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So run the following command to generate the various folders and files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// tsnode-project

&lt;span class="nb"&gt;touch &lt;/span&gt;tsconfig.json

&lt;span class="nb"&gt;mkdir &lt;/span&gt;src &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;touch &lt;/span&gt;index.ts

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

&lt;/div&gt;



&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Dependencies
&lt;/h3&gt;

&lt;p&gt;Next, we install the necessary packages needed by typescript to work,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
npm i &lt;span class="nt"&gt;-D&lt;/span&gt; typescript 
npm i &lt;span class="nt"&gt;-D&lt;/span&gt; ts-node 
npm i &lt;span class="nt"&gt;-D&lt;/span&gt; nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Packages Explaination
&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;ts-node&lt;/strong&gt; - This package makes it possible to run typescript code without compiling it to Javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;nodemon&lt;/strong&gt; - Automatically restarts the application when file changes in the directory are detected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;typescript&lt;/strong&gt; - TypeScript compiles to readable, standards-based JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup TSConfig
&lt;/h3&gt;

&lt;p&gt;Now that we have the necessary packages installed, we can go ahead and set up our typescript config file. So open our  &lt;code&gt;tscofig.json&lt;/code&gt; and add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"es6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"commonjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"rootDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"outDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"sourceMap"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"resolveJsonModule"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"lib"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"es6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dom"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"include"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"src/**/*.ts"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"exclude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"node_modules"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/div&gt;



&lt;p&gt;If you want to know what each option mean, then head over to the &lt;a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html" rel="noopener noreferrer"&gt;typescript documentation&lt;/a&gt; to find out more but for now, I would only explain the &lt;code&gt;rootDir&lt;/code&gt; and &lt;code&gt;outDir&lt;/code&gt; options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;rootDir&lt;/strong&gt; - This simply refers to the directory in which the typescript compiler should look for all &lt;strong&gt;&lt;code&gt;.ts&lt;/code&gt;&lt;/strong&gt; files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;outDir&lt;/strong&gt; - This refers to the directory in which the compiled js files will be located. Feel free to change it to whatever you want.&lt;/p&gt;

&lt;p&gt;After adding the necessary lines, we can then add a &lt;strong&gt;&lt;code&gt;script&lt;/code&gt;&lt;/strong&gt; to our &lt;code&gt;package.json&lt;/code&gt; file which would &lt;em&gt;start our server&lt;/em&gt; and also &lt;em&gt;run a build on our application&lt;/em&gt;. The script would look something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc &amp;amp;&amp;amp; node dist/index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currently, our &lt;code&gt;index.ts&lt;/code&gt; file is empty so let's set up a &lt;a href="https://expressjs.com/" rel="noopener noreferrer"&gt;basic express server&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Run &lt;strong&gt;&lt;code&gt;npm i express -S&lt;/code&gt;&lt;/strong&gt; then add the following line to the &lt;strong&gt;&lt;code&gt;index.ts&lt;/code&gt;&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="c1"&gt;// index.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YAY! It actually works!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="nx"&gt;err&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;Now, we can run our server by running the command &lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Result
&lt;/h2&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzmwfisedhgfv5qu2bb95.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzmwfisedhgfv5qu2bb95.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  All Done
&lt;/h1&gt;

&lt;p&gt;And there you have it! A fully working Node TypeScript project in under 4 minutes. If for some weird reason it doesn't seem to work for you,  then you can check out the code here &lt;a href="https://github.com/theghostyced/tsnode-project-devto" rel="noopener noreferrer"&gt;https://github.com/theghostyced/tsnode-project-devto&lt;/a&gt;. Also feel free to extend the application to support &lt;strong&gt;&lt;code&gt;tslint&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;prettier&lt;/code&gt;&lt;/strong&gt;, etc.&lt;/p&gt;

&lt;p&gt;So am &lt;a href="https://twitter.com/theghostyced" rel="noopener noreferrer"&gt;CED&lt;/a&gt; and thanks for reading. If you found an issue with the post please do share.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>codenewbie</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How To Learn - What Most Schools 🎓 Don't Teach</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Wed, 05 Jun 2019 15:26:12 +0000</pubDate>
      <link>https://dev.to/theghostyced/how-to-learn-what-most-schools-don-t-teach-2p5f</link>
      <guid>https://dev.to/theghostyced/how-to-learn-what-most-schools-don-t-teach-2p5f</guid>
      <description>&lt;p&gt;I ran &lt;a href="https://twitter.com/AmarachiAmaechi/status/1135349501861027840"&gt;into a tweet by Amarachi Amaechi&lt;/a&gt; that talked about things schools don't necessarily teach you. Among every item listed, I realized that one more thing most schools don't teach is &lt;strong&gt;How To Learn&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In Nigeria(&lt;em&gt;my beloved country&lt;/em&gt; 😁), one of the most popular forms of learning is &lt;code&gt;La Cram La Pour La Forget&lt;/code&gt;. It basically refers to cramming, studying intensively over a short period of time just before an examination, then dishing out what was studied and completely forgetting it after the exam.&lt;/p&gt;

&lt;p&gt;I hated this form of learning(&lt;em&gt;If it can even be called that&lt;/em&gt;) and after dropping out of school, I sought a better means of learning. After &lt;a href="https://www.coursera.org/learn/learning-how-to-learn"&gt;taking various lessons and courses&lt;/a&gt;, I found a few ways of learning and decided to share.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;WHAT IS LEARNING?&lt;/strong&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;"Tell me and I forget, teach me and I may remember, involve me and I learn." - Benjamin Franklin&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Learning as defined by &lt;em&gt;Malcolm Knowles&lt;/em&gt;, is a means of gaining knowledge and expertise. &lt;/p&gt;

&lt;p&gt;We have all heard the popular phrase - &lt;em&gt;"If you stop learning, you start dying - Albert Einstein"&lt;/em&gt;, no words have ever been so true. When you cease to learn, you cease to grow and when you cease to grow, you cease to improve, get better and move forward. With time, you just begin to - exist.&lt;/p&gt;

&lt;p&gt;Before we can go into learning how to learn, we must first understand the organ that does the learning - &lt;strong&gt;THE BRAIN&lt;/strong&gt;. We need to under how thoughts are processed in the brain and the various forms of thinking.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;HOW THE BRAIN PROCESSES THOUGHT&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A thought is an idea or opinion produced by thinking or occurring suddenly in the mind.&lt;/p&gt;

&lt;p&gt;The brain is composed of &lt;a href="http://www.brainfacts.org/Brain-Anatomy-and-Function/Anatomy/2012/The-Neuron"&gt;over 50 billion neurons&lt;/a&gt; which it uses for communication. The brain is what it is because of the structural and functional properties of interconnected neurons known as - &lt;strong&gt;NEURAL NETWORKS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When you think or process a piece of information, the information is received by &lt;a href="https://reliawire.com/dendrites/"&gt;the neuron's dendrites&lt;/a&gt; and then processed in &lt;a href="https://study.com/academy/lesson/what-is-a-cell-body-definition-function-types.html"&gt;the cell body&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;After the processing is complete, the information is passed to other neurons for processing &lt;a href="https://www.britannica.com/science/synapse"&gt;via the synapse&lt;/a&gt; and the whole process is repeated over and over again.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;MODES OF THINKING&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Researchers found out that we have two fundamental modes of thinking, we have the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Focused or Concentrated mode&lt;/li&gt;
&lt;li&gt;Diffused or Disperse mode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Focused Mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A man went to the toy store to purchase some toys. He got a toy car and a toy truck, together they both cost &lt;em&gt;$1.10&lt;/em&gt; and the toy truck is &lt;em&gt;$1&lt;/em&gt; more than the toy car. What is the price of the toy car?&lt;/p&gt;

&lt;p&gt;After seeing this question, the first thing you might have probably done was to fully concentrate on the question and try to provide an answer - that's &lt;em&gt;&lt;strong&gt;Focused Thinking Mode&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;With focused thinking, you zero all your thought to a particular spot or location. It's sort of like a flashlight modified to shine only on a particular spot.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Diffused Mode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The diffuse mode is a more relaxed state of thinking. This is the mode or situation your brain is at whenever you either go for walks or a swim or even asleep. &lt;/p&gt;

&lt;p&gt;Think of it like blowing out a &lt;code&gt;dandelion flower&lt;/code&gt;. The movement of the petals in the wind can be considered as a sort of &lt;em&gt;&lt;strong&gt;Diffused thinking mode&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In this mode, your brain is left to wander and make unintended connections and understandings from different disciplines which allows for the creation of diverse and robust mental models.&lt;/p&gt;




&lt;p&gt;These 2 fundamentally different modes of thinking are complementary in our effort to learn and understand new knowledge. It is important to know that you can't do both at the same time. Rather, you need to consciously alternate.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;WAYS TO LEARN?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Learning, believe it or not, is really easy. It only takes dedication and repetition. With that said, here are 6 ways we learn:-&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;DISENGAGE YOUR MIND&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Previously, we made mention on the 2 fundamental modes of thinking(&lt;em&gt;focused and diffused&lt;/em&gt;). When they are trying to gain new knowledge in a particular field, most individuals usually make the mistake of staying in the focused mode of thinking especially when they are stuck with a particular problem.&lt;/p&gt;

&lt;p&gt;Unknown to them, if you keep going at a problem in the focused mode unsuccessfully for too long, you develop tunnel vision(&lt;em&gt;seeing only your own point of view&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;A better solution when you are stuck with a problem is to simply switch your mind to a diffused mode of thinking. That's why when most developers are stuck with a bug for a long time, the advice given by most people is - &lt;em&gt;"go to bed and you will be able to solve it in the morning"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Switching to a diffused mode of thinking allows the brain to make other widespread connection. After a few minutes in the diffused mode, you can then switch back to a focused mode of thinking.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;QUESTION EVERYTHING&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;You know that game we play where every statement made by someone is met with a sounding &lt;em&gt;"why"&lt;/em&gt; question? Well, here you would need to play that game with yourself.&lt;/p&gt;

&lt;p&gt;Questioning everything is a good way to learn. Whenever you come across something new or interesting to you, don't just brush it off as another advancement in technology(&lt;em&gt;in Nigeria most people say "Ndi Ocha", they have come again&lt;/em&gt; 😂).&lt;/p&gt;

&lt;p&gt; Instead, question what that technology does, question how it was made, question the technologies being used to achieve it. Soon enough, your brain would start trying to making connections and in turn, it begins to learn and search for answers.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;EXPLAIN IT&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;"If you can't explain it &lt;strong&gt;simply&lt;/strong&gt;, you don't understand it well enough." - Albert Einstein&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After learning something new, one neat trick you can use to cement the knowledge would be to explain it to someone else_(kinda like am doing now)_. &lt;/p&gt;

&lt;p&gt;By doing this, you are then triggering your brain to store &lt;a href="https://science.howstuffworks.com/life/inside-the-mind/human-brain/human-memory2.htm"&gt;the information in your long-term memory rather than the working memory.&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;EXERCISE&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;As a kid, I never knew the benefits of exercise. At that time, exercise to me was a means for guys to gain muscles in other to pick up girls(&lt;em&gt;thanks &lt;a href="https://www.youtube.com/watch?v=Ft1iE92rGBM"&gt;Johnny Bravo&lt;/a&gt;&lt;/em&gt; 💪).&lt;/p&gt;

&lt;p&gt;Exercise has been shown to cause the &lt;code&gt;hippocampus&lt;/code&gt;, the part of your brain tasked with the formation of new memories and associated with learning and emotions, to grow in size. This serves to increase mental function in older adults.&lt;/p&gt;

&lt;p&gt;Moreover, exercise increases your heart rate, which promotes the flow of blood and oxygen to your brain. So the more you exercise, the more your oxygen your brain gets.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;LEARN BY DOING&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;"You don't learn to walk by following rules. You learn by doing, and by falling over." ― Richard Branson&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This quote says it all, you don't learn to be a pro footballer by watching Messi play, you don't learn to be a great scientist by watching Sheldon 😅, you learn by taking actions.&lt;/p&gt;

&lt;p&gt;When you learn by doing, there is a higher chance of you to retain everything you have learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;CONCLUSION&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I tried to keep this article as short as possible, hopefully, you were able to finish it. So &lt;a href="https://twitter.com/theghostyced"&gt;am CED&lt;/a&gt; and ironically I'm a Software Developer 😅. &lt;/p&gt;

&lt;p&gt;Thanks for reading. See anything that needs correcting, do let me know in the comments below. Happy learning y'all 👋.&lt;/p&gt;

&lt;h5&gt;
  
  
  &lt;strong&gt;HELPFUL LINKS&lt;/strong&gt;
&lt;/h5&gt;

&lt;h6&gt;
  
  
  &lt;strong&gt;ARTICLES&lt;/strong&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://www.livestrong.com/article/202078-human-brain-thinking-process/"&gt;https://www.livestrong.com/article/202078-human-brain-thinking-process/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.themetalearners.com/how-to-utilize-both-brains-thinking-modes-focused-vs-diffuse/"&gt;https://www.themetalearners.com/how-to-utilize-both-brains-thinking-modes-focused-vs-diffuse/&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.brainfacts.org/diseases-and-disorders/mental-health/2018/what-a-good-nights-sleep-does-for-the-brain-103018"&gt;http://www.brainfacts.org/diseases-and-disorders/mental-health/2018/what-a-good-nights-sleep-does-for-the-brain-103018&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;strong&gt;COURSE&lt;/strong&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://www.coursera.org/learn/learning-how-to-learn"&gt;https://www.coursera.org/learn/learning-how-to-learn&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;strong&gt;YOUTUBE&lt;/strong&gt;
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=yxUkUaV2VPs"&gt;https://www.youtube.com/watch?v=yxUkUaV2VPs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=WTr12dK2Se0"&gt;https://www.youtube.com/watch?v=WTr12dK2Se0&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>career</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Algorithm Question By Google</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Tue, 04 Jun 2019 11:27:20 +0000</pubDate>
      <link>https://dev.to/theghostyced/algorithms-2d5g</link>
      <guid>https://dev.to/theghostyced/algorithms-2d5g</guid>
      <description>&lt;p&gt;&lt;strong&gt;QUESTION&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can determine how "out of order" an array A is by counting the number of inversions it has.&lt;/p&gt;

&lt;p&gt;Two elements A[i] and A[j] form an inversion if A[i] &amp;gt; A[j] but i &amp;lt; j. &lt;br&gt;
That is, a smaller element appears after a larger element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CHALLENGE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Given an array, count the number of inversions it has. Do this faster than O(N^2) time.&lt;/p&gt;

&lt;p&gt;You may assume each element in the array is distinct.&lt;/p&gt;

&lt;p&gt;For example, a sorted list has zero inversions. &lt;br&gt;
The array [2, 4, 1, 3, 5] has three inversions: (2, 1), (4, 1), and (4, 3). &lt;br&gt;
The array [5, 4, 3, 2, 1] has ten inversions: every distinct pair forms an inversion.&lt;/p&gt;

</description>
      <category>challenge</category>
      <category>algorithms</category>
      <category>google</category>
    </item>
    <item>
      <title>Tricks to get you coding 👨‍💻 again when you aren't motivated to.</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Wed, 29 May 2019 20:41:18 +0000</pubDate>
      <link>https://dev.to/theghostyced/tricks-to-get-you-coding-again-when-you-aren-t-motivated-to-1bdl</link>
      <guid>https://dev.to/theghostyced/tricks-to-get-you-coding-again-when-you-aren-t-motivated-to-1bdl</guid>
      <description>&lt;p&gt;Everyone has a passion for something, for some people its music, others could be playing video games and to some, it could be they love solving problems with code. Programming is hard, what makes it worthwhile is the passion we have for solving problems.&lt;/p&gt;

&lt;p&gt;What then happens if we lose that passion? 🤔&lt;/p&gt;

&lt;p&gt;A few years back, I lost sight of why I began coding and found out that I was often times forcing myself to try and code. After observing myself, I found a few methods I used to reignite my passion and get myself coding again.&lt;/p&gt;

&lt;p&gt;Those methods are what am about to share with you right now.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Tricks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Reflecting on your Goals🏅&lt;/li&gt;
&lt;li&gt;
Attending a Meetup🗣&lt;/li&gt;
&lt;li&gt;
Watching a Movie🍿&lt;/li&gt;
&lt;li&gt;
Joining a Community of Peers👥&lt;/li&gt;
&lt;li&gt;
Watching Anime🇯🇵&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Reflecting on your Goals &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Dreams are little nudges from your soul -- &lt;em&gt;Victoria Moran&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HvUXMjwD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mk0relationship4gjny.kinstacdn.com/wp-content/uploads/2014/01/reflection-time.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HvUXMjwD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mk0relationship4gjny.kinstacdn.com/wp-content/uploads/2014/01/reflection-time.jpg" alt="Man reflecting on his goals"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First off, everyone has a particular reason why they came into programming(&lt;em&gt;and yes even those that do it for the money&lt;/em&gt; 😏). The best way to reignite your motivation and start coding once again is to simply reflect on those goals you had while venturing into programming.&lt;/p&gt;

&lt;p&gt;It could be to &lt;strong&gt;start earning $250k/yr&lt;/strong&gt; on a job, it could be &lt;strong&gt;solving a dying problem&lt;/strong&gt; in your local community, it could even be &lt;strong&gt;working for one of the biggest tech companies out there&lt;/strong&gt;(&lt;em&gt;yeah I see you Google&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Whatever it is, reflect on it and remember why you began coding in the first place.&lt;/p&gt;




&lt;h3&gt;
  
  
  Attending a Meetup &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Another way to motivate yourself to code again would be attending meetups. Meetups are a lovely way of re-motivating yourself as you get to meet and interact with other developers who have designed and built awesome applications. Some even go as far as giving you career advice and paths to take as a developer.&lt;/p&gt;

&lt;p&gt;&lt;a href="//meetup.com"&gt;Meetup&lt;/a&gt; is an easy and straight forward platform to start looking for meetups to attend. Create an account on Meetup and within seconds, you are given a lot of interesting suggestions on various upcoming meetups.&lt;/p&gt;




&lt;h3&gt;
  
  
  Watching a Movie &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Movies are a great way to pass time or relax after a long day(&lt;em&gt;esp. via Netflix&lt;/em&gt;). So it would seem weird to think that you could actually be motivating yourself to code while watching a movie. Well, it all depends on the type of movie you watch.&lt;/p&gt;

&lt;p&gt;There are a lot of movies that could help motivate a developer to code, Silicon Valley, Mr. Robot, just to name a few. In order to keep this article as short as possible, I wrote another article on &lt;a href="https://medium.com/the-andela-way/10-movies-to-get-you-coding-again-3d463b1bd9af"&gt;&lt;code&gt;10 Movies to get you coding again&lt;/code&gt;&lt;/a&gt;. Any of those movies should definitely help get you motivated.&lt;/p&gt;




&lt;h3&gt;
  
  
  Joining a Community of Peers &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;A community is a simple way for you to connect with your peers or developers more experienced than you that have been in your shoes before. &lt;/p&gt;

&lt;p&gt;Just like attending a meetup, when you find yourself in a community of experienced developers, you get to learn first-hand about some awesome technologies and projects most of them work on daily. &lt;/p&gt;

&lt;p&gt;Reading about other people with similar experiences, who may be struggling with the same issues that you are, can be hugely encouraging and motivating.&lt;/p&gt;




&lt;h3&gt;
  
  
  Watching Anime &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Now, this is the silliest of all and that's why I left it last. Just hear me out for a second(&lt;em&gt;remember I said "&lt;strong&gt;tricks I use&lt;/strong&gt;"&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Seeing other's individuals achieve their goals is a sure way of getting or rekindling your motivation. Japanese anime is filled with situations like this. &lt;/p&gt;

&lt;p&gt;You see the protagonist fighting with everything they have till their dying breath until they reach their goal. That kind of determination usually fuels my motivation and gets me remembering my goals and all I said I would achieve and those thoughts get me coding again.&lt;/p&gt;

&lt;p&gt;I know it sounds silly 😂, but am telling you it actually works for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;So yeah, those were some of the tricks I use to get myself to code again.&lt;/p&gt;

&lt;p&gt;Have something you already do, do share in the comments below. Itching to find out new tricks.&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>⚡ ️Blazing Python 🐍 Scripts with Concurrency ⚡️️</title>
      <dc:creator>CED</dc:creator>
      <pubDate>Mon, 27 May 2019 23:26:40 +0000</pubDate>
      <link>https://dev.to/theghostyced/blazing-python-scripts-with-concurrency-59di</link>
      <guid>https://dev.to/theghostyced/blazing-python-scripts-with-concurrency-59di</guid>
      <description>&lt;p&gt;To ensure the high performance of an application, programmers need to write efficient code. Code efficiency is directly linked with algorithmic efficiency and the speed of runtime execution for software.&lt;/p&gt;

&lt;p&gt;The 🐍 &lt;a href="https://www.huffpost.com/entry/computer-programming-languages-why-c-runs-so-much_b_59af8178e4b0c50640cd632e?guccounter=1&amp;amp;guce_referrer=aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS8&amp;amp;guce_referrer_sig=AQAAAIPUUFGtMUrtIlSm2M6P6eg2CjZDE-OL4Gpn1rQXa6X6jwj7N9U5bY0uLxMoOKMlddh_fZUiLy1o6T2VnD0luBVHhyDFf8FBX2axU1a7C-IXGYRGjX3CC4U1VThstAf-ztgbBajuWIBuS18MducDdiTdkjwYrChPyIoETRRxUec6"&gt;Python language is a 🐌 slow language - compared to C or FORTRAN&lt;/a&gt; and to combat this, various methods have been developed to help speed up the programming language.&lt;/p&gt;

&lt;p&gt;One method is &lt;code&gt;CONCURRENCY&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CONCURRENCY&lt;/strong&gt; 🤷‍♂️&lt;/p&gt;

&lt;p&gt;Concurrency is when two tasks can start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant(&lt;em&gt;that the becomes &lt;a href="https://wiki.haskell.org/Parallelism_vs._Concurrency"&gt;&lt;code&gt;Parallelism&lt;/code&gt;&lt;/a&gt;&lt;/em&gt;). Still confused 😕? Let's paint a scenario:&lt;/p&gt;

&lt;p&gt;We are planning a dream wedding for a particular couple. At our disposal, we have Mary, Susan, Max, and Simon. A cake, band, decorations, and invitation cards are needed for the dream wedding. We assign the baking of the cake to Susan, the hiring of a band to Simon, the setting up of decorations to Mary and the sending of invitations to Max.&lt;/p&gt;

&lt;p&gt;These four friends(&lt;em&gt;or processors&lt;/em&gt;) all perform their tasks(&lt;em&gt;or processes&lt;/em&gt;) at the same time without task switching or interruptions until it is completed. This is - &lt;em&gt;in layman's&lt;/em&gt; term referred to as &lt;strong&gt;CONCURRENCY&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TYPES OF CONCURRENCY IN PYTHON&lt;/strong&gt; 🐍&lt;/p&gt;

&lt;p&gt;The basic types of concurrency in Python include:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;multi-threading 🧵&lt;/li&gt;
&lt;li&gt;multiprocessing 🧩(&lt;em&gt;yeah, I know that's a jigsaw piece&lt;/em&gt; 😏)&lt;/li&gt;
&lt;li&gt;asyncio ⏰ (&lt;em&gt;more on this in another tutorial&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;MULTI-THREADING&lt;/strong&gt; 🧵&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Thread&lt;/code&gt; is the &lt;em&gt;smallest&lt;/em&gt; unit of execution in an operating system. Threads are a way for a program to split itself into two or more simultaneously running tasks. A thread is not itself a program, it only runs within a particular program or &lt;code&gt;process&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GUD0sp9M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AvZ_RTovr8GW1y7bHBiRQ_A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GUD0sp9M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2AvZ_RTovr8GW1y7bHBiRQ_A.png" alt="multithreading example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Multi-threading is game-changing and is mainly used for &lt;a href="https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean"&gt;I/O bound operations&lt;/a&gt;. It is the ability of a &lt;a href="https://en.wikipedia.org/wiki/Central_processing_unit"&gt;central processing unit (CPU)&lt;/a&gt; (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. Each thread shares the same resource supplied by the process it exists in.&lt;/p&gt;

&lt;p&gt;Let's illustrate an example of a multi-threaded operation. First, we take a look at the synchronous process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronous Process&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# A simple python script that gets query a list of site
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Read &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;url&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.google.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.facebook.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.twitter.com/theghostyced"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

    &lt;span class="n"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Downloaded &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;end_time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; seconds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a simple python program that downloads the content of the provided websites. After downloading the content of the website, it then prints out the number of sites visited and the time it took.&lt;br&gt;
The script makes use of the &lt;a href="https://2.python-requests.org//en/master/"&gt;&lt;code&gt;requests&lt;/code&gt;&lt;/a&gt; library and the built-in python standard time library.&lt;/p&gt;

&lt;p&gt;The output of running the code is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
Read 107786 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107786 from https://www.facebook.com
Read 608077 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107787 from https://www.facebook.com
Read 608077 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107351 from https://www.facebook.com
Read 608311 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107507 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107918 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107149 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11365 from https://www.google.com
Read 107445 from https://www.facebook.com
Read 608077 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107351 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11369 from https://www.google.com
Read 107482 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Downloaded 90 in 17.5553081035614 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, it takes the script 17.5 seconds to complete the task. Now let's try it again and see if we can speed it up using a multi-threaded approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multi Threaded Process&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# A simple python script that gets query a list of site
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;concurrent.futures&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Read &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;url&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;concurrent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;futures&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ThreadPoolExecutor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_workers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;executor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# our scripts start time
&lt;/span&gt;    &lt;span class="n"&gt;sites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.google.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.facebook.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.twitter.com/theghostyced"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

    &lt;span class="n"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Downloaded &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;end_time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; seconds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the code above, we imported the &lt;a href="https://docs.python.org/3.4/library/concurrent.futures.html?highlight=concurrent.futures#module-concurrent.futures"&gt;&lt;code&gt;concurrent.futures&lt;/code&gt; module&lt;/a&gt; from the Python Standard library. The module has an Executor class which is where the &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; subclass comes from. Let's break down the &lt;code&gt;ThreadPoolExecutor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All the &lt;code&gt;ThreadPoolExecutor&lt;/code&gt; subclass is doing is simply creating a &lt;a href="https://docs.python.org/3.4/library/multiprocessing.html?highlight=pool#module-multiprocessing.pool"&gt;&lt;code&gt;Pool&lt;/code&gt;&lt;/a&gt; of Threads. The Executor part, controls how and when each of the threads in the pool will run.&lt;/p&gt;

&lt;p&gt;The output of the above scripts is shown below:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
Read 608312 from https://www.twitter.com/theghostyced
Read 11354 from https://www.google.com
Read 107810 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11343 from https://www.google.com
Read 107823 from https://www.facebook.com
Read 608312 from https://www.twitter.com/theghostyced
Read 11326 from https://www.google.com
Read 107388 from https://www.facebook.com
Read 11350 from https://www.google.com
Read 608312 from https://www.twitter.com/theghostyced
Read 107787 from https://www.facebook.com
Read 608311 from https://www.twitter.com/theghostyced
Read 608077 from https://www.twitter.com/theghostyced
Read 11299 from https://www.google.com
Read 11367 from https://www.google.com
Read 608312 from https://www.twitter.com/theghostyced
Read 107785 from https://www.facebook.com
Read 11321 from https://www.google.com
Read 107800 from https://www.facebook.com
Read 107350 from https://www.facebook.com
Read 608076 from https://www.twitter.com/theghostyced
Read 608312 from https://www.twitter.com/theghostyced
Read 608312 from https://www.twitter.com/theghostyced
Read 608311 from https://www.twitter.com/theghostyced
Downloaded 90 in 6.443061351776123 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, it takes the script 6.4 seconds to complete the task. Compared to the 17.5 seconds it took when running the code synchronously. You could be saying to yourself - it's only a 12 seconds difference, I can live with that. Imagine we had a larger amount of data, say 1000, then you would significantly see the difference in both approaches.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;MULTIPROCESSING&lt;/strong&gt; 🧩&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Process&lt;/strong&gt; is simply an instance of a running program. To put it in layman's terms, when we write our computer programs/script in a text file and execute this program, it becomes a process that performs all the tasks mentioned in the program/script. A &lt;code&gt;Process&lt;/code&gt; does not share any memory space with other processes like &lt;code&gt;Threads&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Multiprocessing involves the use of two or more cores within a single computer system. By default, multithreading is not possible  with the Python 🐍 programming language due to the &lt;a href="https://dev.to/docoprusta/explain-python-global-interpreter-lock-gil-like-im-five-25k8"&gt;&lt;code&gt;GIL or Global Interpreter Lock hindrance&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The GIL Hindrance
&lt;/h2&gt;

&lt;p&gt;Python was developed by Guido van Rossum in the 1980s and at that time, computers only made use of a single CPU and to increase memory management, Guido implemented the GIL which allows only one thread to hold the control of the Python interpreter. Meaning, making use of more than one CPU core or separate CPUs to run threads in parallel was not possible.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.python.org/3.4/library/multiprocessing.html"&gt;&lt;code&gt;multiprocessing module&lt;/code&gt;&lt;/a&gt; was introduced in order to bypass this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NB&lt;/strong&gt; -- &lt;em&gt;The GIL does not prevent the creation of multiple threads. All the GIL does is make sure only one thread is executing Python code at a time; control still switches between threads. If you still having confusions, &lt;a href="https://dev.to/docoprusta/explain-python-global-interpreter-lock-gil-like-im-five-25k8"&gt;&lt;code&gt;this article will definitely help you out&lt;/code&gt;&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's illustrate how a multi-processing operation would be written using the synchronous code we have above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronous Process&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# A simple python script that gets query a list of site
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Read &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;url&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;urls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.google.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.facebook.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.twitter.com/theghostyced"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

    &lt;span class="n"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Downloaded &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;end_time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; seconds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Multiprocessing Method&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# A simple python script that gets query a list of site
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;multiprocessing&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Read &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;url&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;multiprocessing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;get_single_site&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;start_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# our scripts start time
&lt;/span&gt;    &lt;span class="n"&gt;sites&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.google.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.facebook.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"https://www.twitter.com/theghostyced"&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

    &lt;span class="n"&gt;get_all_sites&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end_time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start_time&lt;/span&gt;

    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"Downloaded &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sites&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;end_time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; seconds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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



&lt;p&gt;Here we import the multiprocessing package from Python's standard library. The multiprocessing module comes with a few sub-classes Process and Pool.&lt;/p&gt;

&lt;p&gt;Here we are making use of the &lt;code&gt;Pool&lt;/code&gt; subclass. The &lt;code&gt;Pool&lt;/code&gt; takes the number of workers or processes it needs to spawn as its first argument which is what is happening on &lt;code&gt;with multiprocessing.Pool(5) as pool:&lt;/code&gt; line. On &lt;code&gt;pool.map(get_single_site, sites)&lt;/code&gt; we use the map method that is provided to the Pool . The method takes in the function that needs to be called as the first argument and the iterable which is our urls list as the second. It then chops the iterable into a number of chunks which it submits to the process pool as separate tasks.&lt;/p&gt;

&lt;p&gt;The output for this given operation is:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
Read 608423 from https://www.twitter.com/theghostyced
Read 108078 from https://www.facebook.com
Read 11386 from https://www.google.com
Read 11387 from https://www.google.com
Read 11304 from https://www.google.com
Read 11353 from https://www.google.com
Read 108021 from https://www.facebook.com
Read 107985 from https://www.facebook.com
Read 108022 from https://www.facebook.com
Read 608423 from https://www.twitter.com/theghostyced
Read 108079 from https://www.facebook.com
Read 608423 from https://www.twitter.com/theghostyced
Read 11340 from https://www.google.com
Read 608423 from https://www.twitter.com/theghostyced
Read 11321 from https://www.google.com
Read 608423 from https://www.twitter.com/theghostyced
Read 107985 from https://www.facebook.com
Read 608423 from https://www.twitter.com/theghostyced
Read 11384 from https://www.google.com
Read 107549 from https://www.facebook.com
Read 608423 from https://www.twitter.com/theghostyced
Read 11294 from https://www.google.com
Read 608423 from https://www.twitter.com/theghostyced
Read 107985 from https://www.facebook.com
Read 11360 from https://www.google.com
Read 609124 from https://www.twitter.com/theghostyced
Downloaded 90 in 6.056399154663086 seconds
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, it takes the script 6 seconds to complete the task and just slightly faster than the threading solution. This is justified because the kind of operation being done is an I/O bounded on. Multiprocessing performs better when it does CPU bound operations like crunching some huge amount of data numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;I know right now you are itching to try it out for yourself so before you go, a few notes on when to use concurrency.&lt;/p&gt;

&lt;p&gt;You need to figure out if your program is &lt;a href="https://stackoverflow.com/questions/868568/what-do-the-terms-cpu-bound-and-i-o-bound-mean"&gt;CPU-bound or I/O-bound&lt;/a&gt; first. Remember that I/O-bound programs are those that spend most of their time waiting for something to happen(making external calls or requests) while CPU-bound programs spend their time processing data or crunching numbers as fast as they can.&lt;/p&gt;

&lt;p&gt;So for I/O-bound operation, MultiThreading would be the best way to go and for CPU-bound operations, Multiprocessing would be the right way to go.&lt;/p&gt;




&lt;p&gt;👋 👋 👋 👋&lt;br&gt;
I'm &lt;a href="https://twitter.com/theghostyced"&gt;CED&lt;/a&gt; and I hope you enjoyed the tutorial on how to speed up the runtime of your Python 🐍 scripts.&lt;/p&gt;

</description>
      <category>python</category>
      <category>concurrency</category>
      <category>multiprocessing</category>
      <category>multithreading</category>
    </item>
  </channel>
</rss>
