As someone who occasionally posts "try this {code}" answers, I would like to explain my reasoning.
In most of the languages I program in, code is surprisingly close to natural language. For example, let's say someone asked a question like
How can I do something like console.log
, but log it to the page instead of the console?
I might answer such a question with:
Try this:
var div = document.createElement('div');
div.appendChild(document.createTextNode("Hello, world!"));
document.body.appendChild(div);
I would hope that it's fairly obvious that this code:
- Creates a
<div>
element.
- Appends a newly created text node with "Hello, world!" in it
- Appends the div to the body
In fact, commenting this particular code or explaining the it seems... kind of redundant. I'm practically repeating myself.
However, this doesn't automatically mean that "try this {code}" answers are okay.
For example, if the {code} given is simply a copy-paste of the OP's code, but with a tiny adjustment, then the answer is poor quality because the code does not explain itself. In these cases, I usually comment with:
While this does solve the problem, it's difficult to see why. Please consider editing your answer to include an explanation of what you changed, why you changed it, and why it solves the problem.
Sometimes I'll downvote if it's particularly obscure, like just a single character difference, or if the edit made is only visible after scrolling horizontally, but overall, since it does answer the question, it's not really something I'd consider flagging for deletion.
Overall, it's really down to a case-by-case basis how I treat this kind of thing, and I believe my opinion on this is reasonable enough to be shared :)