<?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>Forem: Lucas Rodriguez</title>
    <description>The latest articles on Forem by Lucas Rodriguez (@lucasrodriguez93).</description>
    <link>https://forem.com/lucasrodriguez93</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%2F1023101%2Fb88e9aad-cb8e-4a92-9c66-3c588523cc8d.jpg</url>
      <title>Forem: Lucas Rodriguez</title>
      <link>https://forem.com/lucasrodriguez93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lucasrodriguez93"/>
    <language>en</language>
    <item>
      <title>Going beyond the old and boring console.log()</title>
      <dc:creator>Lucas Rodriguez</dc:creator>
      <pubDate>Fri, 10 Feb 2023 13:00:00 +0000</pubDate>
      <link>https://forem.com/lucasrodriguez93/going-beyond-the-old-and-boring-consolelog-1l0d</link>
      <guid>https://forem.com/lucasrodriguez93/going-beyond-the-old-and-boring-consolelog-1l0d</guid>
      <description>&lt;p&gt;Every dev that has ever used JS / TS has used at least once in his life the famous console.log(), either to just go step by step of the code, to show a return message and more, but you there, did you know that there's much more than just the console.log() ?&lt;/p&gt;

&lt;p&gt;Below you will find so options that can even help us when developing our code. But just making it clear, those are not all the options that exist. Let's check them out!👇&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 console.assert
&lt;/h2&gt;

&lt;p&gt;The console.assert() is for checking some expression we pass and if it is false, we return a message in the terminal / console.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  🧹 console.clear
&lt;/h2&gt;

&lt;p&gt;This option is for, as the name suggests, clearing our console / terminal.&lt;/p&gt;

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

&lt;p&gt;The console.clear() can be very nice at the end of our code where we can clear the terminal and not worry about it later.&lt;/p&gt;

&lt;h2&gt;
  
  
  ➕ console.count
&lt;/h2&gt;

&lt;p&gt;We can use console.count() to count how many times a function has been executed / called or how many times we have gone through that code already etc.&lt;/p&gt;

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

&lt;p&gt;In this example, we just called console.count() 5 times, the result of this in our terminal will be as follows:&lt;/p&gt;

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

&lt;p&gt;Another example is the following, where we can pass a text that will be displayed on the terminal along with the default count from the console.count() itself (this text is one of the parameters we can pass):&lt;/p&gt;

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

&lt;p&gt;The result of this code in the terminal will be as follows:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  ⚠️ console.warn
&lt;/h2&gt;

&lt;p&gt;We use console.warn() to pass warnings to the user or just to identify something that is not going well, in this case the text itself gets the color yellow.&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;p&gt;In this case we manually execute the code by going to the browser console, but this command can be given in your JS code and be executed after the user does some action on your HTML page, for example sending forms or clicking on some button.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ console.error
&lt;/h3&gt;

&lt;p&gt;Almost like console.warn the console.error serves precisely to show us possible errors on the console / terminal, this time the text will be colored red.&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  ❓ console.info
&lt;/h2&gt;

&lt;p&gt;Almost identical to the traditional console.log() that we normally use, in this case we can use the console.info() to really know that it is necessary information and better understand the code, because otherwise there are no differences like the previous examples where the color of the letters and other things end up changing. In Firefox and Chrome, a little &lt;em&gt;“i”&lt;/em&gt; icon can be displayed next to the console log items.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  📋 console.table
&lt;/h2&gt;

&lt;p&gt;The console.table() can be a great help when we want to better visualize some information on the console, precisely by using the console.table() we will create a table on the console where we will see the information we pass to it.&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;p&gt;It creates a table with two columns, one with the index of each element in the array and the data that is in each of those indexes.&lt;/p&gt;

&lt;p&gt;Another example that we can explore is passing an object instead of array to console.table():&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  👥 console.group
&lt;/h2&gt;

&lt;p&gt;With the console.group() we can group some information to be able to visualize it in the best way, in a way that we delimit these groups (in the following examples we will see this) and also to help the messages will be indented inside our console.group() to help our visualization even more.&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  🕑 console.time
&lt;/h2&gt;

&lt;p&gt;Have you ever thought about seeing how long it takes for a certain part of your code to run completely? That’s what console.time() is for, it will show you at the end of everything how long it took to reach the end from the console.time() call to the console.timeEnd() call.&lt;/p&gt;

&lt;p&gt;Inside console.time() we pass as parameter a name for our timer, so to speak, and in console.timeEnd() we pass the &lt;strong&gt;same name&lt;/strong&gt; to indicate that the time counting should be finished.&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  🛣️ console.trace
&lt;/h2&gt;

&lt;p&gt;With console.trace() we can trace how the code is executing, step by step.&lt;/p&gt;

&lt;p&gt;In the example below, we will just create 2 functions, one function called func1 that will call the other function func2, which in turn will have a console.trace() .&lt;/p&gt;

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

&lt;p&gt;And the result is:&lt;/p&gt;

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

&lt;p&gt;We can notice that as commented it shows a trail of which functions have been called.&lt;/p&gt;

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

&lt;p&gt;If you have any questions or want to share any tips, please leave a comment below. I would love to read your opinion and feedback!&lt;/p&gt;

&lt;p&gt;Thanks for reading! Follow me in this platform to read more programming content. Have a great day, see you soon! 👋&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>What is Purity in Javascript and Why Does It Matter?</title>
      <dc:creator>Lucas Rodriguez</dc:creator>
      <pubDate>Thu, 09 Feb 2023 13:00:00 +0000</pubDate>
      <link>https://forem.com/lucasrodriguez93/what-is-purity-in-javascript-and-why-does-it-matter-7ba</link>
      <guid>https://forem.com/lucasrodriguez93/what-is-purity-in-javascript-and-why-does-it-matter-7ba</guid>
      <description>&lt;p&gt;The process of studying JavaScript usually begins in a very practical way. From declaring variables to understanding the hell of Asynchronous JavaScript, the more theoretical concepts get left out. And this gap starts to get annoying when you get into deeper topics.&lt;/p&gt;

&lt;p&gt;With this in mind, let's start by explaining some of the more theoretical (and even philosophical) concepts of Modern JavaScript, starting with &lt;em&gt;purity&lt;/em&gt;. 👇&lt;/p&gt;

&lt;h2&gt;
  
  
  What is this purity?
&lt;/h2&gt;

&lt;p&gt;Imagine that you are creating a complex system using the functional programming paradigm. This means that your project will have a plethora of functions, one calling the other, to the point where there comes a point where the code takes on a life of its own and does things you didn’t ask for.&lt;/p&gt;

&lt;p&gt;If your goal is not to create a crazy AI, this is something you want to avoid at first. You don’t want that by changing something in one function, something in another function that you don’t even know where it is will do a carpentered double twist with no idea how it was done.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;pure function&lt;/strong&gt;, therefore, is one in which there is no change. An &lt;strong&gt;impure function&lt;/strong&gt; is one that modifies variables, states, or data beyond its scope.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The philosopher Heraclitus said that it is not possible to bathe in the same river twice, because the river of a second ago is no longer the same river of now, due to its flow. Pure functions go in the opposite direction and try to preserve the same river so that you can bathe in it as many times as you want, always knowing what will happen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pure functions
&lt;/h3&gt;

&lt;p&gt;Let’s take a look at this JavaScript function:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculateDouble = (num) =&amp;gt; num * 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we have a function that will calculate the double of a number. If we call calculateDouble(4) we get the result 8. &lt;em&gt;No matter how many times we call this function&lt;/em&gt;, we know that if the argument is 4, the result will be 8. If the argument is 5, the result will be 10, and so on.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;calculateDouble(4) // returns 8
calculateDouble(4) // returns 8
calculateDouble(4) // returns 8
// the result never changes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A pure function is going to have this &lt;em&gt;unchanging behavior&lt;/em&gt;. It’s a function that knows who it is. It’s like that senior person at work, super disciplined and methodical. Everyone in the firm knows what time she is going to have a cup of coffee, how she is going to respond when approached, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are two characteristics:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Given the same input, it will always return the same output.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It will not produce any side effects.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Impure functions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now look at this function and try to understand what it is doing:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 8;
const calculateDouble = () =&amp;gt; num *= 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This function has the same purpose as the previous example. But notice that it is accessing a variable outside its own scope. When we call it with calculateDouble()we will get the result 16. If we call it again, the result will be 32, and so on. We have here an impure function.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;calculateDouble() // returns 16
calculateDouble() // returns 32
calculateDouble() // returns 64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Another example of an impure function is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const showAlert = () =&amp;gt; {
  alert('This is a side effect!');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is also an impure function because it has a side effect. The result will always be the same depending on the environment you have configured, i.e., it will always return an alert in the window with the same message. However, for it to produce this alert the DOM API is accessed. In the case of the DOM we are assured that it will probably never change, but if it were another API, &lt;em&gt;who would guarantee us immutability?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Another example of an impure function is the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiplyRandom = (num) =&amp;gt; num * Math.random();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we are taking a number and multiplying it by a random value (which is what the function Math.random() does. Incidentally, it is also impure). Even if your arguments are always the same, the results will be different:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;multiplyRandom(3) // returns 1.2427312565643085
multiplyRandom(3) // returns 0.0966362658711748
multiplyRandom(3) // returns 2.493662851387264
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is clear from these examples that the impure function doesn’t really know what it returns. Compared to that stable co-worker, this is the one who comes in at 8 a.m. one day and is almost 12 p.m. the next, and you don’t know whether to respond politely or angrily. But it could be that this is exactly the creative employee who does something amazing if he has a leader capable of using his talent.&lt;/p&gt;

&lt;p&gt;*So don’t condemn the impure functions. *Avoidance is always good, but they can be important if you know where to use them, taking great care to document the behavior so that the code is understandable to others or even to you in the future.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the benefits of using pure functions?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reproducibility:&lt;/strong&gt; one of the principles of a scientific experiment is its ability to be reproduced and the result remain the same. In programming, this ability allows tests to be run more easily, bugs to not occur…&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parallelization:&lt;/strong&gt; although JavaScript, in theory, runs with single thread, i.e. one thing at a time, asynchronous functions are already quite common. With pure functions, you can call them in parallel without fear of the result changing, saving execution time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memoization:&lt;/strong&gt; this term means, in this case, the ability to store in memory the result of the function. We saw that the result of the function will always be the same. This means that we can save the value of the function in a variable and then use it instead of the function and the result will be the same.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many other benefits, especially when you use functional programming, but for this we need to go into other concepts that will be the subject of future posts.&lt;/p&gt;

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

&lt;p&gt;If you have any questions or want to share any tips, please leave a comment below. I would love to read your opinion and feedback!&lt;/p&gt;

&lt;p&gt;Thanks for reading! Follow me in this platform to read more programming content. Have a great day, see you soon! 👋&lt;/p&gt;

</description>
      <category>productivity</category>
    </item>
    <item>
      <title>Electron: Build Desktop Applications Using Plain Javascript</title>
      <dc:creator>Lucas Rodriguez</dc:creator>
      <pubDate>Wed, 08 Feb 2023 14:06:17 +0000</pubDate>
      <link>https://forem.com/lucasrodriguez93/electron-build-desktop-applications-using-plain-javascript-40dn</link>
      <guid>https://forem.com/lucasrodriguez93/electron-build-desktop-applications-using-plain-javascript-40dn</guid>
      <description>&lt;p&gt;The JavaScript ecosystem has proven to be very versatile over the years with great innovations, one of them being the ability to build desktop applications using just Node.js, HTML, CSS, and of course JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⚛️ What is it?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://electronjs.org/" rel="noopener noreferrer"&gt;Electron&lt;/a&gt; is a cross-platform shell — a user interface for accessing operating system services both via command line (&lt;a href="https://en.wikipedia.org/wiki/Command-line_interface" rel="noopener noreferrer"&gt;CLI&lt;/a&gt;) and graphical user interface (&lt;a href="https://en.wikipedia.org/wiki/Graphical_user_interface" rel="noopener noreferrer"&gt;GUI&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;With Electron we can develop desktop applications using HTML, CSS and Javascript — the famous Web technologies. It has integration with Node.js, we can use it to build not only the screens but all the logic of a BackEnd that accesses resources of the operating system — local directories, database in a simpler way.&lt;/p&gt;

&lt;p&gt;It is cross-platform — we can install the application on Windows, Linux and MacOS.&lt;/p&gt;

&lt;h2&gt;
  
  
  📖 History
&lt;/h2&gt;

&lt;p&gt;Electron was created by &lt;a href="https://github.com/zcbenz" rel="noopener noreferrer"&gt;Cheng Zhao&lt;/a&gt;, and released on April 11, 2013 under the name Atom Shell. As soon as May 6, 2014 the Atom code editor on GitHub became open source under the MIT license. In 2015 Atom Shell was renamed to Electron, and as time went by it became more popular and in just two years they managed to create and make available automatic app updates, Windows installers, sending app error logs, desktop notifications, among other features. In 2016 the Windows Store already started accepting Electron apps in its store.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⏳ Desktop applications in 2023?
&lt;/h2&gt;

&lt;p&gt;Yes. There are several success cases and needs that we have — as programmers we use the VS Code code editor that was made with Electron, Atom itself the first app made with Electron.&lt;/p&gt;

&lt;p&gt;It was also built with Slack, Figma, Twitch, Whatsapp desktop, among other apps that you can find on this site: &lt;a href="https://www.electronjs.org/apps" rel="noopener noreferrer"&gt;https://www.electronjs.org/apps&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🏛️ Architecture
&lt;/h2&gt;

&lt;p&gt;Electron comes with Chromium browser, an open source project from where Google Chrome came from. All the visuals, windows, etc are rendered in this layer and the BackEnd runs on Node.js. Both have access to each other via RPC (Remote Procedure Call).&lt;/p&gt;

&lt;p&gt;Two important concepts around the technology: &lt;strong&gt;main process&lt;/strong&gt; and &lt;strong&gt;renderer process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The file that is defined in the main property of package.json is called the main process. It is unique throughout the application, responsible for creating the application window(s) through instances of BrowserWindow.&lt;/p&gt;

&lt;p&gt;Electron uses Chromium to render the web pages, the multi-process architecture of that tool is used as well, each web page (Electron’s desktop window) runs its own process, which is called the &lt;strong&gt;render process.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In short, we have Node.js in the main process using the Electron library that makes native calls to the operating system. And the Electron API running the application windows with HTML, CSS, JavaScript and its under-the-hood assets with Chromium.&lt;/p&gt;

&lt;p&gt;The windows (FrontEnd) cannot invoke the native resources in order not to break the security of the application, but the communication between both processes — main and rendering, is done through a concept of &lt;strong&gt;inter-process communication&lt;/strong&gt; (IPC) using the RPC (Remote Procedure Call) method. For this we have the &lt;a href="https://www.electronjs.org/docs/latest/api/ipc-renderer" rel="noopener noreferrer"&gt;ipcRender&lt;/a&gt; and &lt;a href="https://www.electronjs.org/docs/latest/api/ipc-main" rel="noopener noreferrer"&gt;ipcMain&lt;/a&gt; APIs.&lt;/p&gt;

&lt;p&gt;This is an &lt;strong&gt;advanced topic&lt;/strong&gt;, we can create an article just for this. This knowledge is needed when we are going to access a database and other operating system resources with requests made from the application windows (renderer process).&lt;/p&gt;

&lt;h2&gt;
  
  
  📁 Project Structure
&lt;/h2&gt;

&lt;p&gt;Application in Electron, is essentially a Node.js application, needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;package.json — Points to the main app file and lists its dependencies&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;main.js — Starts the app and creates a browser window for rendering HTML. This is the main process of the app (main process)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;index.html — A web page that gets rendered. This is the renderer process of the app&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  👋 Creating the first project
&lt;/h2&gt;

&lt;p&gt;Prerequisites: Basic knowledge in HTML, CSS, JavaScript, and Node.js.&lt;/p&gt;

&lt;h3&gt;
  
  
  Starting the project
&lt;/h3&gt;

&lt;p&gt;Create a folder to make your project and initialize it in Node.js:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir hi-electron &amp;amp;&amp;amp; cd hi-electron &amp;amp;&amp;amp; npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open the hi electron folder in your code editor. If you are using VSCode use: code .&lt;/p&gt;

&lt;p&gt;Install the electron dependency with:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev electron
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After the installation, the node_modules folder will be created in the root of the project.&lt;/p&gt;

&lt;p&gt;In the package.json there will be a property “main” you can change from index.js to main.js this is the main file of the project that we will create next. And in the scripts property change the test to start and put electron main.js for Electron to run the main.js file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "hi-electron",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron main.js"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "electron": "^22.2.0"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The main.js file will contain the code responsible for creating windows and handling all operating system events.&lt;/p&gt;

&lt;p&gt;Let’s create a main.js and index.html file in the root of the project using the terminal if you prefer:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch main.js index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🔃 Adding the Live Reload
&lt;/h3&gt;

&lt;p&gt;Let’s add a lib to live reload the application with every code change we make, this helps a lot in development.&lt;/p&gt;

&lt;p&gt;Without it we would have to stop the node process and rerun every comma we change in the code.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install electron-reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Soon we will see the main.js code that will configure the application and use this lib we have just installed.&lt;/p&gt;

&lt;h3&gt;
  
  
  The main.js file
&lt;/h3&gt;

&lt;p&gt;Basically this is the first code of the app with Electron and I’ve commented out every bit of it so you understand. This is the input file that will be executed when we run the npm start command.&lt;/p&gt;

&lt;p&gt;Copy and paste this code into the main.js file that we created and read it throughly:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { app, BrowserWindow } = require('electron');&lt;br&gt;
const path = require('path');&lt;br&gt;
const url = require('url');

&lt;p&gt;// create a basic window&lt;br&gt;
let win;&lt;br&gt;
function createWindow() {&lt;br&gt;
  win = new BrowserWindow({width: 800, height: 600});&lt;br&gt;
  win.loadURL(url.format({&lt;br&gt;
    pathname: path.join(__dirname, 'index.html'),&lt;br&gt;
    protocol: 'file:',&lt;br&gt;
    slashes: true&lt;br&gt;
  }));&lt;br&gt;
  win.on('closed', () =&amp;gt; {&lt;br&gt;
    win = null;&lt;br&gt;
  });&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// create a window when Electron is ready&lt;br&gt;
app.on('ready', createWindow);&lt;/p&gt;

&lt;p&gt;// close the window when all the windows are closed&lt;br&gt;
app.on('window-all-closed', () =&amp;gt; {&lt;br&gt;
  if (process.platform !== 'darwin') {&lt;br&gt;
    app.quit();&lt;br&gt;
  }&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;// create a window when the app is activated&lt;br&gt;
app.on('activate', () =&amp;gt; {&lt;br&gt;
  if (win === null) {&lt;br&gt;
    createWindow();&lt;br&gt;
  }&lt;br&gt;
});&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  The index.html file&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;In this file paste this code below, it contains the HTML struture, the CSS styling and the logic in Javascript that will be displayed in the previously created window.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&amp;lt;html lang="en"&amp;gt;&lt;br&gt;
&amp;lt;head&amp;gt;&lt;br&gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;&lt;br&gt;
  &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;&lt;br&gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;&lt;br&gt;
  &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;&lt;br&gt;
  &amp;lt;style&amp;gt;&lt;br&gt;
    body {&lt;br&gt;
      background-color: #f0f0f0;&lt;br&gt;
    }&lt;br&gt;
    header {&lt;br&gt;
      background-color: #f0f0f0;&lt;br&gt;
      padding: 20px;&lt;br&gt;
      text-align: center;&lt;br&gt;
    }&lt;br&gt;
    main {&lt;br&gt;
      background-color: #f0f0f0;&lt;br&gt;
      padding: 20px;&lt;br&gt;
      text-align: center;&lt;br&gt;
    }&lt;br&gt;
    footer {&lt;br&gt;
      background-color: #f0f0f0;&lt;br&gt;
      padding: 20px;&lt;br&gt;
      text-align: center;&lt;br&gt;
    }&lt;br&gt;
  &amp;lt;/style&amp;gt;&lt;br&gt;
&amp;lt;/head&amp;gt;&lt;br&gt;
&amp;lt;body&amp;gt;&lt;br&gt;
  &amp;lt;header&amp;gt;&lt;br&gt;
    &amp;lt;h1&amp;gt;My First HTML Page&amp;lt;/h1&amp;gt;&lt;br&gt;
  &amp;lt;/header&amp;gt;&lt;br&gt;
  &amp;lt;main&amp;gt;&lt;br&gt;
    &amp;lt;p&amp;gt;My first paragraph.&amp;lt;/p&amp;gt;&lt;br&gt;
  &amp;lt;/main&amp;gt;&lt;br&gt;
  &amp;lt;footer&amp;gt;&lt;br&gt;
    &amp;lt;p&amp;gt;My first footer.&amp;lt;/p&amp;gt;&lt;br&gt;
  &amp;lt;/footer&amp;gt;&lt;br&gt;
&amp;lt;/body&amp;gt;&lt;br&gt;
&amp;lt;/html&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Running the project&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Open your terminal and in the root of the project run:&lt;/p&gt;


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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  ⚡️ Next steps&lt;br&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Experiment with the Electron API:&lt;/strong&gt; Try building a simple application using the Electron API and get familiar with the various components such as BrowserWindow, Menu, and Tray.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Develop a full-fledged application:&lt;/strong&gt; Once you have a good understanding of the basics, you can start building a more complex application with features like multi-window support, file system access, and native notifications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Customize the application’s look and feel:&lt;/strong&gt; You can use CSS and JavaScript to customize the appearance of your application and make it look polished and professional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Integrate with other technologies: **Electron can be used with a variety of other technologies such as React, Angular, and Vue.js, to build powerful and complex applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Publish your application:&lt;/strong&gt; Once your application is complete, you can publish it to the web or package it for distribution on various platforms like Windows, macOS, and Linux.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Join the Electron community:&lt;/strong&gt; Joining the Electron community is a great way to stay up-to-date on the latest developments, learn from other developers, and get help with any challenges you may encounter. There are many online forums, Reddit groups, and GitHub repos dedicated to Electron.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;I really liked the possibility to create a beautiful Desktop application in an easy way, using web technologies, we can use Bootstrap, React, Vue.js, any technology that runs in the browser and makes the screen building (UI) experience easier.&lt;/p&gt;

&lt;p&gt;I believe that with HTML, CSS and JS it is easier and faster to implement UI/UX best practices than with Desktop applications made with Java, C#, Visual Basic or Delphi. Let me know what you think about this in the comments.&lt;/p&gt;

&lt;p&gt;I think the possibility to reuse web knowledge to develop desktop apps is amazing.&lt;/p&gt;

&lt;p&gt;If you have any questions or want to share any tips, please leave a comment below. I would love to read your opinion and feedback!&lt;/p&gt;

&lt;p&gt;Thanks for reading! Follow me in this platform to read more programming content. Have a great day, see you soon! 👋&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
