<?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 Paula Moura on Medium]]></title>
        <description><![CDATA[Stories by Paula Moura on Medium]]></description>
        <link>https://medium.com/@paulamoura?source=rss-8d011bf10a70------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T7J0gcbASN6fzuAefwUNUA.jpeg</url>
            <title>Stories by Paula Moura on Medium</title>
            <link>https://medium.com/@paulamoura?source=rss-8d011bf10a70------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:01:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@paulamoura/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[Let’s reduce!]]></title>
            <link>https://paulamoura.medium.com/lets-reduce-97164d787da?source=rss-8d011bf10a70------2</link>
            <guid isPermaLink="false">https://medium.com/p/97164d787da</guid>
            <category><![CDATA[reduce]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Paula Moura]]></dc:creator>
            <pubDate>Tue, 27 Jul 2021 20:37:37 GMT</pubDate>
            <atom:updated>2021-07-28T02:18:41.281Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xqA9nVgGBC7NZ4XU2NXVog.jpeg" /></figure><p>Reduce was introduced in ES5 but it’s still confusing for most people. So let’s talk a little bit about it.</p><p>According to the W3 Schools, reduce is a method that executes a reducer function for each value of an array. In other words, reduce is a method that reduces multiple values into just one using the function received as the first parameter.</p><pre>const values = [1, 2, 3, 4, 5];</pre><pre>const singleValue = values.reduce((acc, value) =&gt; {<br>   return acc + value;<br>});</pre><p>As you can see, the reducer function receives two parameters: the accumulator, that stores the accumulated value, and the value of the current item of the array it&#39;s iterating over. The reducer function must return the new value of the accumulator after being aggregated with the value received. Yes, it&#39;s confusing. We can understand it better by analysing step by step:</p><pre>const values = [1, 2, 3, 4, 5];</pre><pre>const singleValue = values.reduce((acc, value) =&gt; {<br>   return acc + value;<br>}, 0);</pre><pre>STEP 1: reducerFunction(0, 1) -&gt; 0 + 1   -&gt; return 1;<br>STEP 2: reducerFunction(1, 2) -&gt; 1 + 2   -&gt; return 3;<br>STEP 3: reducerFunction(3, 3) -&gt; 3 + 3   -&gt; return 6;<br>STEP 4: reducerFunction(6, 4) -&gt; 6 + 4   -&gt; return 10;<br>STEP 5: reducerFunction(10, 5) -&gt; 10 + 5 -&gt; return 15;</pre><p>As you can see, I added a second parameter that represents the initial value, in our case: 0. This parameter is optional, but it makes it easier to understand.</p><p>So, at the first iteration, reduce will receive the value passed as the second parameter and the first value of the array, and will return their sum.</p><p>In the next iteration, the value received will be the one returned at the first iteration, and then it will return the sum of it with the second value of the array. It goes like that until the last iteration and a single value will receive the sum of all items of the array. Is it more clear now?</p><p>Most examples of reduce are like the one I just showed: using numbers. But, reduce is much more powerfull than that.</p><p>We can, for example, return an object. Imagine that I want to count how many even and odds are in the array. There are several ways to do that, but we want to use reduce because it&#39;s fun.</p><pre>const values = [1, 2, 3, 4, 5];<br>const singleValue = values.reduce((acc, value) =&gt; {<br>   let {even, odd} = acc;<br>   const isOdd = (value % 2);<br>   if (isOdd) {<br>     odd++;<br>   } else {<br>     even++;<br>   }     <br>   return {even, odd};<br>}, {even: 0, odd: 0});</pre><p>As you can see, in this case I used a populated object as the initial value. It can be of any type and that&#39;s what makes it more powerfull. We can reduce arrays to strings, new arrays, maps, objects, numbers..</p><p>The best way to get used to it, is using it! Try changing your code to reduce and see how helpfull it is in making a clean code. Have fun!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97164d787da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Having fun with object destructuring]]></title>
            <link>https://paulamoura.medium.com/having-fun-with-object-destructuring-43a3a5859536?source=rss-8d011bf10a70------2</link>
            <guid isPermaLink="false">https://medium.com/p/43a3a5859536</guid>
            <category><![CDATA[destructuring]]></category>
            <category><![CDATA[es6-js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Paula Moura]]></dc:creator>
            <pubDate>Wed, 16 Dec 2020 20:00:20 GMT</pubDate>
            <atom:updated>2020-12-17T12:05:28.955Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xqA9nVgGBC7NZ4XU2NXVog.jpeg" /></figure><p>I’ve been doing code review of a React Native app for a while now. Every now and then I see people declaring each variable separately of an object as we used to do before ES6.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a5f4afbffb0af0b4044d222bf2183d6/href">https://medium.com/media/0a5f4afbffb0af0b4044d222bf2183d6/href</a></iframe><p>I tend to think that when a person declares variables like that it’s because they don’t know the beauty of object destructuring so I always end up showing them several things we can do using this amazing feature.</p><p>So, if you’re unfamiliar with it, come with me!</p><p>The first thing that I want you to do is to analyze the code above and understand the object that we are receiving in our function. The variables <strong><em>name</em></strong> and <strong><em>hairColor</em></strong> are used and receive the same name as it’s declared in the object. Then we have <strong><em>hasTattoo</em></strong>, which is different from the received, and finally, we have a nested <strong><em>job</em></strong> object which also changes the name to make the code more clear to read.</p><p>Let’s start destructuring the two first variables that will keep the name received. To do that, you just need to declare your variables like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30fcb2030bfa099a929355e0bd08bb5/href">https://medium.com/media/b30fcb2030bfa099a929355e0bd08bb5/href</a></iframe><p>Note that we can have more than one variable at the same line, and the order here is not important. You just have to use the same name used in the object that you’re destructuring.</p><p>Now, we want to get <strong><em>tattoo</em></strong> but we want to call it <strong><em>hasTattoo</em></strong> to make the code easier to understand. To do that, you can use the following notation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11b84979092655882d96ed47478efea0/href">https://medium.com/media/11b84979092655882d96ed47478efea0/href</a></iframe><p>Finally, we got all data at the first level of our object. So maybe you’re wondering, if you should add another line and destructure the nested object. The answer is NO! You can declare multiple variables of different levels in just one assignment. All you have to do is repeat the object’s structure. Let’s see how it looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f30d29ec0ee6c9f94f57cb59511cec92/href">https://medium.com/media/f30d29ec0ee6c9f94f57cb59511cec92/href</a></iframe><p>Here I wanted to change just the name of <strong><em>enterprise</em></strong> to <strong><em>jobEnterprise</em></strong>, and it has the same notation as if it was in the first level, that is, object’s key and then the name you want.</p><p>Maybe you noticed that so far I only used const variables, but you can use let too. And more, you can reassign one variable using the following notation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/695866fb16e4cfe6e3944e1d81658a31/href">https://medium.com/media/695866fb16e4cfe6e3944e1d81658a31/href</a></iframe><p>Finally, imagine the service changed and now it also returns <strong><em>favoriteFood</em></strong><em> </em>in the response. And, of course, we want to use it on our screen. How can we do that and avoid the boring <strong><em>if (this.response.favoriteFood !== undefined) …</em></strong>? For that, we can assign a default value while destructuring.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f6e50b8b741e8c58aec2d5fecd25c66/href">https://medium.com/media/8f6e50b8b741e8c58aec2d5fecd25c66/href</a></iframe><p>And that’s how it looks like using one assignment. For me this code sugaring makes the code much cleaner and readable. What do you think?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43a3a5859536" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>