<p>If code doesn't work and is necessary to understand the question, then the question is not clear, and it can be closed for being unclear and requiring clarity.</p>
<p>Also, ideally a code shouldn't be necessary to understand a question. It should be like an appendix to explain an idea further.</p>
<p>I personally like to use code in answers and questions. Sometimes I use it as a more essential part of an explanation and then I ensure that it can be read as pseudo-code. I do this especially in questions where people explain their question with a python code and I answer with a question that contains R code. Then I make it such that the code is clear to understand. In that case, if it doesn't work, then it should still be understandable. For such cases where a code speaks for itself, it is still desirable, but shouldn't be necessary that the code can run without errors or loading packages.</p>
<p>An example of such use of code is in an answer to the question <a href="https://stats.stackexchange.com/questions/616591/">Why does this multiple linear regression fail to recover the true coefficients?</a></p>
<blockquote>
<p>In R you get the same behavior when we use the function <code>lm</code> which
gives as output</p>
<pre><code>&gt; lm(y~X+0)

Call:
lm(formula = y ~ X + 0)

Coefficients:
   X1     X2     X3  
-0.02   0.94     NA
</code></pre>
</blockquote>
<p>Here I use code in an explanation. But I speak about only a single line out of a larger code. So technically it doesn't work.</p>
