11
\$\begingroup\$

Often when I am reviewing code on Code Review I focus on readability of code, SRP, testing and things of that nature. For example, as I am writing an answer, I point out bad variable, function and/or class names and suggest (better) alternatives. I clearly state why bad names are bad in general and why I suggested the alternatives.

After going through a bunch of critique points I sometimes end up putting up a rewrite of the original code near the bottom of my answer. I usually only do this if the original question did not contain too much code. See this example.

I clearly do not give a "code only" answer with no explanation at all. I tend to give very thorough answers and always try to explain myself as best I can.

Is it acceptable to provide fully rewritten code in the answer of a Code Review question?

\$\endgroup\$
20
\$\begingroup\$

Is it acceptable to provide fully rewritten code in the answer of a CodeReview question?

Yes. Actually I done this the other day.

Do you have to? No.
Is it encouraged? No.
Is it discouraged? If it has the tag , otherwise no.
Does the OP like it? Probably.

An answer is quite personal, and so as long as you review the code, we can't slap you on the wrists. We may not upvote if we don't like the review, or disagree with it. But if code blocks help you review then keep using them. It's what they're there for. And don't forget, they're optional too. So an answer with no code can be fine too.

\$\endgroup\$
8
\$\begingroup\$

I certainly hope it's acceptable, as I try to always add the complete code to my answers!

I do this because I don't just read the code and suggest fixes, I implement them and ensure that the code continues to compile and run. I also execute the tests, when the OP is wonderful enough to include those. I find that doing the refactoring in my editor places me in a better state of mind; I'm just doing what I do everyday.

I usually precede the code with a long list of bullet points that detail each of the changes that I've made. I add these as code comments as I'm refactoring, then reformat them for human consumption before finally posting. If I'm feeling very ambitious, these are actually the git commit logs :-)

TL;DR Yes, it's fine to post the entire improved code, but use human-focused prose to describe what you did and why.

\$\endgroup\$
3
\$\begingroup\$

The Off-topic version of a Code-Only answer is just that, a Code-Only answer.

As long as your answer provides useful review of the OP's code, if you want to illustrate your points by writing your own version then it's absolutely fine.

\$\endgroup\$

You must log in to answer this question.

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