C# Code Simplification Query: The Null Container and the Foreach Loop
<p>I frequently have code that looks something like this:</p>

<pre><code>if (itm != null)
{
    foreach (type x in itm.subItems())
    {
        //dostuff
    }
}
//do more stuff
</code></pre>

<p>In situations where <code>//do more stuff</code> is omitted, it is very easy to avoid the extra foreach loop.  By exitting scope using the appropriate command (depending on what is going on, this generally would mean a <code>return</code> statement or a <code>continue statement</code>).</p>

<p>This type of thing tends to result in arrow code.  I currently have a few ways to deal with this:</p>

<ul>
<li>Use code like <code>itm = itm == null ? itm.subItems() : emptyArray</code></li>
<li>Allow arrow code</li>
<li>Use <code>goto</code></li>
<li>Use evil scoping hacks (wrapping the whole thing, if statement in all, in a scope and then breaking out of it).  In my opinion evil scoping hacks are basically equivalent to <code>goto</code> except uglier and harder to read, so I don't consider this a valid solution.</li>
<li>Refactor some of the chunks into new methods.  There are in fact a few cases where this probably <strong>is</strong> a good solution, but mostly it's not appropriate since the null references are mainly error conditions from MS-functions.</li>
</ul>

<p>Anyone care to offer a response on what approaches are considered preferable?</p>
