<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>ceifa</title><description>Posts by Gabriel Francisco.</description><link>https://ceifa.dev</link><item><title>The real secret to getting started with programming</title><link>https://ceifa.dev/programming-begginer</link><guid isPermaLink="true">https://ceifa.dev/programming-begginer</guid><description>These days it’s very common to see articles and courses like “The ultimate guide to getting into programming”, or “The one simple trick that will teach you to code in 2 weeks”, or…</description><pubDate>Tue, 07 Sep 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;These days it’s very common to see articles and courses like “The ultimate guide to getting into programming”, or “The one simple trick that will teach you to code in 2 weeks”, or even worse, “Take this course and walk out building an Uber clone”.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Sorry to burst your bubble, but it’s all a lie.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;These articles are usually full of useless phrases and meaningless advice, and the courses that promise you’ll build Uber won’t teach you programming. They’ll teach you how to build an Uber by copying code.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Even with my deep dislike for this kind of content, I decided to write my own, because I get this question a lot from the communities I’m part of. I still think it’s a terrible question, but at least now I’ll have a link to send and won’t have to talk it through one-on-one every time.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;lie-to-yourself&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Lie to yourself&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;A few times a month I hang out with some friends in a Discord community and we start coding together. On some of those nights people keep joining and leaving the voice channel, and it’s easy to hear “man, I wish I knew how to code so I could make something cool”. Seriously, that is by far the worst thing you can tell yourself when you want to learn something.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;do-it&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Do it&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Almost every week I see new programming courses or code camps launching, and they can be a great kick-start for building something from scratch, but they’ll rarely teach you how to solve real problems. The more solid and practical courses, like Trybe’s for example, can expose you to great concepts you’ll &lt;em&gt;occasionally&lt;/em&gt; use in real life, in a short time, but if that’s your only real qualification, it may not be enough.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The big secret to actually learning is simpler than it looks: just &lt;strong&gt;do it&lt;/strong&gt;. The same way you don’t need to be Leonardo DiCaprio and know every acting concept and technique to recite Hamlet’s &lt;strong&gt;to be or not to be&lt;/strong&gt; monologue, you don’t need to know even the basics of programming to start making something.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;First set a simple goal, something you can build that isn’t too complex, like a Discord bot or a mod for that game you love. Then make Google your best friend and start putting the puzzle together: “How do I make a Discord bot?”, “How do I add numbers in JavaScript?”, and &lt;em&gt;voilà&lt;/em&gt;! You have a bot that adds numbers.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Use and abuse the community. There are thousands of communities on Discord, and whatever you’re building, there will be one for it.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;strong&gt;Writing good code isn’t easy.&lt;/strong&gt; The upside is that nobody said your code has to be good. While you’re learning, focus on making it work, not on making it readable. Trust me, technique is much easier to pick up over time than self-confidence.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;write-code&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Write code&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;You learned the basics and still can’t land a job? The answer is simple: &lt;strong&gt;write more code!&lt;/strong&gt; I see a lot of beginners who are afraid of simply programming for free in their spare time. If you’re one of them, first you need to be honest with yourself and figure out whether you really want to be a programmer. The same way an actor can act and rehearse without being on a TV network, you can program without having a job.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;recommendations&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Recommendations&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=ecIWPzGEbFc&quot;&gt;This video from Uncle Bob&lt;/a&gt; has a really interesting take on the barrier to entry in the industry.&lt;/span&gt;&lt;/p&gt;
</content:encoded></item><item><title>A short summary of WebRTC</title><link>https://ceifa.dev/webrtc</link><guid isPermaLink="true">https://ceifa.dev/webrtc</guid><description>The WebRTC API (Web Real-Time Communications) is a technology conceived by Google in 2009 and standardized by the W3C and IETF in 2013, after a demo of a video call between…</description><pubDate>Wed, 05 May 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The WebRTC API (Web Real-Time Communications) is a technology conceived by Google in 2009 and standardized by the W3C and IETF in 2013, after a &lt;a href=&quot;https://blog.chromium.org/2013/02/hello-firefox-this-is-chrome-calling.html&quot;&gt;demo of a video call between Mozilla’s and Google’s browsers&lt;/a&gt;. Despite being used mostly for media streaming, WebRTC isn’t necessarily about that. It lets apps and websites capture and send arbitrary data between clients peer-to-peer, without a middleman.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;how-does-it-work&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;How does it work?&lt;/span&gt;&lt;/h2&gt;
&lt;h3 id=&quot;stun-server-and-ice&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;STUN server and ICE&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;To connect peers, first you’ll need a STUN (Session Traversal Utilities for NAT) server. STUN is a protocol that other protocols use as a tool for dealing with NAT (Network Address Translation). In our case we need it to generate the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Connectivity#ice_candidates&quot;&gt;ICE candidates&lt;/a&gt;, a list of the IPs, ports and protocols available on that client.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;signaling-server&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Signaling server&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Peers don’t know the others exist before connecting. So once each client has its ICE candidates, they need to negotiate a connection. For that, each peer has to send an &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/SDP&quot;&gt;SDP&lt;/a&gt; (Session Description Protocol) to the other peers, and getting each SDP to the other side is the developer’s job.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The signaling flow goes like this: the first peer creates an offer and sends it to the second peer, which receives it and sends an answer back. Once the first peer receives the answer, they’re ready to connect.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;turn-server&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;TURN server&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;You can’t count on browsers managing a direct connection, because some networks are more locked down and have stricter firewalls. In that case you’d need a TURN (Traversal Using Relays around NAT) server to relay the traffic between the peers.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;the-whole-flow&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;The whole flow&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I hate these WebRTC handshake diagrams because they tend to be really confusing, but if you’re a visual person, the flow looks more or less like this:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;/images/webrtc-flow.webp&quot;&gt;&lt;img src=&quot;/images/webrtc-flow.webp&quot; alt=&quot;The WebRTC connection flow between two peers, a signaling server and STUN/TURN servers&quot; width=&quot;641&quot; height=&quot;559&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;in-practice&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;In practice&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Despite all the servers needed for reliable communication over WebRTC, in practice you don’t need all of it for small projects. They’re cheap to run, so a &lt;a href=&quot;https://gist.github.com/sagivo/3a4b2f2c7ac6e1b5267c2f1f59ac6c6b&quot;&gt;STUN server&lt;/a&gt; and a signaling server are easy to find on the internet. TURN servers are trickier because they use a lot of bandwidth, but you don’t always need one, and if you do, there are &lt;a href=&quot;https://github.com/coturn/coturn&quot;&gt;good open source options&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;To try it out, I wrote a chat in 100 lines using WebRTC with no private server at all, only public tools. &lt;a href=&quot;https://github.com/ceifa/serverless-webrtc-chat/blob/master/src/index.js&quot;&gt;The code is here&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
</content:encoded></item><item><title>Why you shouldn’t use setTimeout</title><link>https://ceifa.dev/settimeout-sucks</link><guid isPermaLink="true">https://ceifa.dev/settimeout-sucks</guid><description>I recently learned about the side effects of setTimeout, the hard way… I’ll try to share a bit of my suffering so more people understand how evil this function is and can protect…</description><pubDate>Thu, 29 Oct 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I recently learned about the side effects of &lt;code&gt;setTimeout&lt;/code&gt;, the hard way… I’ll try to share a bit of my suffering so more people understand how evil this function is and can protect their families.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;dont-use-it-as-eval&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Don’t use it as eval&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;This one is a bit more obvious. You probably already know that using eval is bad practice too, and unless you’re building a DRM it’s really hard to find a legitimate case where it makes sense.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;What few people know is that &lt;code&gt;setTimeout&lt;/code&gt; doesn’t only take a function as its first argument. It also takes strings, which your browser will run exactly like eval.&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&apos;DoEvilMaybeXSS()&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The lesson here is to never trust your functions’ inputs. If you’re using TypeScript it’s a little easier to trust them*, and if you’re using JavaScript, always check with &lt;code&gt;typeof&lt;/code&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;dont-use-it-for-animations&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Don’t use it for animations&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Creating animations with JavaScript is really easy. If you’ve ever tried, you probably used &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt;. A basic example would be:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; draw&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(draw&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // Draws the animation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;})()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The &lt;code&gt;draw&lt;/code&gt; function would be called every 50ms. That frequency isn’t ideal for animations, since smoothness also depends on the frame rate (fps). Most screens refresh at 60Hz, so for the smoothest result we should aim for 60fps:&lt;/span&gt;&lt;/p&gt;
&lt;details&gt;
&lt;summary&gt;Code used for the animation&lt;/summary&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; tick &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; doAnimation&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; advance &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; ctx&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.getContext&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;2d&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    tick &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; advance;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    tick &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;%=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.width&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    ctx&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.clearRect&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.width&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.height); &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    ctx&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.fillRect&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(tick&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; canvas&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.height)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/details&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; drawWithTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // 1s = 1000ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // 1000ms / 60(fps) = 16.7ms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(drawWithTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1000&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 60&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    doAnimation&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.querySelector&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;#timeout&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;This produces the following result:&lt;/span&gt;&lt;/p&gt;
&lt;figure&gt;
&lt;canvas id=&quot;timeout&quot; role=&quot;img&quot; aria-label=&quot;A bar sliding from left to right, redrawn by setTimeout 60 times a second&quot;&gt;&lt;/canvas&gt;
&lt;/figure&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The problem is that &lt;code&gt;setTimeout&lt;/code&gt; doesn’t care about the browser’s rendering. It will draw whenever it wants, not when or while the browser can. That means the browser has to deal with rendering the animation while it paints more important parts of the screen, and if the frame rate is out of sync with the screen’s repaints, it can use more CPU.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The fix for this case is &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame&quot;&gt;requestAnimationFrame&lt;/a&gt;. It’s always in sync (unless you turned VSync off) and guarantees it will be called right before each frame is rendered. The same example would look like this:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; drawWithRaf&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    requestAnimationFrame&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(drawWithRaf)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    doAnimation&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;document&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.querySelector&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;#raf&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;})();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;And it produces the following result, which is much smoother:&lt;/span&gt;&lt;/p&gt;
&lt;figure&gt;
&lt;canvas id=&quot;raf&quot; role=&quot;img&quot; aria-label=&quot;The same bar, redrawn by requestAnimationFrame&quot;&gt;&lt;/canvas&gt;
&lt;/figure&gt;
&lt;script&gt;
(() =&gt; {
    const still = matchMedia(&quot;(prefers-reduced-motion: reduce)&quot;)
    // Draws in CSS pixels on a canvas sized for the screen&apos;s pixel density,
    // and only while the canvas is on screen.
    function demo(canvas, schedule) {
        const ctx = canvas.getContext(&quot;2d&quot;)
        let tick = 0, visible = false, running = false
        function draw() {
            const width = canvas.clientWidth, height = canvas.clientHeight
            const ratio = devicePixelRatio || 1
            if (canvas.width !== Math.round(width * ratio)) {
                canvas.width = Math.round(width * ratio)
                canvas.height = Math.round(height * ratio)
            }
            // With reduced motion the bar is drawn but stays put.
            tick = still.matches ? (width - 10) / 2 : (tick + 1) % width
            ctx.setTransform(ratio, 0, 0, ratio, 0, 0)
            ctx.clearRect(0, 0, width, height)
            ctx.fillStyle = getComputedStyle(canvas).color
            ctx.fillRect(tick, 0, 10, height)
        }
        function loop() {
            if (!visible || !canvas.isConnected) return running = false
            schedule(loop)
            draw()
        }
        new IntersectionObserver(([entry]) =&gt; {
            visible = entry.isIntersecting
            if (visible &amp;&amp; !running) {
                running = true
                loop()
            }
        }).observe(canvas)
    }
    demo(document.querySelector(&quot;#timeout&quot;), (next) =&gt; setTimeout(next, 1000 / 60))
    demo(document.querySelector(&quot;#raf&quot;), (next) =&gt; requestAnimationFrame(next))
})()
&lt;/script&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Note: if the speeds look very different, know that you’re privileged to own a really good monitor&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;freeing-up-the-event-loop&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Freeing up the event loop&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;It’s common to use &lt;code&gt;setTimeout&lt;/code&gt; to reduce the amount of work done during a website’s first render:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// ... a bunch of important js ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... some expensive, non-trivial logic ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;In the case above I picked a 10ms delay, but will the browser really be free to process it 10ms from now? Always question these magic numbers. If you’re not sure about the value you’re using, you’re probably doing it wrong. With &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback&quot;&gt;requestIdleCallback&lt;/a&gt; you’ll always get a good moment to run the code. Its purpose is to queue a function to be called at a more convenient time, when it affects other components less.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;be-predictable&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Be predictable&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Using setTimeout is a one-way street: the more you use it, the more you’ll need it. Look at this example:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; changeFoo&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        foo &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    }&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;We’ve already created a problem, because now callers also need a timeout to work properly:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;foo &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;changeFoo&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;console&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.log&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(foo) &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    console&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.log&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(foo) &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// 10&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;And it also affects unit tests, which by the way are great at catching these cases of potential chaos:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&apos;variable foo should change to 10&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // Arrange&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    foo &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // Act&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    changeFoo&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // Assert&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;        equals&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(foo&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    }&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;})&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;We already know where this ends: at some point your code will be full of pointless &lt;code&gt;setTimeout&lt;/code&gt;s that you don’t know why they exist. You’ll try to remove them but you won’t be able to, because they were barely thought through and there’s no predicting what any change will do.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The examples I used illustrate the point but they’re weak. It’s much more common to see timeouts used to wait for something asynchronous or for something whose flow you don’t really control. In those cases use callbacks, async/await and events. They’ll give you much more confidence.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;legitimate-cases&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Legitimate cases&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;So you have a real case of a trivial function that doesn’t depend on real-world time and can be delayed?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;As a last resort, if you can, use an async delay:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; sleep&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; ms &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; Promise&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(resolve &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; setTimeout&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(resolve&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; ms))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;async&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; () &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    console&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.log&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&apos;foo&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    await&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; sleep&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// Waits 1 second&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    console&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.log&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&apos;bar&apos;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;})()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;This makes it much clearer to tests and callers what happens. :)&lt;/span&gt;&lt;/p&gt;
</content:encoded></item><item><title>What Garry taught me about programming</title><link>https://ceifa.dev/garry-lessons</link><guid isPermaLink="true">https://ceifa.dev/garry-lessons</guid><description>Garry’s Mod is a game that became a big hit, and believe it or not, the credit doesn’t go to its sandbox mode but to its community. It’s basically a community game where anyone…</description><pubDate>Fri, 21 Aug 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;a href=&quot;https://store.steampowered.com/app/4000/Garrys_Mod/&quot;&gt;Garry’s Mod&lt;/a&gt; is a game that &lt;a href=&quot;https://www.pcgamer.com/garrys-mod-interview/&quot;&gt;became a big hit&lt;/a&gt;, and believe it or not, the credit doesn’t go to its sandbox mode but to its community. It’s basically a community game where anyone can be a developer and make new things, and that’s beautiful. As I write this there are &lt;em&gt;more than 3,600 game modes&lt;/em&gt; and &lt;em&gt;more than 25,000&lt;/em&gt; maps on the &lt;a href=&quot;https://steamcommunity.com/app/4000/workshop/&quot;&gt;workshop&lt;/a&gt;, and the most interesting part is that the game came out on Steam in 2006 and still has a big, active community today.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I’ve been an active member since 2014, and I’ve run servers in the game since 2015. Along the way I picked up a lot of experience, and not all of it is specific to video games. The point of this post is to share some of it.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;lua&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Lua&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;a href=&quot;http://www.lua.org/&quot;&gt;Lua&lt;/a&gt; is without a doubt the simplest language I’ve ever used. Version 5.1 (the one gmod uses) has only 8 types and 21 keywords. It’s a scripting language that needs almost nothing to run, it’s easy to prototype with and see things working, and even though it’s interpreted the performance is great (and &lt;a href=&quot;https://luajit.org/&quot;&gt;LuaJIT&lt;/a&gt; exists to make that even more true).&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;its-not-your-usual-imperative-language&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;It’s not your usual imperative language&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Lua makes very few decisions for you. It lets you program in lots of ways: functional, OOP, and so on. But be careful, because it’s very different from other languages. If you want to write Lua, I recommend studying it before writing anything important, since it’s a very open language that will let you make terrible decisions without warning you.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;In Lua there are no &lt;em&gt;objects&lt;/em&gt; or arrays, everything is a table, and a table can behave like an ordered array, a graph, a tree, an associative array, etc. It’s a single type that can hold many dynamically allocated values, created with &lt;code&gt;{}&lt;/code&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;As you can imagine, there are no classes in Lua either. If you want something like that, the most you get are metatables. There are actually several projects that try to bring these concepts into the Lua world. The only one I recommend is &lt;a href=&quot;https://moonscript.org/&quot;&gt;MoonScript&lt;/a&gt;. The rest, as far as I’ve seen, just add more junk to your code and more complexity.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;you-shouldnt-learn-lua&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;You shouldn’t learn Lua&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Despite all that, I don’t recommend Lua to anyone, especially beginners. It’s a language that will hardly be useful for your future, and its behavior and conventions are very different from what we use in more popular languages. A few examples: there’s no &lt;code&gt;++&lt;/code&gt; increment operator or &lt;code&gt;+=&lt;/code&gt; assignment, for &lt;em&gt;“not equal”&lt;/em&gt; conditions you use &lt;code&gt;~=&lt;/code&gt; instead of &lt;code&gt;!=&lt;/code&gt;, there’s no &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt; but &lt;code&gt;and&lt;/code&gt; and &lt;code&gt;or&lt;/code&gt;, there’s no &lt;code&gt;continue&lt;/code&gt; keyword for loops, which ends up cascading lots of &lt;code&gt;if&lt;/code&gt;s, and arrays start at index &lt;code&gt;1&lt;/code&gt;, not &lt;code&gt;0&lt;/code&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;separation-of-concerns&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Separation of concerns&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;If you’ve played GMOD, you’ve probably run out of disk space. Every server you join downloads lots of custom files and models, and soon you have almost 100GB used by a single game. The most interesting part is that even after downloading more than 1,000 mods, they still work fine and interoperate transparently. I’ve always found that impressive. Back in the real world, honestly, writing independent code is really hard. Sure, we have design patterns for it, but sooner or later we end up tying some parts together.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The biggest lesson from all this is that you should always write code that is componentized, replaceable and extensible. Let’s build an example: say you have a shop mod, and in it the users who donated to your server get a 10% discount on the whole shop. The simplest logic we’d think of is this (code in Lua):&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span data-land=&quot;text&quot;&gt;Some file in the shop mod:&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; Shop:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;GetPrice&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt;player&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt; item_id&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    local&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; Database:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;GetItemPrice&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(item_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; player:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;IsDonator&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; price&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;At first glance it looks right, but it’s not great to mix shop logic with donation discount logic. They’re completely different things. Ideally you’d make it extensible and put that logic straight into the donation mod, and for that Garry’s Mod has the &lt;a href=&quot;https://wiki.facepunch.com/gmod/Hook_Library_Usage&quot;&gt;hook API&lt;/a&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span data-land=&quot;text&quot;&gt;Some file in the shop mod:&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; Shop:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;GetPrice&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt;player&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt; item_id&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    local&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; plain_price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; Database:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;GetItemPrice&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(item_id)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    local&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; player_price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; hook.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;Run&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;SHOP_PlayerItemPrice&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;, player, item_id, plain_price)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; player_price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;or&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; plain_price&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span data-land=&quot;text&quot;&gt;Some file in the donation mod:&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;lua&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; _G&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.Shop &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; _G&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.Shop:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;IsEnabled&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    local&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; donatorDiscount &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    hook.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;SHOP_PlayerItemPrice&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;CheckDonatorDiscount&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;        function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt;player&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt; item_id&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-parameter)&quot;&gt; plain_price&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;            -- Always check that the arguments are valid when they come from public code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; IsValid&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(player) &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; item_id &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;and&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; isnumber&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(plain_price) &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; player:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;IsDonator&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;then&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;                    return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; plain_price &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; donatorDiscount)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;                end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;            end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;        end&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Of course this code isn’t 100% separating concerns: there’s item logic inside the shop logic, event logic inside a function that gets a price, there are barely any layers, etc. But I think it illustrates extensible code well, which is what matters right now.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Focus on building small, extensible modules. In the real world, event architecture the way I showed it gets judged a lot, and passing data around in events is practically a crime, but honestly there are many cases where it works well. In JavaScript today we try hard to use monads, pure code and immutability, and the easiest way to get there is by writing &lt;a href=&quot;https://twitter.com/Rich_Harris/status/1139908960520220677&quot;&gt;small independent modules&lt;/a&gt; where extensibility lives in the parameters you pass.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Separation of concerns obviously involves a lot more. In bigger systems it might mean separating services or applications. The examples here are simple, and it’s actually debatable whether they apply the &lt;a href=&quot;https://en.wikipedia.org/wiki/Single-responsibility_principle&quot;&gt;SRP&lt;/a&gt; at all, so don’t take them as truth, take them as a starting point.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Performance&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Game development (or game modding) is delicate. Thousands of operations happen every millisecond, and if they’re not orchestrated properly you’ll easily run into performance problems, and when you try to optimize you might actually make things worse.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;focus-on-the-big-bottlenecks&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Focus on the big bottlenecks&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;When there are performance problems, developers tend to dive into the code to micro-optimize: caching variables locally to reuse them, swapping arithmetic for constants, etc. That approach is naive at best and won’t get you anywhere.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;What you should do is focus on the big problems and make macro-optimizations. Those are the ones that pay off. In Garry’s Mod, doubling FPS with a few optimizations isn’t unusual, and the same goes for web apps: at the place I work now, a simple context menu once increased page load time by almost 80%, which is bizarre to say the least.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;What are macro-optimizations? Reducing the complexity of heavy algorithms, using more efficient data structures, caching expensive calculations, doing less work inside the rendering context, and so on. Use a &lt;a href=&quot;https://developer.chrome.com/docs/devtools/performance&quot;&gt;profiler&lt;/a&gt; if you can. It’s your friend and will help you find them.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The processing power available today is huge, and while that’s great, it’s also worrying. Garry’s Mod has the Think hook, where you can run code on every frame, which means around 300 iterations per second, and it’s not unusual to see mods running a bunch of unnecessary loops in there. The problem is that these bottlenecks don’t show up right away. You only notice when you remember you used to run the game at 400 FPS and now you’re at 200.&lt;/span&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Garry’s Mod is quite the school. Complex things done in simple ways, and every now and then you find yourself learning a bit more about bitwise operations, 3D rendering, networking, etc.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;If you’re into Garry’s Mod modding too, don’t hesitate to join our &lt;a href=&quot;https://discord.gg/ytkXGNU&quot;&gt;Brazilian devs community on Discord&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
</content:encoded></item><item><title>Stop writing React (a letter from a distressed svelter)</title><link>https://ceifa.dev/stop-writing-react</link><guid isPermaLink="true">https://ceifa.dev/stop-writing-react</guid><description>React is good, but it doesn’t fit every case. A look at Svelte and how the two compare.</description><pubDate>Sat, 08 Aug 2020 00:00:00 GMT</pubDate><content:encoded>&lt;blockquote&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Note: I wrote this in 2020, when this blog was built with Svelte. It runs on Astro now.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Ahhh, &lt;a href=&quot;https://react.dev/&quot;&gt;React&lt;/a&gt;. Some people say it’s hard to set up, to learn, etc., but I don’t think so. For me it was always really simple: one &lt;code&gt;create-react-app&lt;/code&gt; and you have a boilerplate ready, now just start writing JavaScript. Everything seems to work so well. You write a few “uses” and things become reactive and behave the way you expect. It couldn’t be better.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I thought that for a long time, until I watched Rich Harris’s talk &lt;a href=&quot;https://www.youtube.com/watch?v=AdNJ3fydeao&quot;&gt;Rethinking reactivity&lt;/a&gt;, &lt;strong&gt;and after trying Svelte, I just can’t go back to React&lt;/strong&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Just to be clear up front: React is good and has good use cases. It performs well thanks to its techniques around the virtual DOM, memoization, immutability, efficient rendering, etc. And it will still get a lot better, especially because it has a big community behind it and Facebook is committed to it, even assigning employees exclusively to the library. One example of how it keeps improving is the current, still experimental, concurrent mode.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;performance&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Performance&lt;/span&gt;&lt;/h2&gt;
&lt;h3 id=&quot;bundle-size&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Bundle size&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;As I write this, according to &lt;a href=&quot;https://gist.github.com/Restuta/cda69e50a853aa64912d&quot;&gt;this gist&lt;/a&gt; (easy to test and reproduce), React necessarily ships &lt;strong&gt;31.8kB&lt;/strong&gt; into an application, minified and gzipped. Svelte on its own, minified and gzipped, brings along a whopping &lt;strong&gt;0kB&lt;/strong&gt;. That’s because Svelte isn’t a framework or a library, &lt;strong&gt;Svelte is a language&lt;/strong&gt;, and it has no &lt;em&gt;runtime&lt;/em&gt; or anything like that. It just compiles your code as it is.&lt;/span&gt;&lt;/p&gt;
&lt;h3 id=&quot;dom-vs-virtual-dom&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;DOM vs virtual DOM&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;In &lt;a href=&quot;https://www.youtube.com/watch?v=x7cQ3mrcKaY&quot;&gt;Rethinking Best Practices&lt;/a&gt;, a 2013 talk by Pete Hunt from Facebook’s React team, he says:&lt;/span&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;This is actually extremely fast, primarily because most DOM operations tend to be slow. There’s been a lot of performance work on the DOM, but most DOM operations tend to drop frames.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;And sadly that’s true. DOM operations aren’t that cheap, because on every change the browser tries to repaint the screen, and if that happens many times in a short period it can start to bottleneck your application, since it uses the same thread as your JavaScript.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The problem is that using a virtual DOM doesn’t necessarily solve this, because it works &lt;em&gt;on top of&lt;/em&gt; the real DOM, and we know &lt;a href=&quot;https://twitter.com/pcwalton/status/1015694528857047040&quot;&gt;diffing isn’t exactly free&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I’m not saying the virtual DOM is slow. Today’s frameworks have lots of optimizations for that. But what if we simply didn’t need one?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;That’s exactly what Svelte does. Unlike traditional frameworks, it works as a compiler and moves all that work to build time instead of resolving it at runtime, which keeps things much simpler and does only what’s needed, using the real DOM directly.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;code&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Code&lt;/span&gt;&lt;/h2&gt;
&lt;h3 id=&quot;learning-curve&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Learning curve&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;I remember doing my first project from scratch with React. I started with almost zero knowledge of the tool and figured things out just by reading the official docs (which are really good and simple) and code examples. React is easy: you basically only need to learn JavaScript, and with that you can start building a bunch of stuff with &lt;code&gt;JSX&lt;/code&gt;. For example, this is React code:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; TodoApp&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; React&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;Component&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... a bunch of js logic ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    render&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;todos&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.map&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(todo &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;{todo}&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                ))}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;For a &lt;strong&gt;JavaScript&lt;/strong&gt; developer (not necessarily a web designer) it’s very clear what’s happening: when the component needs to be displayed it calls &lt;code&gt;render&lt;/code&gt;, which essentially maps the &lt;code&gt;todos&lt;/code&gt;, turning each one into a new HTML element.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Svelte isn’t like that. Even though it focuses on staying as close as possible to plain HTML, including having to write the &lt;code&gt;script&lt;/code&gt; and &lt;code&gt;style&lt;/code&gt; tags, it still has the problem of having its own &lt;em&gt;template engine&lt;/em&gt;, which doesn’t necessarily follow JavaScript conventions:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... a bunch of js logic ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    {#each todos as todo}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;{todo}&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    {/each}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;react-isnt-reactive&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;React isn’t reactive&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;React depends on you calling some functions from its API for reactivity to actually happen (&lt;code&gt;this.setState()&lt;/code&gt;, &lt;code&gt;useState()&lt;/code&gt;, &lt;code&gt;useEffect()&lt;/code&gt;). Svelte, being a language, doesn’t need that. By design it uses language statements like the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label&quot;&gt;label&lt;/a&gt; to handle reactivity and bakes everything in at compile time. Look at this &lt;a href=&quot;https://svelte.dev/repl/3341b89218734139abe75ac1bc8aa530?version=3.24.1&quot;&gt;example&lt;/a&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;html&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    $&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; doubleCounter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// &quot;$&quot; means it should react to changes in the variables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;script&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;button&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; on:click&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string)&quot;&gt;{()&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; =&amp;gt; counter++}&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    {counter} ~ {doubleCounter}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;button&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;These are parts of the compiled code:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    button &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; element&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot;button&quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    t0 &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(ctx[&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;]); &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// It bound index 0 to the &quot;counter&quot; variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    t1 &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;&quot; ~ &quot;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    t2 &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; text&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(ctx[&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;]); &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// It bound index 1 to the &quot;doubleCounter&quot; variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; instance&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;($$self&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; $$props&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; $$invalidate) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; click_handler&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; () &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; $$invalidate&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter); &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// On click, increments counter and invalidates it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    let&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; doubleCounter;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;    $$self&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;$$&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; () &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;$$self&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;$$&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.dirty &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;) { &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// If counter changed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;            $&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; $$invalidate&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; doubleCounter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; counter &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;); &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;// Updates doubleCounter and invalidates it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; [counter&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; doubleCounter&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-punctuation)&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; click_handler];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;locus-of-control&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Locus of control&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Re-rendering a component on every change is a really good strategy, but we need to be careful, because some components that seem to do simple work can start causing performance problems:&lt;/span&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code css-variables&quot; style=&quot;background-color:var(--astro-code-background);color:var(--astro-code-foreground); overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;js&quot; data-land=&quot;top&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; TodoApp&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; React&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;Component&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-comment)&quot;&gt;    // ... a bunch of js logic ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;    render&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;            &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                {&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;state&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-constant)&quot;&gt;todos&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt;.map&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(todo &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; onClick&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;{() &lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-keyword)&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-function)&quot;&gt; deleteTodo&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;(todo)}&amp;gt;{todo}&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;li&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;                ))}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;            &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-token-string-expression)&quot;&gt;ul&lt;/span&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:var(--astro-code-foreground)&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Here it creates a new array of &lt;code&gt;li&lt;/code&gt; elements, each with an anonymous function, every time the state updates, whether or not the &lt;code&gt;todos&lt;/code&gt; property changed. Of course a component like this won’t hurt your application, since with today’s hardware creating 10 or 1,000 functions is cheap, but thinking about scale, as your application grows this problem becomes more and more noticeable.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;use-cases&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Use cases&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;strong&gt;React is worth it.&lt;/strong&gt; Even though I choose Svelte for personal projects (including this blog), I’d rather use React for business applications, because React has a huge engaged community, with lots of libraries and tools that help development, and it surely won’t be abandoned easily, given it’s used by companies of astronomical size. But you have to weigh it case by case. If you have a legacy app that already has performance problems, adding another heavy library might make things worse. I’d go with micro-frontends and start moving critical parts to Svelte.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;It’s worth remembering that React by itself &lt;a href=&quot;https://react.dev/reference/react-dom&quot;&gt;isn’t even about web pages&lt;/a&gt;, it’s about building interfaces. And for mobile apps I’d definitely go with React Native.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;React’s biggest problem isn’t React, it’s the people who write it. The &lt;em&gt;hook gang&lt;/em&gt;, the pure components fallacy and the &lt;a href=&quot;https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367&quot;&gt;overuse of state managers&lt;/a&gt; (which sometimes just add more complexity) really get on my nerves. But I’ll leave that for another post.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;&lt;a href=&quot;https://x.com/_ceifa&quot;&gt;DMs open for insults.&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</content:encoded></item><item><title>The trap wave</title><link>https://ceifa.dev/trap-is-the-new-rock</link><guid isPermaLink="true">https://ceifa.dev/trap-is-the-new-rock</guid><description>Just like there were times when everyone wanted to be a rock star or a football player, we’re living in one where everyone wants to make trap. The number of people I know doing it…</description><pubDate>Thu, 23 Jul 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Just like there were times when everyone wanted to be a rock star or a football player, we’re living in one where everyone wants to make trap. The number of people I know doing it is getting out of hand. The pattern is obvious: they mirror what they like and go after it to try to do the same. Kids from the outskirts, “rich” kids with stable jobs, it doesn’t matter. Sooner or later they’ll send you their new track and tell you their new name is lil X or yung Y.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;originality&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Originality&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;The truth I see behind all this is that it barely matters whether you have a good sound or a good producer. In the end it’s all about having connections and, even more, originality.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;Sidoka, Recayd, Matuê, they all brought something new to the scene. And Cristal, I can’t even put into words that much genius in someone so young, who has been writing poetry since childhood with a mix of depth and abstraction that became a signature. &lt;a href=&quot;https://www.youtube.com/watch?v=qJw_3bTnCfI&quot;&gt;Have a listen&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;traps-golden-age-hasnt-happened-yet&quot;&gt;&lt;span data-land=&quot;text&quot;&gt;Trap’s golden age hasn’t happened yet&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;&lt;span data-land=&quot;text&quot;&gt;This is the best time to live trap. While the US already has a defined scene with big names, Brazil is still building its own. There’s still a lot of room for new artists, and society’s growing acceptance is making that possible. 2019 was a great year for the scene, but I don’t think we’ve lived the big era yet. Trap is still on track to be the next big thing, the same way rock and funk were in past decades.&lt;/span&gt;&lt;/p&gt;
</content:encoded></item></channel></rss>