<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Colin Kierans on Medium]]></title>
        <description><![CDATA[Stories by Colin Kierans on Medium]]></description>
        <link>https://medium.com/@colinkierans?source=rss-a3db5a98d92f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Y-fdRA4gXkBJ_Agb.</url>
            <title>Stories by Colin Kierans on Medium</title>
            <link>https://medium.com/@colinkierans?source=rss-a3db5a98d92f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 18:17:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@colinkierans/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making Some Code Easier to Read]]></title>
            <link>https://medium.com/@colinkierans/making-some-code-easier-to-read-2ca25f3ac00f?source=rss-a3db5a98d92f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ca25f3ac00f</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Colin Kierans]]></dc:creator>
            <pubDate>Fri, 10 Nov 2023 19:38:02 GMT</pubDate>
            <atom:updated>2023-11-10T19:38:02.673Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_2h_9qKA97-qC1pbopX0QQ.png" /><figcaption>This cute hamster is having trouble reading my code.</figcaption></figure><p>Something I have done in the past is try to condense my code into as few lines as possible. In my head, fewer lines meant less code, so it meant the code was better. Consider the following code:</p><pre>function getFeaturedProducts (products: Product) {<br>  return products.filter(p =&gt; p.num_reviews &gt;= 10).sort((a,b) =&gt; a.rating - b.rating).slice(0, 5)<br>}</pre><p>I find this code hard to read for a couple reason. It feels like I need to parse the entire line and hold its entire contents in my head if I want to work on just one small part of it. I know what <em>slice(0,5)</em> does, but to understand what business logic is happening at this point <strong>I need to have the entire line loaded into my brain</strong> at once.</p><p>Let’s make some changes.</p><p>First, let’s change our magic numbers to use variables. This gives context, and immediately lets developers know <strong>what the numbers mean.</strong></p><pre>function getFeaturedProducts (products: Product) {<br>  const minimumReviews = 10;<br>  const maxNumFeatured = 5;<br>  return products.filter(p =&gt; p.num_reviews &gt;= minimumReviews).sort((a,b) =&gt; a.rating &gt; b.rating ? -1 : 1).slice(0, maxNumFeatured)<br>}</pre><p>With these variable names and numbers, you don’t have to figure out what a number represents by working backwards from functionality. Instead of thinking “well the number 10 is being compared to the number of reviews, and we’re filtering based on that, so it most likely is a minimum number of reviews” we can <strong>just read the variable name</strong>.</p><p>The next change is very simple, we’re just going to add line breaks.</p><pre>function getFeaturedProducts (products: Product) {<br>  const minimumReviews = 10;<br>  const maxNumFeatured = 5;<br>  return products<br>    .filter(p =&gt; p.num_reviews &gt;= minimumReviews)<br>    .sort((a,b) =&gt; a.rating &gt; b.rating ? -1 : 1)<br>    .slice(0, maxNumFeatured)<br>}</pre><p>This does a few things. It makes it easier to read at glance, it reduces the need for horizontal scrolling, and most importantly it makes interpreting individual steps of the code easier.</p><p>With everything on one line, it had lumped multiple steps together. This can lead to you attempting to hold the entire context of the entire line in your head while you examine just a portion of it. By splitting it up into multiple lines, you can now read the sort line and <strong>more easily only worry about just that aspect of the code</strong>.</p><p>This code is now fairly legible. Hurray!</p><p>This next change might be overkill for this small example. We’re going to <strong>split up our chained array methods and use variables</strong>.</p><pre>function getFeaturedProducts (products: Product) {<br>  const minimumReviews = 10;<br>  const maxNumFeatured = 5;<br>  const productsWorthShowing = products.filter(p =&gt; p.num_reviews &gt;= minimumReviews)<br>  const orderedProducts = productsWorthShowing.sort((a,b) =&gt; a.rating &gt; b.rating ? -1 : 1)<br>  const snippetOfProducts = orderedProducts.slice(0, maxNumFeatured)<br>  return snippetOfProducts<br>}</pre><p>Consider the line with the <em>const orderedProducts</em> variable compared to before. With just this one line we can answer the question <strong>“what are we sorting?”</strong> Before, we’d have to also read the line above and parse the <em>.filter</em> call to answer that question.</p><p>Again, this is probably not needed for such a small function, but code can get much more complex than this.</p><p>Do you prefer the code before or after? What other changes can make it easier to read or better code?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ca25f3ac00f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>