13

I feel like this is a solution to many questions about language behavior, where it could be simply tested. And not the kind where you could type code to see if there was an error, code that would find the behavior but must be run. I've thought that on the one hand, there isn't a need for the question, it could be tested, however this is sometimes an inconvenience. On the other hand, the asker can benefit from the knowledge of an answer without this inconvenience if someone already knows the answer. Sharing knowledge versus expecting everyone to find it.

9
  • 4
    Not sure if I completely get you. But if it is as your title says then yes, compile and run the code and see if it works.
    – codeMagic
    Commented Oct 23, 2014 at 19:21
  • 21
    Be aware that for many questions about language-behavior, testing is a very vague signal, only language-lawyering really gets you an answer. Commented Oct 23, 2014 at 19:22
  • 1
    My answer at Asking about advice to use one operator instead of another was poorly received is related, I think.
    – jscs
    Commented Oct 23, 2014 at 19:36
  • 1
    Do you have some good samples at hand? You should mentioning them in your question then. Commented Oct 23, 2014 at 19:50
  • 7
    @Deduplicator The fact that testing may not provide a conclusive answer. You still need to test, and you still need to explain the results of your tests in a question, in the event that you need help determining a conclusive answer to a question, rather than just an experimental indication.
    – Servy
    Commented Oct 23, 2014 at 20:22
  • 1
    How would you know if the observed behaviour is guaranteed or what a particular compiler does? In C and C++ it's even worse, since there is a lot you can do and that appears to work but is actually undefined behaviour. Commented Oct 25, 2014 at 14:25
  • 2
    Many question here are of the "debug my code type" and require the effort to set up a copy of some kind if you want to help. Not testing before posting here is kind of extra lazy.
    – mvw
    Commented Oct 25, 2014 at 14:50
  • What about languages with multi-implementations? Language lawyering?
    – Ven
    Commented Oct 25, 2014 at 14:51
  • @Ven: Did I not answer that adequately?
    – Ben Voigt
    Commented Oct 25, 2014 at 15:13

3 Answers 3

44

Yes, testing hypotheticals is part of doing your research before posting a question. If you have a question along the lines of "What happens when I put a function call as the argument to another function call?" or "Can I use a negative index on a string?", you need to spend a little while trying it out, seeing what happens, and trying to understand the results.

If you can't explain the results of your experimentation, try a few variations. Try to search on the web for similar situations. See what you can see in the documentation. (Notice how similar this is to the process of debugging.)

Keep track of your everything you find, and when you finally give up, you'll have a really excellent Stack Overflow question.

2
  • 1
    in practice, it increases the probability of XY problem i.e., the original issue is lost in a series of semi-failed solutions (semi- because it might compile/run successfully on a given compiler, architecture but it says nothing about different compilers (flags), architectures). The good answer might require great familiarity with the language standard and how it is implemented by various compilers on different architectures.
    – jfs
    Commented Oct 25, 2014 at 14:58
  • @J.F.Sebastian And this is why XY is over-invoked. SO is not supposed to be a helpdesk, so if someone's asking a question about a behaviour of, say, standard C++, I don't really care what their original issue is and they should not be looking for an altogether alternative solution. It's not a question on program design or design choices. It's a question about a behaviour of C++. Commented Oct 25, 2014 at 20:22
32

Note: This answer applies to languages which are standardized. Some have only a single implementation and no formal description; in that case you're still testing the implementation, but it makes no sense to ask about specified behavior if there is no specification.


As @Deduplicator alludes to in his comment, language behavior cannot be simply tested. Ever. Tests observe a specific compiler, with bugs, extensions, and half-implemented support for future versions. Only the language specification provides answers about language behavior.

That said, it is by far better to write a minimal example, compile (and run if possible), and include the results in the question. The question then takes the form of

I observed that (code X) gives (result Y) on (compiler Z, version V). Is this guaranteed by language standard version S to be portable?

Referencing parts of the Standard you've already read is also A VERY GOOD THING.

I think it is guaranteed by rule R.

or

This confuses me because according to rule R I was expecting result E.

If you don't have, or don't understand, your language Standard, then quoting from reference documentation instead is a reasonable substitute.

0

I think if it's just a question of "will this work" then the question can be considered lazy if the debated strategy is trivial; if however the asker is demanding "why will/won't this work", or the "will this work" refers to something laborious to implement, then the question is perfectly legitimate.

However even in the case of lazy questions, the information provided by the SE community will likely be holistic and of benefit not just to the asker, who will acquire an (unsought) insight into the relevant concepts à propos de his/her dilemma (which might well be germane to their wider project), but also to others who may have a wide range of queries or issues – if the question title is amended to make it more general and the asker is issued proper guidance on how to amend their question to be more general and generally more useful to the SE community.

You must log in to answer this question.

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