9
\$\begingroup\$

I have stumbled across some questions where it is hard to determine whether or not the code in the question actually works or not.

Example #1: https://codereview.stackexchange.com/questions/35616/replacing-parenthesis-in-code

Example #2: Creating a binary search tree

This has got me thinking, can we do something to encourage users to add a snippet of code that proves that there code is working as it should? Kind of like a small unit test, it does not have to be a long snippet, probably just a couple of lines for most questions.

Of course it is not possible to add such a snippet for all questions here, but I think that for many questions that ask about code that is independent from other things it would be useful.

A big advantage of providing a testing code snippet is that it shows reviewers how their code is meant to be used, what one or more entry-points to the code is. I believe this would help the reviewers getting a better understanding about their code in general.

So can we (or should we) do something to encourage users to include a code snippet that proves that their code works and/or how it is used?

\$\endgroup\$
  • 1
    \$\begingroup\$ Another advantage is that, if the reviewer wants to, they can more easily 'pull' the code in to their own system and actually run it without having to create a test system... like I did here: codereview.stackexchange.com/questions/35552/… \$\endgroup\$ – rolfl Nov 19 '13 at 2:35
5
\$\begingroup\$

this is a fine line issue I think, but like you were saying, they wouldn't be able to do this for all questions.

but I think that we should be able to encourage the users to post something that shows that their code works, especially when several people say that the code doesn't look like it should function properly.

and even when it is not debatable we could do as @retailcoder suggests

Perhaps kindly ask the OP when the code is non-trivial?

sometimes the code doesn't allow for easy testing to be shown, but when it is something where you can show what it does, yeah I think the OP should show it off!!

what programmer wouldn't take an opportunity to show off their code?

\$\endgroup\$
  • 1
    \$\begingroup\$ Perhaps kindly ask the OP when the code is non-trivial? \$\endgroup\$ – Mathieu Guindon Nov 18 '13 at 20:20

You must log in to answer this question.

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