Hypothetical code makes poor Code Review questions, which produce poor answers.
For example, here is a code excerpt that made a poor Code Review question:
bool f(const string& key, const string& value)
// some codes for input check
exec_cmd("cmd %s %s", key.c_str(), value.c_str());
// some codes for making return value
bool f(const ...
Hypothetical code is difficult or impossible to review because it is a moving, shape-shifting target. For example, @elibyy asked:
for example i'm deleting file if it exist
An answer was given:
You could just call
When the question was posted, your question was probably perfectly on-topic. However, as sites grow and evolve, their scope and requirements for on-topic questions change.
In this case, it is primarily the placemark-holder // do something that trigged the closing of the question.
It is more explained in the meta question Why is hypothetical example code ...
Yes, example code as a small part of a question, clearly labeled as example not for review, is perfectly fine, and even helpful.
That is assuming the rest of the question is real, fully working code, as per our on-topic rules in the help center.
And as @Mast pointed out, keep in mind that it's difficult to get good answers when you post a lot of code. You ...
Example Project vs Example Code
It's example code that we don't want to see. Example code is hard to review because it either:
lacks sufficient context (and/or)
has identifiers with names like foo and bar (and/or)
has methods with placeholder comments // ... instead of actual code
@200_Success does a better job at describing this, here (in an answer to ...
I went edited the question and voted to reopen. I split the real, reviewable code away from the example call, which is now in a quote block. The edit bumps in into the review queue for the community to decide if it should be reopened. I expect that it will be. Thank you for bringing this to meta.
The community has voted to reopen this question. Thank you ...
In Keeping track of byte count in a binary protocol handler, we had an example of a question that originally contained stub code that did not accomplish very much. After being put on hold, the author added more code. It then became apparent that there were deeper problems that needed to be addressed.
In an attempt to simplify his question,
So my first thought is: Should the question really have been simplified? Was there any comments suggesting that it should? On Code Review, we do want the actual code, we do not want any simplifications or any short and simple examples (like Stack Overflow).
he made some edits to his code that made his code not ...
If you look at what got OP's question closed for a while, it was the omission of a parameter name in a legit edit. All of the other code was completely valid and was easily understandable.
That is just wrong. The code in question had been this
public static class MyObjectExtensions
public static IEnumerable<MyObject> FindAllObjectsRecursive(...
Example code lacks context. This is mentioned in @200_success' answer to Why is hypothetical example code off-topic for Code Review?
Example project has created context for the code. The context might have been created just as a learning exercise, but I see nothing wrong with that.
Is posting and reviewing example projects here a constructive activity? ...
What is an example?
Meriam Webster defines example as:
1 : one that serves as a pattern to be imitated or not to be imitated [a good example]
2 : a punishment inflicted on someone as a warning to others; also : an individual so punished
3 : one that is representative of all of a group or type
4 : a parallel or closely similar case ...
An example of an example project that's not example code:
The Alfred question were discussed quite a bit in chat last night.
I think that the Alfred question should stay as on-topic because:
The code is not example code. It is the project that has been constructed for reasons of learning some design patterns, but there's nothing wrong with that. The ...
This is a bit of an edge case, but I'm not sure it is example code with no real purpose.
The question is poorly written (and everyone is welcome to improve it by editing!), but the OP does state the following:
one friend of mine said it's difficult to read, but it works
This implies that the code
is part of a real project and
satisfies the ...