<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oleg Yuzvik</title>
    <description>The latest articles on DEV Community by Oleg Yuzvik (@yuzviko).</description>
    <link>https://dev.to/yuzviko</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F209918%2F0b6dd728-545f-4e94-8e1f-4b21548db278.png</url>
      <title>DEV Community: Oleg Yuzvik</title>
      <link>https://dev.to/yuzviko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yuzviko"/>
    <language>en</language>
    <item>
      <title>Check and assign value or just assign value?</title>
      <dc:creator>Oleg Yuzvik</dc:creator>
      <pubDate>Fri, 30 Aug 2019 14:59:04 +0000</pubDate>
      <link>https://dev.to/yuzviko/check-and-assign-value-or-just-assign-value-ppm</link>
      <guid>https://dev.to/yuzviko/check-and-assign-value-or-just-assign-value-ppm</guid>
      <description>&lt;p&gt;Sometimes there is a need to set some flag if one of the items in the collection satisfies a condition. It is usually done in the loop of some kind, like this:&lt;br&gt;
&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;
let hasEdibleBananas = false;

bananas.forEach(banana =&amp;gt; {

  // ... do some banana related stuff here

  if (banana.isYellow) {
      hasEdibleBananas = true;
  } 
})
&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
This code works, but we reassign value to &lt;em&gt;hasEdibleBananas&lt;/em&gt; multiple times in case there is more than 1 yellow banana.&lt;br&gt;
We could add another condition that checks if &lt;em&gt;hasEdibleBananas&lt;/em&gt; is already set before assigning a value to it:&lt;br&gt;
&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;
let hasEdibleBananas = false;

bananas.forEach(banana =&amp;gt; {

  // ... do some banana related stuff here

  if (banana.isYellow &amp;amp;&amp;amp; !hasEdibleBananas) {
      hasEdibleBananas = true;
  } 
})
&lt;/pre&gt;

&lt;p&gt;&lt;br&gt;
My question is, whether there is really a need for checking if the &lt;em&gt;hasEdibleBananas&lt;/em&gt; flag is set before setting it.&lt;br&gt;
As far as I know, there should not be any perceivable performance benefit in the second case - when we perform additional check, neither do I think that the code is more or less readable in either of the cases. In fact, some could say that adding another condition that doesn't make any difference from the functionality point of view adds unnecessary noise to the code and thus makes it less readable.&lt;/p&gt;

&lt;p&gt;What do you think?&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
