I am not sure wether I should post this here or not, but I just had a debate about C++ good practises with a coworker today and I can't find the point we were discussing in the CppCoreGuidelines or any forums.
We were talking about "if" "else if" "else"
statement, and my coworker was saying that if you have a "if - else if"
statement then you must put a "else"
, even if it is empty.
For example, in his opinion, things like this is no good practise.
if (condition1)
{
// Some instructions to do when condition1
...
}
else if (condition2)
{
// Some instructions to do when condition2
...
}
and I should rather write this code :
if (condition1)
{
// Some instructions to do when condition1
...
}
else if (condition2)
{
// Some instructions to do when condition2
...
}
else
{
/* Do nothing */
}
His main point was that just like switch
statements must have a default
case, if - else if
statements must have a else
.
I have several questions :
答案 0 :(得分:4)
This is all very personal.
is that true ?
From a language legality perspective, no that's false.
Some guidelines might want programmers to explicitly add else
statements but personally this sounds like an anti-pattern to me.
答案 1 :(得分:3)
my coworker was saying that if you have a "if - else if" statement then you must put a "else", even if it is empty.
No style guide I have ever come across says that. And, if there is one, it's just a matter of opinion.
I do sometimes write "empty" else
blocks, but only when they contain some interesting, useful, explanatory documenting comment about why nothing's happening in the else
case when it might at first glance appear like a natural thing to do.
if (FozzieBearIsAlive())
{
GiveMissPiggyATreat();
}
else
{
// Miss Piggy doesn't deserve a treat, because she may
// have killed Fozzie Bear
}
It's a pretty contrived example, which could otherwise be written like this:
// Give Miss Piggy a treat, but only if Fozzie Bear is alive
// because otherwise we might wonder whether she killed him
if (FozzieBearIsAlive())
{
GiveMissPiggyATreat();
}
But sometimes the former is nicer.
Mandating that you always include some empty else
block with nothing in it, though, is something I have not heard of and would not support.
His main point was that just like switch statements must have a default case
Also false, except in the sense of some subjective style guide.
In fact, when switching on enums, I discourage default
cases unless you functionally require one, because you prevent the compiler from warning you when you add an enumeration and forget to update all your switch
es.
答案 2 :(得分:2)
First of all, switch
statements do not require a default
case. In fact, they may be completely empty:
switch (...) {
// fine
}
Similarly, if
does not require an else
.
Some guidelines have strange rules, and some may indeed require to always have all possible branches explicitly written down in the code. However, it is very rare to do so, specially with branches.
I have personally seen very weird requirements out there on contracted code, so I can believe some guidelines/certification/contract/... may force it, specially older ones. My guess is that they want to be "formal" or maybe they hope it helps with "code quality" or with formal verification (if done manually, for some reason...).
答案 3 :(得分:1)
这实际上取决于您正在从事的任何项目/公司的编码准则。除此之外,这是个人喜好。
例如,我目前的公司希望始终确保switch语句有default
的情况,并为if语句添加else
子句。
只需注意您公司/项目的指导方针,您就会变得很好。