title: How to recognize that short code blocks can be refactored into something cleaner?
body: <p>i have a bit of code that i wrote a few weeks ago (the code's purpose isn't so much important as its structure):</p>
  
  <pre><code>if (_image.Empty)
  {
     //Use the true image size if they haven't specified a custom size
     if (_glyphSize.Width &gt; 0)
        imageSize.Width = _glyphSize.Width //override
     else
        imageSize.Width = _image.GetWidth;
  
     if (_glyphSize.Height &gt; 0) then
        imageSize.Height = _glyphSize.Height
     else
        imageSize.Height = _image.GetHeight
  }
  else
  {
     //No image, but they can still override it with a custom size
     if (_glyphSize.Width &gt; 0) then
        imageSize.Width = _glyphSize.Width
     else
        imageSize.Width = 0;
  
     if (_glyphSize.Height &gt; 0)
        imageSize.Height = _glyphSize.Height
     else
        imageSize.Height := 0;
  }
  </code></pre>
  
  <p>i was going over it tonight, and as i was cleaning it up, i realized that the cleaned version is must more concise:</p>
  
  <pre><code>//Figure out the final image width
  if (_glyphSize.Width &gt; 0)
     imageSize.Width = _glyphSize.Width
  else if (not _glyph.Empty)
     imageSize.Width = _glyph.GetWidth
  else
     imageSize.Width = 0;
  
  //Figure out the final image height
  if (_glyphSize.Height &gt; 0)
     imageSize.Height = _glyphSize.Height
  else if (not _glyph.Empty)
     imageSize.Height = _glyph.GetHeight
  else
     imageSize.Height = 0;
  </code></pre>
  
  <p><strong>Note:</strong> i've trimmed down the code to bare logical flow, and obfsucated the source language.</p>
  
  <p>In the end i took the nested <code>if</code>'s, and inverted them. Doing that allowed this shortening. My question is: how can i recognize this in the future?</p>
  
  <p>What are the tell-tale signs that i've just written some code that can be refactored into something shorter?</p>
  
  <hr>
  
  <p>Another example i had from a few weeks ago was something akin to a permission check: the user can perform an action:</p>
  
  <ul>
  <li>if they have the permission they can do it</li>
  <li>if they don't have the permission, but the override is in effect</li>
  </ul>
  
  <p>Which i initially coded as:</p>
  
  <pre><code>if ((HasPermission || (!HasPermission and OverrideEnabled))
  {
     ...do stuff
  }
  </code></pre>
  
  <p>The logical conditions on that <code>if</code> clause seemed kind of wordy. i tried to reach back to my boolean algebra course to figure out how to simplify it. In the end i could do it, so i ended up drawing a truth table:</p>
  
  <pre><code>Permission  Override   Result
      0           0        0
      0           1        1
      1           0        1
      1           1        1
  </code></pre>
  
  <p>Which when i look at it is an <strong>OR</strong> operation. So my <code>if</code> statement became:</p>
  
  <pre><code>if (HasPermission  or OverrideEnabled)
  {
     ...
  }
  </code></pre>
  
  <p>Which is obvious and simple. And so now i'm wondering how i couldn't see that to begin with.</p>
  
  <hr>
  
  <p>Which brings me back to my SO question: What tell-tale signs could/should i be looking for in order to recognize that some block of code needs some TLC?</p>