<?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: bittnkr</title>
    <description>The latest articles on DEV Community by bittnkr (@bittnkr).</description>
    <link>https://dev.to/bittnkr</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%2F291620%2F0475084b-4a79-4eb3-9f4a-baca743b8a23.png</url>
      <title>DEV Community: bittnkr</title>
      <link>https://dev.to/bittnkr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bittnkr"/>
    <language>en</language>
    <item>
      <title>My Trip to Here</title>
      <dc:creator>bittnkr</dc:creator>
      <pubDate>Wed, 21 Apr 2021 14:11:48 +0000</pubDate>
      <link>https://dev.to/bittnkr/introducing-uniq-2f8d</link>
      <guid>https://dev.to/bittnkr/introducing-uniq-2f8d</guid>
      <description>&lt;p&gt;This history started almost 10 years ago, on the the night of November 10, 2011.&lt;/p&gt;

&lt;p&gt;Back to home, I couldn't turn off my mind from a need I had at work: Speed up a daily task that was taking hours to be completed.&lt;/p&gt;

&lt;p&gt;Buying another server was not the solution. We already had the best we could buy. A brand new 16 core server. Which could finish the job just 20% faster then its antecessor.&lt;/p&gt;

&lt;p&gt;That task was part of a legacy system running in a single thread process. &lt;/p&gt;

&lt;p&gt;And was my task to write a multithreading version of it, splitting up the data the in smaller pieces, execute each one in parallel and glue the results at the end. So that one hour task would be finished in about 4 minutes.&lt;/p&gt;

&lt;p&gt;Figuring a way to break my data in smaller chunks to send to some imaginary running process of my system, before falling asleep, Leaving a breadcrumb to myself tomorrow, I took a piece of paper and wrote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;send(data, actor);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this door, in the next days I started designing a new class model. At same time, something "magical" was happening to me. Taken by what I use to call The Muse, I started having waves of inspiration, where every question I posed was just accompanied by the solution. And each solution leading to a new intriguing question.&lt;/p&gt;

&lt;p&gt;I was having some kind of epiphany. For some brief moments I had glimpses of higher dimensions. And everything just made sense, with an overwhelming flow of ideas that came chaotically to my mind at an incredible speed that I could not even type. So I took a pen and a half-used statistics notebook and started writing furiously.&lt;/p&gt;

&lt;p&gt;At that point I had been working with software development for 20 years and finally I was able to see the whole picture. Not only of the computer science but also of my own life and existence. &lt;/p&gt;

&lt;p&gt;The puzzle was being completed. And I was realizing that everything is just a part of a 'bigger machine' in a level I could never imagined. &lt;/p&gt;

&lt;p&gt;But suddenly, by the end of the year, just as she came, the capricious muse was gone. And all I had from her visit was a notebook full of squiggles and some proof of concepts written in pascal. &lt;/p&gt;

&lt;p&gt;Of these, the most valuable was a small implementation of a &lt;em&gt;lock-free circular buffered queue&lt;/em&gt;. Which is the reason of this writings and the base of &lt;a href="https://github.com/bittnkr/uniq"&gt;UniQ&lt;/a&gt; multithreading libray. hence the name, &lt;em&gt;uni-queue&lt;/em&gt;, a single unifying queue. &lt;/p&gt;

&lt;p&gt;You may ask, what the heck is this and why should I care?&lt;/p&gt;

&lt;p&gt;I will answer this in some minutes, but before I must tell you what others are saying about this problem.&lt;/p&gt;

&lt;p&gt;In a question in Stackoverflow about &lt;a href="https://stackoverflow.com/a/890269/9464885"&gt;Circular lock-free buffer&lt;/a&gt;, (asked 11 years ago), the most upvoted answer says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I've made a particular study of lock-free data structures in the last couple of years. I've read most of the papers in the field (there's only about fourty or so - although only about ten or fifteen are any real use&lt;/p&gt;

&lt;p&gt;AFAIK, a lock-free circular buffer has not been invented.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In &lt;a href="https://stackoverflow.com/questions/6089029/lock-free-queue#comment7056198_6089029"&gt;another S.O. question&lt;/a&gt; someone said: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Lock-free queues are unicorns.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Searching the literature, we found no more encouraging words: The book &lt;a href="https://www.amazon.com.br/Art-Multiprocessor-Programming-Revised-Reprint/dp/0123973376"&gt;The Art of Multiprocessor Programming&lt;/a&gt; (2012) says that the construction of a wait-free queue is impossible:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Corollary 5.4.1. It is impossible to construct a wait-free implementation of a queue, stack, priority queue, set, or list from a set of atomic registers. &lt;/p&gt;

&lt;p&gt;Although FIFO queues solve two-thread consensus, they cannot solve 3-thread consensus. (pg. 107)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that was the gift of the muse, the pearl behind the cryptic squiggles in the black book: &lt;/p&gt;

&lt;p&gt;A surprisingly small and simple solution to the 3-thread consensus using only 2 atomic registers. &lt;/p&gt;

&lt;p&gt;It is so simple that when I first figured it out I thought: Why hasn't anyone seen this before?&lt;/p&gt;

&lt;p&gt;In this point, my life got a plot twist.&lt;/p&gt;

&lt;p&gt;After the Muse departure, the black book got the drawer bottom and I was back to my life of middle aged IT guy, in a broken marriage, solving problems of others while my own life was a mess. But something had changed in me... After those glimpses of the higher dimensions, I was not the same person anymore.&lt;/p&gt;

&lt;p&gt;I use to say that the year 2012 was my personal revelation and my world has came to an end. Then, I took the hardest decision in my life and got divorced. Wasn't easy to end a 18-year marriage, but a yearning was burning in my heart and just needed to go get it.&lt;/p&gt;

&lt;p&gt;When in tears, my daughter asked me why I was leaving her, the only answer I had was: I'm not leaving you. I'm going to find myself.&lt;/p&gt;

&lt;p&gt;Then, the book got a safe place in my mom's closet. I got a backpack, a warm jacket and a good pair of boots, and went to walk the world. Learn how to say slangs and tell dirt jokes in foreign languages. (There are some things that sounds funnier when said by a foreigner.) &lt;/p&gt;

&lt;p&gt;When someone asked me what I was doing in the winter in "La Tierra del Fuego" near the Antarctica I just answered in spanish: &lt;em&gt;Para cometer nuevos errores&lt;/em&gt; (To make new errors) because I was tired of doing the same over and over again.&lt;/p&gt;

&lt;p&gt;And that's the reason why took me almost 10 years to bring you the git of the Muse. &lt;/p&gt;

&lt;p&gt;Maybe someday, around a campfire, we could share histories of shamanic experiences with the indian tribes in the Amazon forest or the sky of the Himalayas, but for now, I need to help you unlock your computer.&lt;/p&gt;

&lt;p&gt;If you like to think about fundamental problems of computer science and want to get the most of your computing power, stick with me and I'll take you on a beautiful journey through the magic world of parallel computing and its sorcery.&lt;/p&gt;

&lt;p&gt;See you in the next chapter:&lt;/p&gt;

&lt;p&gt;Multithreading 101 (Soon)&lt;/p&gt;

</description>
      <category>thread</category>
      <category>queue</category>
      <category>lockfree</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Kissing JavaScript #2 globals.js</title>
      <dc:creator>bittnkr</dc:creator>
      <pubDate>Mon, 23 Mar 2020 08:14:26 +0000</pubDate>
      <link>https://dev.to/bittnkr/kissing-javascript-2-globals-js-2b1k</link>
      <guid>https://dev.to/bittnkr/kissing-javascript-2-globals-js-2b1k</guid>
      <description>&lt;p&gt;Have you ever asked why you must type&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFileSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;fs&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;every time you need to read a file or use any other file handling function?&lt;/p&gt;

&lt;p&gt;In my DRY obsession, this bothers me a lot. To me, the first requirement to write simpler code is just write less code. &lt;/p&gt;

&lt;p&gt;One of my strategies to avoid the repetition is the use of global variables. &lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/bittnkr/kissing-javascript-1174"&gt;first post&lt;/a&gt; of this series, there was a part of code I didn't commented about:&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code makes the &lt;code&gt;test()&lt;/code&gt; function globally available, (in nodejs and in the browser) so I only need to require the file once for the entire application.&lt;/p&gt;

&lt;p&gt;Traditionally (before ES6) if you write&lt;code&gt;x = 10&lt;/code&gt; not preceded by &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;, that variable will automatically become a global variable. &lt;/p&gt;

&lt;p&gt;Having an accidental global variable is a bad thing because that variable can replace another with the same name declared in another part or library or simply leak the function scope.&lt;/p&gt;

&lt;p&gt;For this reason, ES6 introduced the &lt;code&gt;"use strict";&lt;/code&gt; directive. One of the things this mode do is disallow global variables by default.&lt;/p&gt;

&lt;p&gt;After that, most of the libraries avoided using global variables to not pollute the global space. &lt;/p&gt;

&lt;p&gt;So, I've a good news to you: &lt;/p&gt;

&lt;p&gt;Now the global space is almost desert and is free to be used at will by you. Yes &lt;strong&gt;you&lt;/strong&gt; are the owner of global space now, and you can use it to make you life simpler.&lt;/p&gt;

&lt;p&gt;So my second tip is just this: Create a file named &lt;code&gt;globals.js&lt;/code&gt; and put on it everything you want to have always at hand.&lt;/p&gt;

&lt;p&gt;Follow a model with part of my &lt;code&gt;globals.js&lt;/code&gt;, with some ideas of nice globals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// check if the fs variable already exists in the global space, if so, just returns&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; 

&lt;span class="c1"&gt;// a shortcut for the rest for the file&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&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="nb"&gt;window&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt;

&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// make path part of fs&lt;/span&gt;

&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;
&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;

&lt;span class="c1"&gt;// from the previous article&lt;/span&gt;
&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;./test.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="c1"&gt;// plus whatever you want always available&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now just put in the main file of your NodeJS project, the line&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;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;./globals.js&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;and after that in anywhere of your project when you need a function of &lt;code&gt;fs&lt;/code&gt; module, you just need to type:&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;var&lt;/span&gt; &lt;span class="nx"&gt;cfg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cfg.json&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;without any require(). &lt;/p&gt;

&lt;p&gt;I know this is not the most complex or genial article you have ever read on here dev.to, but I'm sure the wise use of global space can save you a lot of typing.&lt;/p&gt;

&lt;p&gt;A last word: &lt;/p&gt;

&lt;p&gt;In this time of so many bad news, I want to give you another little tip: Turn off the TV, and give a tender kiss in someone you love and loves you (despite the distancing propaganda we are bombarded). Say her how important she is to you life and how you would miss if she is gone. (The same to him)  &lt;/p&gt;

&lt;p&gt;I my own life, every time I faced death I realized that the most important and the only thing that really matters and we will carry with our souls to the after life is the love we cultivate.&lt;/p&gt;

&lt;p&gt;So, I wish you a life with lot of kisses and love.&lt;/p&gt;

&lt;p&gt;From my heart to your all. 😘&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Kissing JavaScript</title>
      <dc:creator>bittnkr</dc:creator>
      <pubDate>Wed, 18 Mar 2020 17:37:39 +0000</pubDate>
      <link>https://dev.to/bittnkr/kissing-javascript-1174</link>
      <guid>https://dev.to/bittnkr/kissing-javascript-1174</guid>
      <description>&lt;p&gt;Recently &lt;a class="mentioned-user" href="https://dev.to/ben"&gt;@ben&lt;/a&gt;
 asked &lt;a href="https://dev.to/ben/what-makes-for-readable-code-34o7"&gt;What makes for readable code?&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There, I answered in a kind of philosophical way, but since then, I'm feeling the need to bring a more practical answer. So, I decided to accept &lt;a class="mentioned-user" href="https://dev.to/jmfayard"&gt;@jmfayard&lt;/a&gt;
's  &lt;a href="https://dev.to/jmfayard/yes-you-should-write-that-firstpost-481"&gt;advice&lt;/a&gt;, and write my first post on dev.to.&lt;/p&gt;

&lt;p&gt;Someone once said me: the most readable code is... no code at all. &lt;/p&gt;

&lt;p&gt;I think this is not possible (yet). But maybe we could reduce the amount of code we write to achieve the same results without loosing readability or better yet, increase it. Focusing in terseness and readability.&lt;/p&gt;

&lt;p&gt;I have a couple of practices I use to make my code shorter and easier to read (by myself 6 months ahead).&lt;/p&gt;

&lt;p&gt;As a fan of the functional model, the better of my toolbox are simple functions and closures. &lt;/p&gt;

&lt;p&gt;The first tip I want to share is the &lt;code&gt;test()&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;This is one I use a lot because it helps me to reduce the complexity of my conditionals, and the amount of letters I must type and read.&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ifnot&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;v&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;ifnot&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&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="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;ifnot&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;source&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;RegExp&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;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;ifnot&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&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;source&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;ifnot&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&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;v&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;ifnot&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;source&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ifnot&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I know, its a lot of &lt;code&gt;if&lt;/code&gt;s for a little function, but this is its beauty, concentrated complexity for upper comfort.   &lt;/p&gt;

&lt;h3&gt;
  
  
  How to use it?
&lt;/h3&gt;

&lt;p&gt;I use wherever I need to test the presence of an item in a collection and also for nullability and equality).&lt;/p&gt;

&lt;p&gt;Using it, one can write:&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&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;instead of&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, you don't need to care if the collection is an array, object, regexp, string or null. Its always the same call. And it returns the third parameter &lt;code&gt;ifnot&lt;/code&gt; if the match is falsy, this helps on string concatenations.&lt;/p&gt;

&lt;h3&gt;
  
  
  A lovely use case
&lt;/h3&gt;

&lt;p&gt;Lets use it in a very common pattern in JS, the &lt;code&gt;parameter,options,callback&lt;/code&gt; call model, where a function receives some parameters (hopefully a few), plus a configuration object and a callback function to be called when the task is finished (All optional).&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="nx"&gt;kiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cb&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;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`💋  I kiss you &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="s2"&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;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;again&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; (One more)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;cb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;callback&lt;/span&gt;&lt;span class="dl"&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;cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;op&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&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;s&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// some different ways to call kiss()&lt;/span&gt;

&lt;span class="nx"&gt;kiss&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;kiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Honey&lt;/span&gt;&lt;span class="dl"&gt;'&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;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sweetie&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="na"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; 

&lt;span class="nx"&gt;kiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Cariño&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;again&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;   Wow! This is good! 😍&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;Note how we can pass options as string, or property of the &lt;code&gt;op&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Also, you can pass the callback parameter as the last parameter of the function call or a member of &lt;code&gt;op&lt;/code&gt;. (Not always needed, but useful for delegation).&lt;/p&gt;

&lt;p&gt;Another pattern that is very common in my code, is the use of single letter variables as a way to reduce the amount of code my brain must decode.&lt;/p&gt;

&lt;p&gt;Every time I use &lt;code&gt;s&lt;/code&gt; I mean a generic string without a specific name. As well &lt;code&gt;i&lt;/code&gt; for index, &lt;code&gt;r&lt;/code&gt; for result, &lt;code&gt;n&lt;/code&gt; for number, &lt;code&gt;v&lt;/code&gt; for any value and so on.&lt;/p&gt;

&lt;p&gt;One may take care and don't abuse of this power. Too much abbreviations can make your code cryptic. I only use when the meaning is obvious, and mostly for local variables.&lt;/p&gt;

&lt;p&gt;Here, I opted to use &lt;code&gt;op&lt;/code&gt; instead of &lt;code&gt;options&lt;/code&gt; and &lt;code&gt;cb&lt;/code&gt; for &lt;code&gt;callback&lt;/code&gt;, but I really understand if you prefer the full names.&lt;/p&gt;

&lt;p&gt;Let me know if you enjoyed this little adventure and how would you use, change or extend the function &lt;code&gt;test()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If you guys and girls like it, I would love to bring some other little tips like this in the future.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>firstpost</category>
    </item>
    <item>
      <title>If quantum supremacy solved factoring...</title>
      <dc:creator>bittnkr</dc:creator>
      <pubDate>Thu, 02 Jan 2020 04:12:45 +0000</pubDate>
      <link>https://dev.to/bittnkr/if-quantum-supremacy-solved-factoring-26og</link>
      <guid>https://dev.to/bittnkr/if-quantum-supremacy-solved-factoring-26og</guid>
      <description>&lt;p&gt;What are the consequences to the current security standards?&lt;/p&gt;

&lt;p&gt;Generalizing in other words, to those with a Bayesian way of thinking: &lt;/p&gt;

&lt;p&gt;P(secure | P=NP) = ... // How would you replace this dots?&lt;/p&gt;

</description>
      <category>security</category>
      <category>cryptography</category>
      <category>quantum</category>
      <category>factorization</category>
    </item>
  </channel>
</rss>
