2
$\begingroup$

Many people include code in either their questions or their answers, in R or Python or some other language. That's fine. I'm not talking about questions that fall under the 'asking for code' category.

But sometimes the code doesn't run at all, or gives different output, or requires some more code in order to work e.g. in R, installing and loading a particular package. The last is especially problematic when either a) the function in the code is in more than one package or b) the package isn't in CRAN.

What should we do with these? Should there be a new reporting option? Should we correct the code (if we can)? Downvote the question or answer? Or what?

$\endgroup$
8
  • 8
    $\begingroup$ I usually close such questions with a comment explaining that providing problematic or unworking code is not an effective way to ask a question. I request that the question be framed in English and mathematical notation (if necessary). I'm unsure what you mean by "reporting option," but if it's a boilerplate reason for closure, please remember we have no more slots for that: something else would have to be removed from the list of close options first. $\endgroup$
    – whuber Mod
    Dec 24, 2023 at 17:07
  • 2
    $\begingroup$ I was not aware that the number of slots are fixed and a new reason could be added only by removing an existing one. I wonder what would happen in future if we need another one apart from the existing ones, although to be fair, I do think the current ones encompass every situation @whuber. $\endgroup$ Dec 24, 2023 at 17:12
  • 5
    $\begingroup$ @User1865345 Not only is the number limited, but so is the length of each closure reason. (I recall we have 500 characters.) That's why the messages are so terse! This is a limitation throughout SE, I believe -- it's not something we can change. $\endgroup$
    – whuber Mod
    Dec 24, 2023 at 17:21
  • $\begingroup$ Wow! Thanks for the clarification, @whuber. $\endgroup$ Dec 24, 2023 at 17:22
  • $\begingroup$ I didn't know that either. In that case, I agree this problem doesn't merit another slot. $\endgroup$
    – Peter Flom
    Dec 24, 2023 at 17:38
  • 2
    $\begingroup$ Code as illustration can be helpful, but as no language is used by all people here, code that is difficult or impossible for others to follow can only make many posts very unclear. $\endgroup$
    – Nick Cox
    Dec 26, 2023 at 14:25
  • $\begingroup$ @NickCox That's true. And I know questions about code are off topic, but sometimes, especially with more esoteric statistical procedures, it may be very helpful to have the code. While it's true that there is no language that all of us use, I think quite a lot of the "usual answerers" do know at least some R. $\endgroup$
    – Peter Flom
    Dec 26, 2023 at 15:38
  • 1
    $\begingroup$ The point is that any status R has as a lingua franca is limited in practice and not a matter of principle. $\endgroup$
    – Nick Cox
    Dec 27, 2023 at 11:20

2 Answers 2

7
$\begingroup$

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.

Also, ideally a code shouldn't be necessary to understand a question. It should be like an appendix to explain an idea further.

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.

An example of such use of code is in an answer to the question Why does this multiple linear regression fail to recover the true coefficients?

In R you get the same behavior when we use the function lm which gives as output

> lm(y~X+0)

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

Coefficients:
   X1     X2     X3  
-0.02   0.94     NA

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.

$\endgroup$
4
  • 1
    $\begingroup$ I can't find better examples of the use of code in an explanation. But I do remember that sometimes I add some piece of code that explains something (like an algorithm) and a voice in my head tells me that I have to write it very clearly and simple such that anybody that does not know R can still understand it. $\endgroup$ Dec 27, 2023 at 7:51
  • 1
    $\begingroup$ So for me, code is also sometimes used to write out an algorithm or a function, and potentially might not actually work or execute. $\endgroup$ Dec 27, 2023 at 7:56
  • $\begingroup$ ideally a code shouldn't be necessary to understand a question. It should be like an appendix to explain an idea further. ... I ensure that it can be read as pseudo-code. +1. $\endgroup$ Dec 27, 2023 at 8:12
  • 1
    $\begingroup$ +1. About "ideally a code shouldn't be necessary to understand a question", an exception might be when the question is about why different software/packages/languages give different outputs when (apparently) using the same statistical method. In this case, even when the underlying issue is really about statistics, being able to replicate the different outputs with the original code could be essential to answer the question. $\endgroup$
    – J-J-J
    Dec 27, 2023 at 21:47
4
$\begingroup$

If you are confronted by a dubious chunk of code that needs clarification, leave a comment explaining why the script, as it stands, doesn't make any coherent code/meaning.

If OP responds and follows suit with editing the relevant script, then that's good. Otherwise you can close the question on the ground of "Needs details or clarity"; as for answer depending on the whole content, you can either flag it as "Not an answer" or in serious situations, "In need of moderator intervention". Whether to downvote or not depends on the context and severity of the problem in hand.

I don't think it warrants any additional closure reason/reporting option - the existing ones serve well.

As for whether you should correct the code, I again think it depends on the context but I prefer the correction must come from OP's pov.

$\endgroup$
2
  • 4
    $\begingroup$ If the code is both essential to understanding the q. & bad or incomplete enough to preclude understanding it, then the q. should be closed right away. Contrariwise, if the q. Is still answerable despite the bad or incomplete code, there's no reason to close it $\endgroup$ Dec 24, 2023 at 17:35
  • 1
    $\begingroup$ Exactly @Scortchi-ReinstateMonica. Closure should depend on case by case basis. The context and severity do matter. $\endgroup$ Dec 24, 2023 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .