<story><title>Simple Ways of Reducing the Cognitive Load in Code</title><url>http://chrismm.com/blog/writing-good-code-reduce-the-cognitive-load/</url></story><parent_chain><item><author>jonhohle</author><text>His second example to &amp;quot;modularize&amp;quot; a branch condition is not functionally equivalent in _most_ in-use programming languages:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    valid_user = loggedIn() &amp;amp;&amp;amp; hasRole(ROLE_ADMIN)
    valid_data = data != null &amp;amp;&amp;amp; validate(data)

    if (valid_user &amp;amp;&amp;amp; valid_data) …
&lt;/code&gt;&lt;/pre&gt;
Is not equivalent to:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    if (loggedIn() &amp;amp;&amp;amp; hasRole(ROLE_ADMIN) &amp;amp;&amp;amp;
        data != null &amp;amp;&amp;amp; validate(data)) …
&lt;/code&gt;&lt;/pre&gt;
His version will always execute `validate(…)` if `data` is not null regardless of whether the user is logged in or has the appropriate role. Not knowing the cost of `validate(…)`, this could be an expensive operation that could be avoided with short-circuiting. It also seems somewhat silly (and I know it&amp;#x27;s just a contrived example), that a validation function would not also perform the `null` check and leave that up to the caller.</text></item></parent_chain><comment><author>guelo</author><text>If validate() doesn&amp;#x27;t have a side effect then the short-circuiting doesn&amp;#x27;t matter. The micro-optimization of skipping the validation for performance reasons is premature optimization. If the performance optimization is necessary it should be stated more explicitly in the code then just being hidden being a &amp;amp;&amp;amp; short circuit&lt;p&gt;I&amp;#x27;ve always thought this kind of short-circuiting as an implementation detail of the runtime that should not be relied on, a hack from old school C that refuses to go away. I cringe when I see code that relies on it. It is not semantically obvious when a developer intends to use the short-circuiting trick vs when it&amp;#x27;s just inadvertently there. Of course part of the reason it lives on is because of our awful popular languages that require null checks everywhere but don&amp;#x27;t provide any syntactic help for it.</text></comment>
<story><title>Simple Ways of Reducing the Cognitive Load in Code</title><url>http://chrismm.com/blog/writing-good-code-reduce-the-cognitive-load/</url></story><parent_chain><item><author>jonhohle</author><text>His second example to &amp;quot;modularize&amp;quot; a branch condition is not functionally equivalent in _most_ in-use programming languages:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    valid_user = loggedIn() &amp;amp;&amp;amp; hasRole(ROLE_ADMIN)
    valid_data = data != null &amp;amp;&amp;amp; validate(data)

    if (valid_user &amp;amp;&amp;amp; valid_data) …
&lt;/code&gt;&lt;/pre&gt;
Is not equivalent to:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    if (loggedIn() &amp;amp;&amp;amp; hasRole(ROLE_ADMIN) &amp;amp;&amp;amp;
        data != null &amp;amp;&amp;amp; validate(data)) …
&lt;/code&gt;&lt;/pre&gt;
His version will always execute `validate(…)` if `data` is not null regardless of whether the user is logged in or has the appropriate role. Not knowing the cost of `validate(…)`, this could be an expensive operation that could be avoided with short-circuiting. It also seems somewhat silly (and I know it&amp;#x27;s just a contrived example), that a validation function would not also perform the `null` check and leave that up to the caller.</text></item></parent_chain><comment><author>saboot</author><text>Perhaps an alternative would be&lt;p&gt;&lt;pre&gt;&lt;code&gt;  valid_user = loggedIn() &amp;amp;&amp;amp; hasRole(ROLE_ADMIN)
  if (valid_user) {
    valid_data = data != null &amp;amp;&amp;amp; validate(data)
    if (valid_data) {
      ...
    }
  }&lt;/code&gt;&lt;/pre&gt;</text></comment>