<?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: Santiago Rincón</title>
    <description>The latest articles on DEV Community by Santiago Rincón (@thehomelessdev).</description>
    <link>https://dev.to/thehomelessdev</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%2F343066%2Fa5ec3447-a722-46e7-957e-e94c70cc0870.jpg</url>
      <title>DEV Community: Santiago Rincón</title>
      <link>https://dev.to/thehomelessdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thehomelessdev"/>
    <language>en</language>
    <item>
      <title>From Chaos to Clarity: Help Us Build an Open-Source Brainstorm Engine</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Mon, 28 Jul 2025 19:40:01 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/from-chaos-to-clarity-help-us-build-an-open-source-brainstorm-engine-eoa</link>
      <guid>https://dev.to/thehomelessdev/from-chaos-to-clarity-help-us-build-an-open-source-brainstorm-engine-eoa</guid>
      <description>&lt;p&gt;Every dev has a system for notes.&lt;br&gt;
Mine was 40+ GPT chats, folders full of screenshots, and zero follow-through.&lt;/p&gt;

&lt;p&gt;So I built something to fix it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Meet &lt;code&gt;evolving_ideas&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;evolving_ideas&lt;/code&gt; is the first CLI tool in the Trivox ecosystem: a growing suite of automation tools for devs and creators who live at the intersection of code and creativity.&lt;/p&gt;

&lt;p&gt;This first tool helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Capture raw thoughts quickly (via prompt-based templates)&lt;/li&gt;
&lt;li&gt;Evolve ideas with guided iterations&lt;/li&gt;
&lt;li&gt;Version your brainstorms like code&lt;/li&gt;
&lt;li&gt;Turn ideas into TDDs, R&amp;amp;D plans, or creative briefs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All from your terminal.&lt;br&gt;
All open-source.&lt;br&gt;
Built for real-world workflows, not theory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why We're Building This
&lt;/h2&gt;

&lt;p&gt;Creative chaos is Thanos, inevitable, but organizing it shouldn't slow you down.&lt;/p&gt;

&lt;p&gt;Trivox is for developers and creatives who build things that don’t fit into Jira or Notion.&lt;/p&gt;

&lt;p&gt;It’s for people who jump between code, AI, content, and experiments, and need tools that grow with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Join the Build
&lt;/h2&gt;

&lt;p&gt;We’re looking for contributors who:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Love improving developer workflows&lt;/li&gt;
&lt;li&gt;Are into CLI tools, automation, and Python&lt;/li&gt;
&lt;li&gt;Enjoy turning fuzzy ideas into structured systems&lt;/li&gt;
&lt;li&gt;Want to grow with a project from the ground up&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Beginner-friendly issues are tagged, and we’ll support you with context, mentorship, and community.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔗 Links
&lt;/h2&gt;

&lt;p&gt;Repo on GitHub: &lt;a href="https://github.com/trivox/evolving-ideas" rel="noopener noreferrer"&gt;github.com/trivox/evolving-ideas&lt;/a&gt;&lt;br&gt;
Org Website: &lt;a href="https://trivox.io" rel="noopener noreferrer"&gt;trivox.io&lt;/a&gt;&lt;br&gt;
Docs: &lt;a href="https://docs.trivox.io/evolving_ideas" rel="noopener noreferrer"&gt;docs.trivox.io&lt;/a&gt;&lt;br&gt;
Discord/Contact: &lt;a href="https://discord.gg/ER9Sryjd" rel="noopener noreferrer"&gt;discord.gg/ER9Sryjd&lt;/a&gt;&lt;br&gt;
Good First Issues: [github.com/trivox-io/evolving-ideas/issues](&lt;a href="https://github.com/trivox-io/evolving-ideas/issues?q=state%3Aopen%20label%3A%22good%20first%20issue%22" rel="noopener noreferrer"&gt;https://github.com/trivox-io/evolving-ideas/issues?q=state%3Aopen%20label%3A%22good%20first%20issue%22&lt;/a&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Help Us Grow
&lt;/h2&gt;

&lt;p&gt;If you like what we’re building, we'd love your feedback, stars, shares, or memes.&lt;/p&gt;

&lt;p&gt;Let’s bring clarity to the chaos.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>python</category>
      <category>ai</category>
      <category>contributorswanted</category>
    </item>
    <item>
      <title>Recursion in Javascript</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Wed, 24 Jan 2024 14:00:00 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/recursion-in-javascript-50nm</link>
      <guid>https://dev.to/thehomelessdev/recursion-in-javascript-50nm</guid>
      <description>&lt;h2&gt;
  
  
  Have you ever found yourself needing to loop through a complex multidimensional object in JavaScript and didn't know how to do it? If this is the case, you should consider using Google as a powerful tool to find a solution. However, since you're here, if you keep reading, you may find an elegant solution to this problem.
&lt;/h2&gt;

&lt;p&gt;Let's start by taking the following tree as an example.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mySuperComplexTree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child-1-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child-1-1-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child-2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&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;&lt;strong&gt;The problem:&lt;/strong&gt; You have to print to the console each one of the node names, including the root.&lt;/p&gt;

&lt;p&gt;The first time we come across these kinds of problems, we tend to get ourselves led by the Dunning-Kruger effect and think that this can be solved with a simple for loop. The short answer is yes... But also, no, that's not always the best approach.&lt;/p&gt;

&lt;p&gt;Let's see what happens when we follow this approach:&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="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="nx"&gt;mySuperComplexTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;for &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;node&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;mySuperComplexTree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for &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;node2&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;node2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;for &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;node3&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;node2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;node3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// root&lt;/span&gt;
&lt;span class="c1"&gt;// child-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-1-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-1-1-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/uVtAU2EKHrsgifowFb/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/uVtAU2EKHrsgifowFb/giphy.gif" title="Google" alt="Alt Google" width="400" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you might see, even though it worked, there are several issues with this solution:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First of all, it's not easy to read. &lt;strong&gt;Nobody wants to see a bunch of nested loops on their code&lt;/strong&gt; and you're a team player, right?&lt;/li&gt;
&lt;li&gt;What will happen if this tree is supposed to change during runtime? The current implementation is not dynamic, so you must modify the code and add more nested loops manually. This can make the code harder to read and less maintainable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Here's where recursion comes in handy.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what's a recursive function anyway?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xThuWu82QD3pj4wvEQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xThuWu82QD3pj4wvEQ/giphy.gif" title="Recursion" alt="Alt Recursion" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is a function that calls itself during its execution&lt;/strong&gt;. This characteristic allows it to be used to solve problems that can be broken down into smaller and simpler sub-problems that are identical to the overall problem.&lt;/p&gt;

&lt;p&gt;An easy way to visualize this would be a countdown function using recursion. Let's see.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countDownFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&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;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&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="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;countDownFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;countDownFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;countDownFrom&lt;/code&gt; &lt;strong&gt;function prints a number, then calls itself (recursion)&lt;/strong&gt; with the number you passed, minus one, repeating this process &lt;strong&gt;until it reaches a base case&lt;/strong&gt; (in this case, when n is less than 0).&lt;/p&gt;

&lt;p&gt;As you can see, it's basically a loop but simpler and more elegant.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's go back to our tree object.
&lt;/h2&gt;

&lt;p&gt;Our initial problem is that we need to print all the node names of the tree, and we are going to use recursion to solve it because we noticed our tree has a constant structure where each node has a &lt;code&gt;name&lt;/code&gt; property (the string we are going to print), and a &lt;code&gt;children&lt;/code&gt; property (an array of nodes).&lt;/p&gt;

&lt;p&gt;So, our function needs to receive a node and print its name, then as all of the children are similar, we can loop one time through the children and just call the same function passing the child. This will receive a doe, print its name a repeat the process.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printNodeNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for &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;node&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;tree&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;printNodeNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;printNodeNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mySuperComplexTree&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Output:&lt;/span&gt;
&lt;span class="c1"&gt;// root&lt;/span&gt;
&lt;span class="c1"&gt;// child-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-1-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-1-1-1&lt;/span&gt;
&lt;span class="c1"&gt;// child-2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the &lt;code&gt;printNodeNames&lt;/code&gt; function solves our initial problem.&lt;/p&gt;

&lt;p&gt;It starts by printing the name of the current node (which is passed as an argument). Then, it loops through the children's array of the current node, and for each child node, it calls itself (recursion, again), passing the child node as the new argument. This process continues until it has printed all node names in the tree. Just what we said we needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This function demonstrates a common pattern for traversing a tree structure&lt;/strong&gt;: processing the current node (in this case, printing its name), then recursively processing all child nodes.&lt;/p&gt;

&lt;p&gt;This technique is known as a &lt;strong&gt;depth-first traversal&lt;/strong&gt; because it goes as deep as possible into the tree before backtracking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why to use recursion
&lt;/h2&gt;

&lt;p&gt;If for some reason, you're not convinced yet about using recursive functions, I'm going to leave you with four reasons why recursive functions are so useful.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity:&lt;/strong&gt; Recursion is often more straightforward to understand than its iterative counterparts (for, while, etc). They can turn complex tasks into much simpler ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Problem Solving:&lt;/strong&gt; Some problems are inherently recursive, like tree traversals, Tower of Hanoi, etc. For such problems, it is easier to use a recursive function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Divide and Conquer:&lt;/strong&gt; Recursive functions allow you to break down larger problems into smaller, more manageable sub-problems. This is the basis of many efficient algorithms like merge sort and quick sort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Less Code:&lt;/strong&gt; Recursive functions can lead to less code. Ok, shorter code doesn't necessarily mean better, but it can make the code more readable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In conclusion, Recursion is not only an elegant solution for some problems but it is a powerful and useful tool for tasks that can be broken down into smaller ones, such as traversing a tree or sorting a list.&lt;/p&gt;

&lt;p&gt;Their key components are &lt;strong&gt;the base case&lt;/strong&gt; (the condition under which the function stops calling itself) and &lt;strong&gt;the recursive case&lt;/strong&gt; (the part of the function that calls itself).&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;they can be more difficult to understand and debug than iterative solutions&lt;/strong&gt;, and &lt;strong&gt;they can also lead to performance issues such as stack overflow if not implemented carefully.&lt;/strong&gt; Therefore, &lt;strong&gt;it's important to use recursion judiciously and ensure that the base case is always reachable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this post useful and please let me know if you have used recursive functions before for a real-life scenario.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Callback functions in Javascript explained</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Sun, 12 Mar 2023 18:24:13 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/callback-functions-in-javascript-explained-3p3p</link>
      <guid>https://dev.to/thehomelessdev/callback-functions-in-javascript-explained-3p3p</guid>
      <description>&lt;p&gt;Imagine you are leading a development team responsible for creating a website, and you've found that the only remaining task for your team is to add a logo to the website.&lt;/p&gt;

&lt;p&gt;However, as a team of developers, you guys lack the necessary skills to design a professional logo. Thus, you must reach out to the design team to get the piece of art. Once the design team has provided the logo, you can then assign the task of adding it to the website to one of your developers.&lt;/p&gt;

&lt;p&gt;It's currently 2 PM on a Friday and you're eagerly waiting for the design team to provide the necessary design. Nonetheless, as time passes, you become increasingly frustrated with the delay. Finally, at 4:30 PM, the design team delivers the art, and now you're able to assign the task to a developer. However, due to the delay, it is now 6 PM, and the team is frustrated that they could not complete the work earlier.&lt;/p&gt;

&lt;p&gt;Now imagine instead of waiting for the design team to deliver before assigning the task to a developer, you could have asked the design team to provide the logo directly to the developer responsible for adding it to the website. That could have saved you time.&lt;/p&gt;

&lt;p&gt;This way, the developer could have started working on the container and had everything ready to add the design as soon as it was received. Everyone's happy. This is a callback function, and it's such a magical thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  So what exactly are callback functions?
&lt;/h3&gt;

&lt;p&gt;In simple terms, a callback function is just a function you can pass to another function for it to be invoked after performing another action.&lt;/p&gt;

&lt;p&gt;The most interesting thing about them is that we use them all the time without even realizing it. Here are some examples:&lt;/p&gt;

&lt;h4&gt;
  
  
  Event handling
&lt;/h4&gt;

&lt;p&gt;In web development, &lt;strong&gt;callback functions are commonly used to handle events&lt;/strong&gt;: mouse clicks, key presses, form submissions, and so on. Event listeners are attached to HTML elements and triggered when the corresponding event occurs. The callback function is then executed, allowing the program to respond to the event.&lt;/p&gt;

&lt;p&gt;I'm sure you've used something like this.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Button clicked!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Asynchronous operations
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Callback functions are often used in JavaScript to handle asynchronous operations&lt;/strong&gt;, for example: making an HTTP request. The callback function is passed to the asynchronous function, and it is called when the data is fetched.&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="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://pokeapi.co/api/v2/pokemon/ditto&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// callback function executed when the HTTP request is complete&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// callback function executed when the response is converted to JSON&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="nx"&gt;data&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;h4&gt;
  
  
  Array iteration
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;The &lt;code&gt;Array.prototype.forEach()&lt;/code&gt;, &lt;code&gt;Array.prototype.map()&lt;/code&gt;, etc. methods are examples of built-in JavaScript functions that take a callback function as an argument&lt;/strong&gt;. These functions are used to iterate through an array and perform some operation for each element.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="c1"&gt;// forEach method with callback function&lt;/span&gt;
&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// map method with callback function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&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;In all of these examples, &lt;strong&gt;callback functions allow programs to be more flexible and responsive to changing conditions&lt;/strong&gt;. They allow us to write code that can respond to events or data as they become available, and &lt;strong&gt;provide a way to separate concerns in our code, making it more modular, scalable, and easier to maintain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The best part of it is that you can implement them yourself. Let's take &lt;code&gt;Array.prototype.map()&lt;/code&gt; to see &lt;strong&gt;calback functions in detail&lt;/strong&gt;. This method takes a callback function as an argument and returns a new array with the result of calling the callback function on each element of the original array.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&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="nx"&gt;squaredNumbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 4, 9, 16, 25]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have an array of numbers, and we want to create a new array with the squared values of each number. We can achieve this by calling the &lt;code&gt;map()&lt;/code&gt; method on the numbers array and passing a callback function that takes a single argument (the current element of the array) and returns the squared value of that element.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;map()&lt;/code&gt; method then iterates through each element of the original array and applies the callback function to each element. The result of each call to the callback function is stored in a new array, which is returned once the iteration is done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But, how does it work internally?&lt;/strong&gt; Let's see a simplified example of how the &lt;code&gt;map()&lt;/code&gt; method works.&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="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&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;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="c1"&gt;// create a new array to store the result&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// iterate over each element of the original array&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// call the callback function on each element&lt;/span&gt;
    &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// add the result to the new array&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;newArray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// return the new array with the results&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this implementation, we define the &lt;code&gt;map()&lt;/code&gt; method on the &lt;code&gt;Array.prototype&lt;/code&gt; object, which allows us to call the method on any array object. The &lt;code&gt;callback&lt;/code&gt; parameter is the function that will be called on each element of the array.&lt;/p&gt;

&lt;p&gt;Inside the method, we create an empty array &lt;code&gt;newArray&lt;/code&gt; to store the results of calling the &lt;code&gt;callback&lt;/code&gt; function on each element. We then use a &lt;code&gt;for&lt;/code&gt; loop to iterate over each element of the original array.&lt;/p&gt;

&lt;p&gt;On each iteration, we call the &lt;code&gt;callback&lt;/code&gt; function with three arguments: the current element of the array &lt;code&gt;this[i]&lt;/code&gt;, the index of the current element &lt;code&gt;i&lt;/code&gt;, and the original array &lt;code&gt;this&lt;/code&gt;. The &lt;code&gt;callback&lt;/code&gt; function can use these arguments to transform the current element of the array and then return the transformed value.&lt;/p&gt;

&lt;p&gt;We then add the result of the &lt;code&gt;callback&lt;/code&gt; function to the &lt;code&gt;newArray&lt;/code&gt; using the &lt;code&gt;push()&lt;/code&gt; method, and finally, we return the &lt;code&gt;newArray&lt;/code&gt; with the results of calling the &lt;code&gt;callback&lt;/code&gt; function on each element of the original array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let's try to turn the metaphor at the beginning of this post into code with and without callback functions&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  With callback function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example with callback functions for website development process&lt;/span&gt;

&lt;span class="c1"&gt;// Assume we have a function that represents the development team&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;developmentTeam&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Development team is ready to add the illustration to the website&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="c1"&gt;// Assume we have a function that represents the design team&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;designTeam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Design team is working on the illustration...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Design team finished the illustration!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Simulate a delay of 1.5 seconds to complete the illustration&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Assume we have a function that represents the implementation task&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;implementationTask&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer added the illustration to the website&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="c1"&gt;// Assume we want to coordinate between the development and design teams to complete the website&lt;/span&gt;
&lt;span class="nf"&gt;developmentTeam&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;designTeam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;implementationTask&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have three functions: &lt;code&gt;developmentTeam()&lt;/code&gt;, &lt;code&gt;designTeam(callback)&lt;/code&gt;, and &lt;code&gt;implementationTask()&lt;/code&gt;. The first function represents the development team, while the second function represents the design team. The &lt;code&gt;designTeam(callback)&lt;/code&gt; function takes a callback function as an argument, which represents the implementation task that the development team needs to perform.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;designTeam(callback)&lt;/code&gt; function, we simulate a delay of 1.5 seconds to represent the time it takes for the design team to complete the design.&lt;/p&gt;

&lt;p&gt;Once the design is completed, we call the &lt;code&gt;callback&lt;/code&gt; function to notify the development team that they can proceed with the implementation task. Finally, we call &lt;code&gt;developmentTeam()&lt;/code&gt; to indicate that the development team is ready to add the illustration to the website.&lt;/p&gt;

&lt;p&gt;By using a callback function, we can ensure that the implementation task is only executed once the design team has completed the illustration. This helps to streamline the development process and avoid frustration on the development team.&lt;/p&gt;

&lt;h4&gt;
  
  
  Now without callback function
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example without callback functions for website development process&lt;/span&gt;

&lt;span class="c1"&gt;// Assume we have a function that represents the development team&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;developmentTeam&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Development team is ready to add the illustration to the website&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="c1"&gt;// Assume we have a function that represents the design team&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;designTeam&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Design team is working on the illustration...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Design team finished the illustration!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;implementationTask&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Simulate a delay of 1.5 seconds to complete the illustration&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Assume we have a function that represents the implementation task&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;implementationTask&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Developer added the illustration to the website&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="c1"&gt;// Assume we want to coordinate between the development and design teams to complete the website&lt;/span&gt;
&lt;span class="nf"&gt;developmentTeam&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;designTeam&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have the same three functions: &lt;code&gt;developmentTeam()&lt;/code&gt;, &lt;code&gt;designTeam()&lt;/code&gt;, and &lt;code&gt;implementationTask()&lt;/code&gt;. However, instead of passing a callback function to &lt;code&gt;designTeam()&lt;/code&gt;, we call &lt;code&gt;implementationTask()&lt;/code&gt; directly from within &lt;code&gt;designTeam()&lt;/code&gt; once the deign is complete.&lt;/p&gt;

&lt;p&gt;While this approach may work in some cases, it can lead to problems if the implementation task depends on other tasks that need to be completed before it. In such cases, it may be more difficult to manage the dependencies and ensure that tasks are executed in the correct order.&lt;/p&gt;

&lt;p&gt;In general, &lt;strong&gt;using callback functions can help you simplify the development process and make it easier to manage complex dependencies between functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In conclusion, callback functions are an essential feature in JavaScript that allows developers to write asynchronous, event-driven code, among other things. By passing a function as an argument to another function, the callback function can be executed once the first function has completed its task. This allows us, developers, to write more efficient and maintainable code, as &lt;strong&gt;they can avoid blocking the execution of code while waiting for long-running operations to complete&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callback functions are used extensively for many purposes, like handling user input, making HTTP requests, and working with timers&lt;/strong&gt;. They are also commonly used in modern web development frameworks, such as Express and React, to handle asynchronous operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;While callback functions can be a powerful tool in a developer's toolbox, it's essential to use them carefully and understand the potential pitfalls&lt;/strong&gt;. For example, callback functions can lead to callback hell, where the code becomes difficult to read and manage due to excessive nested callbacks. Developers should also consider using other techniques, such as promises or async/await, to manage complex dependencies between functions.&lt;/p&gt;

&lt;p&gt;Overall, callback functions are a crucial part of JavaScript, and every developer should understand how to use them effectively to write efficient, scalable, and maintainable code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why to keep it simple</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Mon, 07 Nov 2022 16:35:49 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/why-to-keep-it-simple-mk6</link>
      <guid>https://dev.to/thehomelessdev/why-to-keep-it-simple-mk6</guid>
      <description>&lt;p&gt;I wanna share with you one of &lt;strong&gt;the most important lessons I've learned working as a software developer&lt;/strong&gt;: Do not overengineer your software.&lt;/p&gt;




&lt;p&gt;Most of the software developers I know—including myself.— when we were young, wild, and free—and by that I mean: junior.—, &lt;strong&gt;we used to think that by offering complex solutions to simple problems, we were going to impress everybody with our code&lt;/strong&gt;. Well, let me tell you... We didn't!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets see my experience.&lt;/strong&gt; It was December 2014 when I got this call from a man who had seen a few simple web pages I had developed using raw HTML, CSS, and javascript, so he wanted to make me an offer I couldn't refuse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;He wanted a social media platform&lt;/strong&gt; where people could talk about their spiritual beliefs. It sounded fun, so I thought: "Yeah! Why not? That would be cool!"&lt;/p&gt;

&lt;p&gt;Besides, by that time, I had developed a custom blank theme for WordPress, a PHP calendar, and even a basic CMS using PHP and this simple MVC framework I'd created myself.–reading a lot of blog posts though– and... How to forget... This WYSIWYG jQuery plugin. I Miss those times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What could go wrong? [sight]... Everything went wrong!&lt;/strong&gt; 🤡&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We started development in 2015&lt;/strong&gt;. It was a difficult journey due of my lack of experience in real-life projects... &lt;strong&gt;We made it happen&lt;/strong&gt; though.&lt;/p&gt;

&lt;p&gt;I'm not planning on going deeper into details, but &lt;strong&gt;it was this huge monolithic app&lt;/strong&gt; mixing an actually solid backend and the mess of a frontend with a lot of .phtml files mixing PHP, CSS, HTML, and even javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To achieve all the requirements I made this super huge javascript function&lt;/strong&gt; that worked as a &lt;strong&gt;data access layer&lt;/strong&gt;, &lt;strong&gt;interpreter&lt;/strong&gt;, &lt;strong&gt;and presenter at the same time&lt;/strong&gt;, while also offering utilities I actually didn't reuse that much.&lt;/p&gt;

&lt;p&gt;I made it work decently, nontheless &lt;strong&gt;whenever they asked for a new feature it was a pain in the ass to the point that I had to re-engineer it from scratch several times&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It wasn't scalable&lt;/strong&gt;, it wasn't easy to implement, and you needed to add a lot of code just to get a simple button. Even the layers in the backend closer to the frontend were getting affected by this bad practice. Then I couldn't handle it anymore so I left the project. The code stayed there, and nobody was able to implement it once again. You can imagine my frustration.&lt;/p&gt;

&lt;p&gt;The reason I'm sharing this is that I've seen a lot of people out there creating super complex solutions, maybe thinking it will guarantee a job or just trying to demonstrate their knowledge by creating their own stuff, which is not bad, as long as you keep it simple.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My best advice for juniors&lt;/strong&gt; is basically that. &lt;strong&gt;Keep it simple&lt;/strong&gt;, don't stop thinking about solutions, don't stop, don't stop being a rebel, keep it simple though. There's no need for you to reinvent the wheel. &lt;strong&gt;There are a lot of helpful tools and resources at your disposal&lt;/strong&gt;, use them to build something better. Keep your code as clean as possible, and something really important: &lt;strong&gt;frontend might be the new backend... It's not the same though&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now I would like to read your experiences. Have you ever face this situation? what did you do to overcome the neccesity of overengineering software?&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to add a custom font to an HTML canvas</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Tue, 27 Apr 2021 17:02:58 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/how-to-add-a-custom-font-to-an-html-canvas-1m3g</link>
      <guid>https://dev.to/thehomelessdev/how-to-add-a-custom-font-to-an-html-canvas-1m3g</guid>
      <description>&lt;p&gt;So yesterday I was working on a canvas and I realized that the font I had tried to add using &lt;code&gt;@font-face&lt;/code&gt; with CSS, wasn't working at all.&lt;/p&gt;

&lt;p&gt;After doing what I guess every developer in my position would do—Yeah, I mean searching on StackOverflow‒I find that this is a pretty common issue.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;how can you solve this?&lt;/strong&gt; Well, the solution is simple, even though I hope you have at least basic knowledge of HTML, CSS, and Javascript&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE HTML&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta charset="UTF-8"&amp;gt;
&amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
&amp;lt;title&amp;gt;My Canvas Project&amp;lt;/title&amp;gt;
&amp;lt;style type="text/css"  rel="stylesheet"&amp;gt;
    body {
        margin: 0;
    }
&amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;canvas&amp;gt;&amp;lt;/canvas&amp;gt;
&amp;lt;script src="canvas.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;As you can see we have a &lt;strong&gt;simple HTML5 document&lt;/strong&gt; here, with a &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; tag and a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag that we will use to load our javascript code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Javascript
&lt;/h2&gt;

&lt;p&gt;Now it's time for us to work on the javascript file, the one that will let us load our custom font. &lt;strong&gt;I'll explain it step by step&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;canvas = document.querySelector('canvas');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;The first thing we need to do is to get the canvas element inside a variable&lt;/strong&gt;. To do that we use the &lt;code&gt;document.querySelector()&lt;/code&gt; method, which receives the element name ('canvas' in this case). Now we can interact with the canvas element from our javascript code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;canvas = document.querySelector('canvas');
var myFont = new FontFace('myFont', 'url(assets/fonts/myFont/myFont.otf)');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Now we create our font object&lt;/strong&gt; using the javascript class &lt;code&gt;FontFace&lt;/code&gt; which receives the &lt;em&gt;font family&lt;/em&gt; and the &lt;em&gt;source&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is an experimental technology, you can learn more about i's browser compatibility &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/FontFace/FontFace#browser_compatibility" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;canvas = document.querySelector('canvas');
var myFont = new FontFace('myFont', 'url(assets/fonts/myFont/myFont.otf)');

myFont.load().then(function(font){

  // our code here

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Now we can load our font&lt;/strong&gt;. As you can see, we are using &lt;code&gt;.then()&lt;/code&gt; after loading the font because &lt;strong&gt;this is an asynchronous operation so we need to get the font before continue&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;canvas = document.querySelector('canvas');
var myFont = new FontFace('myFont', 'url(assets/fonts/myFont/myFont.otf)');

myFont.load().then(function(font){

  // with canvas, if this is ommited won't work
  document.fonts.add(font);
  console.log('Font loaded');

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;There you go. What happened here is that we told the object &lt;code&gt;FontFace&lt;/code&gt; to load our font and then we pass that font as a parameter of the function that will be executed next, the one inside the &lt;code&gt;.load()&lt;/code&gt; method. &lt;strong&gt;Then we add the font to our document fonts and that's it&lt;/strong&gt;, now we can display our "Hello, World!" with our custom font.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

// set width and height as screen w and h
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

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

&lt;/div&gt;
&lt;p&gt;With these two lines, we &lt;strong&gt;specify our canvas width and height&lt;/strong&gt; the same as the screen width and height.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

// get canvas context
var ctx = canvas.getContext("2d");
ctx.font = "50px myFont"; // set font
ctx.textAlign = "center"; // center text
ctx.fillText("Hello, World!", canvas.width/2, canvas.height/2); // draw centered text

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

&lt;/div&gt;
&lt;p&gt;Now, all we need to do is to &lt;strong&gt;get our canvas context&lt;/strong&gt;, &lt;strong&gt;assign the font we're gonna use&lt;/strong&gt;, &lt;strong&gt;center the text&lt;/strong&gt;, and &lt;strong&gt;draw our "Hello, World!" in the middle of our canvas&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here You'll find the whole javascript code. I hope you have found this useful. If anything, just let me know in the comments, and please don't forget to follow me, Iḿ working on an interesting Shell Scripting series focused on video games. Till the next time.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



</description>
      <category>html</category>
      <category>javascript</category>
      <category>gamedev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Back to basics; Shell Scripting</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Mon, 26 Apr 2021 20:54:27 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/back-to-basics-573e</link>
      <guid>https://dev.to/thehomelessdev/back-to-basics-573e</guid>
      <description>&lt;p&gt;Hello, world, it's been a while since the last time I wrote something here, and it's been crazy all the things that have happened since.&lt;/p&gt;

&lt;p&gt;Besides quarantine and stuff, I lost my old job, I've had to move twice, lost my computer, and had a lot of bad times. Nonetheless, not everything are bad news.&lt;/p&gt;

&lt;p&gt;About a week ago, I decided to go back to my start point, bring back my old childhood dream; becoming an indie game developer.&lt;/p&gt;

&lt;p&gt;Anyways, this is not a post for complaining or talking about my life dreams —for that matter, I got my &lt;a href="https://medium.com/@alexsc6955" rel="noopener noreferrer"&gt;personal blog&lt;/a&gt;, there you'll find my life experience kinda like mixed with video game stories—, we're here to talk about dev stuff and coding.&lt;/p&gt;

&lt;p&gt;So, The main reason behind this post is to introduce my new tutorial series where you'll be able to learn Shell Scripting basics and how to develop an easy game by just using your command prompt, a game that will be available on &lt;a href="https://github.com/alexsc6955" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; for you to fork it or contribute. It'd be fun to develop a game as a community.&lt;/p&gt;

&lt;p&gt;Please stay tuned for more info about this tutorial, so please, follow me on &lt;a href="https://twitter.com/thehomelessdev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; and if there's something, in particular, you want to learn about shell scripting, please let me know in the comments down below.&lt;/p&gt;

&lt;p&gt;Thanks for reading, till the next time :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Getting started with PHP</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Mon, 06 Apr 2020 21:47:29 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/getting-started-with-php-3mga</link>
      <guid>https://dev.to/thehomelessdev/getting-started-with-php-3mga</guid>
      <description>&lt;p&gt;Hi there! When you start with web development, at some point, you realize that HTML and CSS are not enough to achieve your project goals.&lt;/p&gt;

&lt;p&gt;There are a lot of technologies to build dynamic websites and PHP is one of the easiest ones and perfect if you want to build simple backends. Besides, it is a good option for beginners' self-taught web developers. It is really easy, and there is a lot of documentation.&lt;/p&gt;

&lt;p&gt;This is the fist of a series of posts in which we will learn the basics of programming logic using PHP.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PHP?
&lt;/h2&gt;

&lt;p&gt;Paraphrasing the omniscient Wikipedia, PHP is a programming language specially created for web development, and it is usually processed on a web server by a PHP interpreter implemented.&lt;/p&gt;

&lt;p&gt;In simple words, it is a programing language to write server side web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do I need?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A computer, obviously, with Windows, Linux or Mac.&lt;/li&gt;
&lt;li&gt;You need to install either &lt;a href="https://code-boxx.com/difference-wamp-lamp-mamp-xampp/" rel="noopener noreferrer"&gt;WAMP, LAMP, MAMP or XAMPP&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Windows users: &lt;a href="http://www.wampserver.com/en/" rel="noopener noreferrer"&gt;WAMP&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Linux users: &lt;a href="https://medium.com/better-programming/how-to-install-lamp-stack-on-ubuntu-db77ac018116" rel="noopener noreferrer"&gt;LAMP&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Mac users: &lt;a href="https://www.mamp.info/en/mac/" rel="noopener noreferrer"&gt;MAMP&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Basic shell commands&lt;/li&gt;

&lt;li&gt;Your favorite text editor&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Hello, World!
&lt;/h2&gt;

&lt;p&gt;Once you install the Apache server and PHP we can start coding. Open the command line, go to your server root (Usually &lt;code&gt;C:\wamp\www\&lt;/code&gt; on Windows, &lt;code&gt;/var/www/&lt;/code&gt; on Linux, and &lt;code&gt;/Applications/MAMP/htdocs/&lt;/code&gt; on Mac) to create a new folder and our first PHP file.&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;mkdir &lt;/span&gt;my-first-php-script

&lt;span class="nb"&gt;cd &lt;/span&gt;my-first-php-script

&lt;span class="nb"&gt;touch &lt;/span&gt;helloworld.php
// On windows
&lt;span class="nb"&gt;type &lt;/span&gt;nul &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; helloworld.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the file you just created and add the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Hello, World!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you are using WAMP or LAMP, go to &lt;code&gt;&lt;a href="http://localhost/my-first-php-script/helloworld.php" rel="noopener noreferrer"&gt;http://localhost/my-first-php-script/helloworld.php&lt;/a&gt;&lt;/code&gt; or &lt;code&gt;&lt;a href="http://127.0.0.1/my-first-php-script/helloworld.php" rel="noopener noreferrer"&gt;http://127.0.0.1/my-first-php-script/helloworld.php&lt;/a&gt;&lt;/code&gt;. If you are using MAMP, go to &lt;code&gt;&lt;a href=""&gt;http://localhost:8888/my-first-php-script/helloworld.php&lt;/a&gt;&lt;/code&gt; And you will see the message "Hello, World!" in your browser.&lt;/p&gt;

&lt;p&gt;What happened here? I think you noticed the delimiters &lt;code&gt;&amp;lt;?php ?&amp;gt;&lt;/code&gt;. They are required to insert the PHP code. Consider that you can mix PHP with HTML so these delimiters tell the server interpreter that this is PHP code. Let's see.&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;touch &lt;/span&gt;index.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the file and add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;PHP Hello World&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Hello, World!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be rendered as a simple HTML page but we are sending the "Hello, World!" from PHP, using the delimiters.&lt;/p&gt;

&lt;p&gt;Let's continue. The command &lt;code&gt;echo&lt;/code&gt; is a language constructor that allows us, in this case, to print a string in the browser. Notice that we are using a semicolon at the end of the line. This is really important in PHP. Each operation line must be ended by a semicolon, there are exceptions that we will see then.&lt;/p&gt;

&lt;p&gt;To ease coding, we can abbreviate the &lt;code&gt;echo&lt;/code&gt; command this way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?=&lt;/span&gt; &lt;span class="s1"&gt;'PHP Hello World'&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see it working.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c"&gt;&amp;lt;!-- Abreviated `echo` command` --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;?=&lt;/span&gt; &lt;span class="s1"&gt;'PHP Hello World'&lt;/span&gt; &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'Hello, World!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can check in your browser and see how the title stays the same. Notice that I didn't add a semicolon at the end of the line. You can add it, but it is not required in this case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comments
&lt;/h2&gt;

&lt;p&gt;PHP supports the same kind of comments that use languages like C, C++, and Shell. Let's see&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;touch &lt;/span&gt;comments.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the file and add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'This is an Example'&lt;/span&gt; &lt;span class="c1"&gt;// One line comment.&lt;/span&gt;
&lt;span class="cm"&gt;/* 
  Multiline comment
*/&lt;/span&gt;
&lt;span class="c1"&gt;# Like shell comment.&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to be careful with multiline comments. A simple error can crash your whole script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'This is OK'&lt;/span&gt;
&lt;span class="cm"&gt;/* 
  The multiline comments have delimiters.
  If you write something after closing, PHP will
  try to execute it as code and as obviously it 
  won't make sense, the script will crash
*/&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="nc"&gt;Close&lt;/span&gt; &lt;span class="n"&gt;delimiter&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'This is not right'&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is all for now. Next time we will learn about variables. I hope y you have found this useful. Please like and share and if you think something is missing, please let me know in the comments below.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Write your first web server with node.js</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Mon, 06 Apr 2020 16:35:34 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/write-your-first-web-server-with-node-js-2260</link>
      <guid>https://dev.to/thehomelessdev/write-your-first-web-server-with-node-js-2260</guid>
      <description>&lt;p&gt;Hey there. This time we will see how to write a web server using node.js and &lt;code&gt;http&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do I need to know?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/rincorpes/understanding-node-js-before-diving-in-24jh"&gt;Basic understanding of node.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/ECMAScript" rel="noopener noreferrer"&gt;Basic Javascript and ECMAScript standard&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What will I learn?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is HTTP&lt;/li&gt;
&lt;li&gt;Write a server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a guide for beginners so it comes with some theory. I will not dig a lot this time (assuming you already know &lt;a href="https://en.wikipedia.org/wiki/Server_(computing)" rel="noopener noreferrer"&gt;what a server is&lt;/a&gt;) but if you don't care about it, you can go straight to the action.&lt;/p&gt;

&lt;p&gt;For this tutorial, we are going to use the &lt;strong&gt;node http module&lt;/strong&gt;, so let's see what is it.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;Hypertext Transfer Protocol&lt;/strong&gt; (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I really love Wikipedia's definitions XD. Let's try to clarify this concept.&lt;/p&gt;

&lt;p&gt;A web application is stored on a piece of hardware with its respective software, which allows communication with other pieces of software called &lt;em&gt;clients&lt;/em&gt;——This is a really basic definition about what a web server is but is enough for us now.&lt;/p&gt;

&lt;p&gt;This "server" needs a set of rules and permissions to allow communication between it and the clients, and that's what HTTP is there for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP is the main web protocol&lt;/strong&gt;. When we enter a website, our browser, which acts as a client, sends a request through an URL, then the server process that petition and sends a response.&lt;/p&gt;

&lt;p&gt;In the case of node.js, we need to configure our server's behavior. It doesn't work as Apache or similar servers, and that's what we are going to learn next.&lt;/p&gt;

&lt;h2&gt;
  
  
  The action
&lt;/h2&gt;

&lt;p&gt;This time we don't need to install anything. the http module comes as default with node.js so the first thing we are going to do is create our project directory and file to run our server. Open the command line and write the following.&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;mkdir &lt;/span&gt;server

&lt;span class="nb"&gt;cd &lt;/span&gt;server

&lt;span class="nb"&gt;touch &lt;/span&gt;server.js
// On windows
&lt;span class="nb"&gt;type &lt;/span&gt;nul &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the &lt;code&gt;server.js&lt;/code&gt; file with your favorite text editor and add the following.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I already said, &lt;strong&gt;http&lt;/strong&gt; comes with node.js so we don't need to install it. With this line, we load the module into the &lt;code&gt;http&lt;/code&gt; constant.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;127.0.0.1&lt;/span&gt;&lt;span class="dl"&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;3000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we are going to run our server on our local machine, we set our host referring &lt;code&gt;localhost&lt;/code&gt; with its IP, and for a convention, we are going to use port &lt;code&gt;3000&lt;/code&gt;.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&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="nx"&gt;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&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;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&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;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using the &lt;code&gt;createServer&lt;/code&gt; method from &lt;code&gt;http&lt;/code&gt; and we are passing around a callback with the parameters &lt;code&gt;req&lt;/code&gt; and &lt;code&gt;res&lt;/code&gt;. &lt;code&gt;req&lt;/code&gt; is an object with the petition information, and &lt;code&gt;res&lt;/code&gt;, an object with the response.&lt;/p&gt;

&lt;p&gt;We are setting the &lt;code&gt;statusCode&lt;/code&gt; 200 which is a standard response for successful HTTP requests.&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;setHeaders&lt;/code&gt; method, we are adding the content type of the response, and to finish, we close the connection with a message using the method &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will learn about HTTP headers and status codes and messages in future posts. By now this is enough. Let's continue.&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="nx"&gt;server&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;host&lt;/span&gt;&lt;span class="p"&gt;,&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;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;`Listening on http://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;host&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="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;The method &lt;code&gt;listen&lt;/code&gt; will receive the port, the host and a callback function that we will use to log a message in the console and it will execute the server.&lt;/p&gt;

&lt;p&gt;Let's try it. In the command line, write.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node ./server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Listening on http://127.0.0.1:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to &lt;a href=""&gt;http://127.0.0.1:3000&lt;/a&gt; and you will see the message "Hello, World!"&lt;/p&gt;

&lt;p&gt;That's it. Now you have your first server running in node'js. In the next post, we will start using the &lt;code&gt;Express&lt;/code&gt; framework to build our first app.&lt;/p&gt;

&lt;p&gt;If you think I missed something, let me know in the comments below. Please, like and share if you found this useful. See you next time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understanding node.js before diving in</title>
      <dc:creator>Santiago Rincón</dc:creator>
      <pubDate>Sun, 22 Mar 2020 22:28:21 +0000</pubDate>
      <link>https://dev.to/thehomelessdev/understanding-node-js-before-diving-in-24jh</link>
      <guid>https://dev.to/thehomelessdev/understanding-node-js-before-diving-in-24jh</guid>
      <description>&lt;p&gt;There are a lot of articles on the Internet that explain what node.js is, nonetheless, &lt;strong&gt;for those of us without a computer science degree it all might sound really complex and confusing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The approach of this post will be trying to explain conceptually and in context, what node.js is, how it works, and why to use it, in a way that anybody can understand. Then I'll say how you can say "Hello" to the world using node.js.&lt;/p&gt;

&lt;p&gt;If you want to go straight to the action, go to the Getting Started section.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Node.js
&lt;/h2&gt;

&lt;p&gt;Let me make things clear here before we start conceptualizing, it might not be your case but believe me, I have seen people comparing node.js to things such as Apache or Tomcat.&lt;/p&gt;

&lt;p&gt;Please, guys, just don't... Those are products ready to install servers and easily allow you to deploy your app. Sure, you can add a module for PHP and a module to allow SSL connection with node.js, however, you will need to add some code to get your server running. We will talk about modules and servers later.&lt;/p&gt;

&lt;p&gt;I have also seen people comparing node.js to Django and once again, no, Django is a framework for back-end development built with python, a tool to ease the development of web apps. In node.js we have Express for that——Though they are built with different approaches——and again, we will talk about it later.&lt;/p&gt;

&lt;p&gt;Now that we made things clear, we can continue.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://nodejs.org/en/about/" rel="noopener noreferrer"&gt;node.js official website&lt;/a&gt; defines it as an &lt;strong&gt;asynchronous &lt;em&gt;event-driven&lt;/em&gt; javaScript runtime, designed to build scalable network applications&lt;/strong&gt;.——Awesome, isn't it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/9JvgKvL0TRbD52MiwR/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/9JvgKvL0TRbD52MiwR/giphy.gif" alt="Good Mythical Morning What GIF By Rhett And Link&amp;lt;br&amp;gt;
" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yeah, I know, the first time I read these words I was like–—Yeah! sure, piece of cake, I know PHP and some Python, what can go wrong?——It all went wrong, I didn't have a clue about what I was facing, and that meant a lot of problems then.&lt;/p&gt;

&lt;p&gt;Let's dig inside node.js concepts while we see how it works, and hopefully, that will help you understand everything I'm talking about once you finish reading this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, is it javascript or what?
&lt;/h2&gt;

&lt;p&gt;Let's see, node.js was born when developers of javascript extended it from something you could only run in browsers, to something you could run on a machine.&lt;/p&gt;

&lt;p&gt;That allowed us to &lt;strong&gt;build with javascript more than just interactive websites&lt;/strong&gt;, providing us the capability to do stuff that other scripting languages like python can do, like back-end, for example. :)&lt;/p&gt;

&lt;p&gt;Now, Did you know that the only language computers understand is binary? Yes, the only thing they understand is a sequence of 0 and 1. Those languages closest to that thing we know as "machine architecture" are &lt;em&gt;low-level&lt;/em&gt; languages and those closest to us (programmers) are &lt;em&gt;high-level&lt;/em&gt; languages.&lt;/p&gt;

&lt;p&gt;Both node.js and javascript are &lt;em&gt;high-level&lt;/em&gt;, and &lt;strong&gt;they run on the &lt;a href="https://en.wikipedia.org/wiki/V8_(JavaScript_engine)" rel="noopener noreferrer"&gt;V8 javascript runtime engine&lt;/a&gt;&lt;/strong&gt;. This engine takes your &lt;em&gt;high-level&lt;/em&gt; code and converts it into something your machine can understand.&lt;/p&gt;

&lt;p&gt;By the way, to make it possible, it requires time to execute a lot of default functions that ease our work and that is what makes it a runtime. There are a lot of instructions running while a program is being executed——It's deeper than that, but that's enough for now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-blocking asynchronous
&lt;/h2&gt;

&lt;p&gt;Ok, I think this is one of the most confusing things about node.js. I have seen a lot of people misunderstanding these concepts but it is actually really easy once you get it.&lt;/p&gt;

&lt;p&gt;If you think about synchronized swimming, you may imagine a group of people making the same movement in parallel in a swimming pool. So, synchronous refers to it, operations taking place at the same time.&lt;/p&gt;

&lt;p&gt;Asynchronous is literally the opposite of synchronous. Now, think about a restaurant. There are waitpersons who have to take the menu to the customers, to serve them food, to give them the bill, to clear the table once they are gone, etc.&lt;/p&gt;

&lt;p&gt;These are asynchronous operations and customers' requirements (menu, food, bill, etc) are blocking calls. But you will never see a waitperson waiting to finish with one customer to start with another. Instead, they attempt to each table simulating some kind of parallelism which improves performance.&lt;/p&gt;

&lt;p&gt;This kind of parallelism is what we call non-blocking architecture. Let's see what happens when we code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('Hello, World')
console.log('I'm learning Node.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The result will be as expected, nothing unusual. First, you will see &lt;code&gt;Hello, World&lt;/code&gt;, then, &lt;code&gt;I'm learning Node.js&lt;/code&gt; but... what if that "Hello, World" comes from an operation that needs time to finish, something like a blocking call?&lt;/p&gt;



&lt;br&gt;
&lt;small&gt;&lt;br&gt;
I will assume you know a little bit about the &lt;a href="https://en.wikipedia.org/wiki/ECMAScript" rel="noopener noreferrer"&gt;EcmaScript&lt;/a&gt; standard and &lt;a href="https://www.w3schools.com/js/js_arrow_function.asp" rel="noopener noreferrer"&gt;arrow functions&lt;/a&gt;. If not, please, go and read.&lt;br&gt;
&lt;/small&gt;&lt;br&gt;


&lt;p&gt;Let's simulate it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(() =&amp;gt; {
  console.log('Hello, World')
}, 5000)

console.log('I'm learning Node.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Result &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I'm learning Node.js
Hello, World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What happened here? As we are using an asynchronous operation, this kind of approach will not work as expected. Sure, the first event will be blocked because it needs time to finish the operation but it will not put your threat to sleep.&lt;/p&gt;

&lt;p&gt;I don't get it, why are we seeing first the next operation then? Is it executing two operations at the same time? Isn't it synchronous? No, it is because of the non-blocking architecture of node.js.&lt;/p&gt;

&lt;p&gt;Let's see. In other languages, tasks are executed one by one, node.js can execute several asynchronous tasks at the same time which means that it will return immediately with whatever result it has. And how is it possible?&lt;/p&gt;

&lt;p&gt;I don't know if you noticed. but we are passing around a function to &lt;code&gt;setTimeout()&lt;/code&gt; as we do with variables in other languages. It's a &lt;strong&gt;&lt;em&gt;callback function&lt;/em&gt;&lt;/strong&gt; and it, my friends, &lt;strong&gt;is the key to make asynchronous possible in node.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this context, a &lt;em&gt;callback function&lt;/em&gt; defines what will be executed after the blocking call finishes. In fact, &lt;strong&gt;all of the asynchronous patterns in node.js are based in &lt;em&gt;callbacks&lt;/em&gt;&lt;/strong&gt;, they just add syntactic sugar to ease our work.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;setTimeout()&lt;/code&gt; is an async function that schedules a &lt;em&gt;callback&lt;/em&gt; execution once a given time has passed. In simple words, it delays a function execution but other stuff can be happening at the same time outside this function thanks to the non-blocking architecture of node.js.&lt;/p&gt;

&lt;p&gt;Maybe you are thinking "What if I configure the delay to 0 seconds"? Well, let's see what happens.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(() =&amp;gt; {
  console.log('I'm learning NodeJs')
}, 0)

console.log('Surprise')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Result &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Surprise
I'm learning NodeJs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Wait, what? Yes, configuring the delay to 0 seconds will not mean that it will be executed first because the &lt;em&gt;callback&lt;/em&gt; is added to the &lt;em&gt;event-loop&lt;/em&gt; so it has to wait for its turn before being executed.&lt;/p&gt;

&lt;p&gt;Did you say &lt;em&gt;event-loop&lt;/em&gt;? Yes, it is responsible for scheduling the asynchronous operations, it is &lt;strong&gt;the heart of node.js&lt;/strong&gt;, and this takes us to the &lt;em&gt;event-driven&lt;/em&gt; concept which is a programming paradigm in which &lt;strong&gt;the flow of the program is determined by events&lt;/strong&gt; such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. This means that applications act on events.&lt;/p&gt;

&lt;p&gt;Asynchronous is a way more extensive topic——material for another post——but we have the basics we need to continue. If there is something you didn't understand, let me know in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why would I use Node.js anyway
&lt;/h2&gt;

&lt;p&gt;If you are a self-taught person, you should know how hard it can be to choose the perfect technology for your project. &lt;strong&gt;Node.js is not always the right answer but it could be for some cases&lt;/strong&gt;, so first, let's try to figure out if you need to use node.js by understanding the problems it solves.&lt;/p&gt;

&lt;p&gt;A few years ago I developed this prototype for a social network using PHP. It had a mixed back and front-end so it was a mess but it actually worked, at least as a prototype.&lt;/p&gt;

&lt;p&gt;The main problem we faced at that time was the server connections. When we had over 100 active users it started to collapse, we needed a better server but we had no money for that. That situation among other management issues, made the project fail.&lt;/p&gt;

&lt;p&gt;On the other hand, this app used third party API's, and a lot of other features that made it slow.&lt;/p&gt;

&lt;p&gt;A few months later I had started a project lo learn customizing Bootstrap and there was where I met node.js. I had a javascript background so it was easy to dive in.&lt;/p&gt;

&lt;p&gt;While I was learning, I discovered it had could help me to solve the previous project problems——Actually, even now I learn new things with node.js that would be helpful to that project.&lt;/p&gt;

&lt;p&gt;The first important thing I discovered was that I could use javascript not just for the front-end but for the back-end.——I'm the kind of person that hates having mixed code.—— On the other hand &lt;strong&gt;The main goal of node.js is to ease scalable network applications development&lt;/strong&gt;, and this is possible because of the way it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Single-threaded&lt;/em&gt;, &lt;em&gt;event-driven&lt;/em&gt; architecture
&lt;/h3&gt;

&lt;p&gt;When you are using languages like PHP, each connection creates a new thread which comes with 2 MB of RAM assigned. Depending on the RAM your system has, anyway, it gives you a theoretical maximum of connections.&lt;/p&gt;

&lt;p&gt;As your project users grow you will need to add more servers to keep it up with the demand. This means adding up more money to cover the server costs. Besides this, we already have costs for traffic, employees, potential technical issues and more.&lt;/p&gt;

&lt;p&gt;For this reason, the bottleneck of this kind of architecture is the maximum amount of connections that a server can handle. Node.js solves this problem by changing the way in which server connections are made.&lt;/p&gt;

&lt;p&gt;Instead of creating a new thread per connection, each one triggers an execution event inside the process of the node.js engine, which means that &lt;strong&gt;node.js operates only with one threat without having a blocking call to start the &lt;em&gt;event-loop&lt;/em&gt;&lt;/strong&gt; as similar &lt;em&gt;event-driven&lt;/em&gt; systems do.&lt;/p&gt;

&lt;p&gt;All in all, &lt;strong&gt;node.js allows your application to hold tens of thousands of simultaneous connections inside the &lt;em&gt;event-loop&lt;/em&gt;&lt;/strong&gt;. But... what happens when the application is so big that becomes extremely difficult to maintain it?&lt;/p&gt;

&lt;h3&gt;
  
  
  The rise of the &lt;em&gt;Microservices Pattern&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Apps start out small, then, they start growing and you can use the MVP pattern. It might ease your firsts maintenance problems, for sure. But it could grow bigger, more features demand and more new users with a lot of expectation.&lt;/p&gt;

&lt;p&gt;Without even regarding, you may end up with a project so big that not even your huge and experienced development team could cope with no struggling.&lt;/p&gt;

&lt;p&gt;It grows more and more and becomes more complex until you realize that just thinking about adding a new feature is a complete nightmare.&lt;/p&gt;

&lt;p&gt;If you are just developing websites you might think this never happens but &lt;strong&gt;it's a common problem for companies and its answer is the &lt;em&gt;microservices pattern&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A microservice is a single self-contained unit which, together with many others, makes up a large application. By splitting your app into small units. Every part of it is independently deployable and scalable, can be written by different teams and in different programming languages and can be tested individually.——Max Stoiber.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means that you can have different teams working in different features and you will not need to deploy the whole code every time you add a new feature.——Something really useful if you ask me.&lt;/p&gt;

&lt;h3&gt;
  
  
  npm
&lt;/h3&gt;

&lt;p&gt;Last but not least, node.js provide us npm (Node Package Manager) which &lt;strong&gt;allows us to install, update and use smaller &lt;em&gt;open-source&lt;/em&gt; package (Those modules we talked about at the beginning of this post) into our application&lt;/strong&gt;, so you will not need to write everything from scratch.&lt;/p&gt;

&lt;p&gt;I have heard people concerned about privacy and security when it comes to using node.js. It's understandable, government, Fintech or MedTech organizations may wary of storing their data in the cloud. For that, npm released &lt;strong&gt;the &lt;a href="https://www.npmjs.com/products/enterprise" rel="noopener noreferrer"&gt;Enterprise version&lt;/a&gt; which allows you to run npm's infrastructure behind your company's firewall&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It gives businesses access to a private registry with advanced security features to help them control access to code, identify vulnerabilities, and automatically replace unsafe code. &lt;/p&gt;

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

&lt;p&gt;Now that hopefully, you understand what node.js is, let's give the next step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install
&lt;/h3&gt;

&lt;p&gt;To install node.js you need to download it first from its official website.&lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Download Node.js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow the instructions to install it.&lt;/p&gt;

&lt;p&gt;When finished, you will end up with both node.js and npm.&lt;/p&gt;

&lt;p&gt;Let's &lt;strong&gt;verify if everything is ok&lt;/strong&gt;. Open the command line and execute the following.&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;It should return something like this but with the version you just installed&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v12.16.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, &lt;strong&gt;verify npm&lt;/strong&gt;&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;Again, you will get something like this but with the installed version&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Hello, World! with Node.js
&lt;/h3&gt;

&lt;p&gt;I think this is one of the easiest "Hello, World!" we will do, ever. &lt;strong&gt;Create a new file&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch hello.js

// If you are using cmd on windows use the following
type nul &amp;gt; hello.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open it with your favorite text editor and add the following line.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hello, World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now just execute it using the &lt;code&gt;node&lt;/code&gt; command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node hello.js
// Output: Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that's it. A bunch theory, &lt;strong&gt;just one line of code and you already got started with node.js&lt;/strong&gt;. :)&lt;/p&gt;

&lt;p&gt;Please give this post some love and share it if you found it useful. If you think there is something I'm missing, please let me know in the comments below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3orieStB8OH7lanfGg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3orieStB8OH7lanfGg/giphy.gif" alt="Good Mythical Morning What GIF By Rhett And Link&amp;lt;br&amp;gt;
" width="480" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS. Take your time to digest all this theory because I'll be back with some interesting practice soon.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
