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 string& key, unsigned long value)
{
// some codes for input check
exec_cmd("cmd %s %lu", key.c_str(), value);
// some codes for making return value
}
Why is this question problematic? It has been stripped of all context, making it hard to give concrete advice. Some of the warning signs are:
- Generic function name
f()
. The question is based on a true story — in other words, it's fictitious, hypothetical code.
- Probable generic string
"cmd"
.
- Placeholder comments (with insufficient detail to mentally reconstruct the code that goes there).
- The function should return a
bool
, but we don't know what that bool
represents.
- We can speculate what
exec_cmd()
does, and by extension, what f()
does, but we're not really sure.
Compare what happens when the author fills in enough details to resolve three of the five deficiencies:
bool set(const string& key, const string& value)
{
// some codes for input check
redisCommand("SET %s %s", key.c_str(), value.c_str());
// some codes for making return value
}
bool set(const string& key, unsigned long value)
{
// some codes for input check
redisCommand("SET %s %lu", key.c_str(), value);
// some codes for making return value
}
Now, we can see exactly what the code is supposed to do, and can give better advice. For example, previously we could only speculate that there was a security hole in the code. Now that we know that it is trying to set a value in a redis database, the mitigation advice would be completely different.
We don't normally allow code in questions to be edited like that, since it invalidates existing answers. However, in this case, the original question was so poor that allowing the revision was a less bad option. Ideally, the original question should have been closed as "not real code" instead of being reviewed, to avoid such a mess.