1

I think a useful although grandiose feature would be to allow SO to run posted code snippets inside a secure sandbox (for various languages) for the purposes of: finding syntax errors, validating code, sometimes producing and posting actual results or output.

I would find this feature useful because I don't have all compilers frameworks and tools installed for which I would like to quickly try some of the unique stuff I see posted on SO. Also source code answers could be validated at deeper levels for weighting usability, etc. Also it might be great just to see the immediate results of code, inline with the posted answer, rather than mentally processing it.

The words "results" and "output" are used loosely here and could be anything from a parse tree to a step-through of code to actual console text or anything in between, whichever representation best suits the situation at hand. It could be automatic or chosen by the poster.

This feature request might seem really "out there" but it's fun to speculate about (well, for me). With recognition a lot of thought and work would have to go into it, but anything is possible if desirable.

One of the biggest challenges would be the fact code is often a fragment so running the native compiler would produce lots of errors. However syntax highlighting on SO is already a step in the right direction because it proves recognition of language features to a degree. Missing code might be mocked by a tool.

2
  • 5
    Why has this suggestion being downvoted? Even if the general consensus is that it would be a bad idea, I think bringing the issue to attention deserves merit, as it exposes the arguments for and against. IMO we should reward for bringer of news by their aptitude, rather than whether the news they bring is good news or bad news.
    – P i
    Oct 24, 2010 at 7:09
  • 4
    @Ohmu it is the custom on Meta to downvote suggestions that one disagrees with, even if it is perfectly worded and a fine suggestoion in itself.
    – Pekka
    Mar 23, 2011 at 19:41

6 Answers 6

13

I'm pretty sure that such a sandbox would immediately become the site of the hottest new game of who can bypass the sandbox and compromise the machine. Plus, this would be a huge job to set up and support, and who would decide what languages, frameworks and environments get supported? There are so many different configurations, and a lot of SO questions are about these subtle differences between environments.

If you want to set up a prototype to demonstrate how it would work, knock yourself out, but I doubt anyone official with SO would spend any time with this.

PS. Sorry for being grumpy today. :(

1
  • 2
    From a security perspective dead on the money.
    – John K
    Dec 11, 2009 at 0:28
6

Its a fantastic idea, and I'm sure that it would see a lot of use, but it strikes me as something more suitable as a separate project.

It would have much wider uses than just StackOverflow but it would also be far from trivial, especially if you wanted to support a number of different languages.

2
  • I like the idea of a separate project for wider usage.
    – John K
    Dec 10, 2009 at 23:55
  • 4
    There is also the problem of supporting different versions of each language. Perl 5.6.0 != Perl 5.8.0 != Perl 5.10.0 Dec 11, 2009 at 0:21
4

http://codepad.org/ does this beautifully, for C, C++, and a bunch of other languages.

This would be an excellent feature in SO. A single click to launch the code in codepad.

the obvious problem with this is that code examples would no longer be optimally concise. but good solution to this would be some form of highlighting.

currently each line of code must be preceded by four spaces.

if you were to alter it thus:

    //TESTCODE: 
    #include <math.h>

->  float mod(float a, float N)
->  {
->      float ret = a - N * floor (a / N);

    printf("%f.1 mod %f.1 = %f.1 \n", a, N, ret);

->      return ret;
->  }
    
    int main (char* argc, char** argv)
    {
        printf ("fmodf(-10.2, 2.0) = %f.1  == FAIL! \n\n", x);

        float x;
        x = mod(10.2f, 2.0f);
        x = mod(10.2f, -2.0f);
        x = mod(-10.2f, 2.0f);
        x = mod(-10.2f, -2.0f);

        return 0;
    }

SO's Display mechanism should know initially only to display highlighted lines prefixed by '->'

a faint horizontal jagged line could indicate that there is hidden code not essential to understanding the answer.

there would be a button to toggle ' expand all / collapse '

when everything is expanded, '->' lines would be in bold

The first line: '//TESTCODE:' tells SO that this code is capable of being run in sandbox

so a second button 'test & run code snippet' would appear

when the user clicks on this, it could just open it up in CodePad. SO wouldn't even need to take the liability of malicious code attacks.

on which note, we could only allow users of > 100 to use this feature, and warn them that usage is logged, and they will lose their points AND/OR membership if they get caught trying to hack it.

then SO could allow users to gain points by fixing up code chunks everywhere. and if they don't have permission to fix a particular chunk, they could flag it with a request, and supplying a replacement, and a senior user could look over it and allow it with a single click.

Remember, points are increasingly hard to come by now that the easy questions have been asked. So it would be great to allow users to establish themselves a basic reputation by doing little jobs like cleaning and tidying.

and linking posts! but I will write separately on this as it is another topic.

another benefit of testable code is that it would also encourage contributors to supply code in usable units.

2
  • 1
    There are still plenty of users that can't even format code correctly with leading spaces so it gets in a code block, much less indent it so it's readable — while this might be a good idea on a different site, it's way too much complexity for SO.
    – Gnome
    Oct 24, 2010 at 8:47
  • As far as codepad: I use it often and use its saved paste feature to permalink from SO to output ( example, example ). It's always a good idea to give complete, working code that can be compiled/run (even if it doesn't do much), anyway. If it's really important to show the output, I'll copy it right into the answer.
    – Gnome
    Oct 24, 2010 at 8:50
2

For Javascript, I typically use links to Google's Code Playground which allows you to execute Javascript code written in the browser. Very neat. I think a similar concept would be difficult to deploy for compiled languages that couldn't be compiled on the client's machine.

1
  • That would certainly be an interesting tool to run the JavaScript snippets posted to SO.
    – John K
    Dec 11, 2009 at 0:27
2

There's a project: JSBIN

www.jsbin.com that does this for javascript.

There's also a python interpreter written in javascript (pyjamas) a C interpreter in javascript (ClueCC) a ruby interpreter (hotruby) a java compiler (gwt) and others. However, unless the question is language or compiler specific, javascript is sufficient for questions about algorithms, and the vast swath of SO questions that actually are about javascript, or html, or other such things.

1
  • Very cool possibilities; thank you.
    – John K
    Dec 11, 2009 at 1:51
1

There's also http://ideone.com/

What is ideone?
Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.

You must log in to answer this question.

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