<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nino Filiu on Medium]]></title>
        <description><![CDATA[Stories by Nino Filiu on Medium]]></description>
        <link>https://medium.com/@nino.filiu?source=rss-b8f4376a75a2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*K8LJA49C1T5aK1GNAWs7hg.jpeg</url>
            <title>Stories by Nino Filiu on Medium</title>
            <link>https://medium.com/@nino.filiu?source=rss-b8f4376a75a2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 20:48:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nino.filiu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Clean composition in JavaScript]]></title>
            <link>https://medium.com/@nino.filiu/clean-composition-in-javascript-fd8c25b02530?source=rss-b8f4376a75a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd8c25b02530</guid>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[composition]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nino Filiu]]></dc:creator>
            <pubDate>Tue, 03 Mar 2020 22:56:35 GMT</pubDate>
            <atom:updated>2020-03-03T23:06:22.966Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NlOqpDDrVrpnU2OD" /><figcaption>Photo by <a href="https://unsplash.com/@joshredd?utm_source=medium&amp;utm_medium=referral">Josh Redd</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Composition is today’s <em>ok boomer</em> of JavaScript: it addresses fundamental flaws of the inheritance-based OOP we got from the older generation, but it is often overused and misused because everybody wants to jump on new trends. Let’s clean things up.</p><h3>What is composition?</h3><p>Small reminder for people living under a rock: class inheritance is typically presented as the way to “achieve OOP” (ie reuse code and build complex objects).</p><p>But there is another way to do that: by manipulating smaller objects or classes instead of inheriting from them. That is composition. Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9074dcc4f0068089b34c4eb2c24a844/href">https://medium.com/media/e9074dcc4f0068089b34c4eb2c24a844/href</a></iframe><p>Although this is fine for simple cases, this has issues. Behaviors expect a certain state structure (for example here, .move() expects a .position to exist). Also, We&#39;re forced to accept all keys of behaviors, making objects potentially bigger than we need, and we have no control over key names. We can do better!</p><h3>Use state in behavior</h3><p>Code that works together should go together. Having a method that only works when some properties are defined and are of a certain type is a developer nightmare, as that creates an error-prone coupling over distant objects. Try to put the state in the behavior itself. The code above should be rewritten as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dda4231e2f794e32b9ce8dc2dd37519/href">https://medium.com/media/0dda4231e2f794e32b9ce8dc2dd37519/href</a></iframe><p>Basically, for the sake of low-coupling, try to compose independent objects rather than functions that only works in a certain context.</p><p>While merging behavior, we can still have issues though, for example when several behaviors operates on the same property. We can bypass such cases by <em>instrumenting</em>.</p><h3>Instrumenting over merging</h3><p>Merging is not a sufficiently powerful pattern for most real world cases, but to reuse existing objects, we can create these objects in the private scope of our factories and instrument them so as to expose only what we want and how we want it.</p><p>Here’s an example of key filtering:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/160a7e02ec8a19c7c4c51674e6bdc542/href">https://medium.com/media/160a7e02ec8a19c7c4c51674e6bdc542/href</a></iframe><p>Renaming takes bit more effort, because subfunctions might not work due to the key change:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/702dd953436ebedd3d10751131ecf45b/href">https://medium.com/media/702dd953436ebedd3d10751131ecf45b/href</a></iframe><p>Filtering and renaming are pretty powerful composition tools already, but really when instrumenting we can use the underlying objects the way we want.</p><h3>In short</h3><p>Composition is not really hard, but since the JS community is very much trend-based, and the first Google results for “Composition in JS” get it a bit wrong, so that can explain why composition is frequently not used as properly as it could.</p><p>Just remember to <strong>move behavior-related state inside your behavior</strong>, and to <strong>instrument rather than merge</strong> when needed, which can for example allow you to <strong>filter and rename keys</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd8c25b02530" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Sleeping in JavaScript]]></title>
            <link>https://medium.com/@nino.filiu/sleeping-in-javascript-e00a1ebbde2e?source=rss-b8f4376a75a2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e00a1ebbde2e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Nino Filiu]]></dc:creator>
            <pubDate>Thu, 20 Dec 2018 17:33:08 GMT</pubDate>
            <atom:updated>2018-12-20T17:33:08.193Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ha538sYmcXdXV0zG70EQJw.jpeg" /></figure><blockquote>“I’m used to the sleep() function which exists in C, Python, Bash, and many other. How can I sleep in JavaScript?”</blockquote><p>You can’t. There is no sleep function in JavaScript.</p><h4>But you can create something very similar</h4><pre>const sleep = ms =&gt; new Promise(resolve =&gt; {<br>    setTimeout(<br>        () =&gt; {resolve()},<br>        ms<br>    );<br>});</pre><p>This creates a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that will resolve in ms milliseconds, so you can use it with Promise.then(callback)</p><pre>console.log(Date());<br>sleep(1000).then(() =&gt; console.log(Date()));</pre><p>But it does not looks like the sleep from other programming languages. A way to make it more C-esque is the following:</p><pre>(async () =&gt; {<br>    console.log(Date());<br>    await sleep(1000);<br>    console.log(Date());<br>})();</pre><p>In this implementation, await is used to keep the code before the promise and after the promise in the same block. However, await can only be used inside of asynchronous functions. <a href="https://javascript.info/async-await">More about await/async</a></p><p>Additionally, this sleep does not keep the CPU busy while waiting, since it uses setTimeout to <em>wake up</em>.</p><h4>Ok, so now you can sleep in JavaScript. But… should you?</h4><p>Well, nobody is stopping you. We live in a free country and it’s up to you to do whatever you want with your code. Make an infinite loop. Crash your computer. Hack the NSA. Program in Java. Just kidding, don’t do the last one.</p><p>On a more serious note, there is no sleep function in JS because the language was built for asynchronous operations: <em>upon event X, do function Y</em>, contrary to synchronous languages: <em>do task A, then do task B, then do task C</em>.</p><p>What most people have in mind when they try to sleep in JS is the following:</p><pre>1. do task A<br>2. do task sleep for 1 second<br>3. do task B</pre><p>But it’s not really compatible with the philosophy of JS and asynchronous programming. Instead, people should have this in mind:</p><pre>1. do task A<br>2. in one second, an event will happen that will trigger task B</pre><p>And that’s exactly what setTimeout does:</p><pre>taskA();<br>setTimeout(() =&gt; taskB(), 1000);</pre><p>In a nutshell, the sleep function described in the beginning of the article can be used to have a cleaner, simpler code. But under the hood, there will always be an event, and a callback.</p><p><a href="https://github.com/ninofiliu/">github </a>/ <a href="https://stackoverflow.com/users/8186898/nino-filiu">stack overflow</a> / <a href="https://ninofiliu.com/hire-me">hire me</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e00a1ebbde2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>