On Sep 20, 9:51 pm, desktop <f...@sss.comwr ote:
Are there any performance issues between:
bool run = true;
while(run) {
if (bob1 &&){
//dothing
break;
}
if(bob2) {
//dothing
break;
}
if(bob2) {
//dothing
break;
}
run = false;
}
The performance of people trying to read your code will suffer
horribly from something like this. The canonical form is:
if ( bob1 ) {
// ...
} else if ( bob2 ) {
// ...
} else if ( bob3 ) {
// ...
}
Obviously, the position of the braces will vary according to
local conventions; some local conventions even allow omitting
them if the if only controls a single statement. But except for
the possible omission of the braces, the token sequence should
be as above.
(I am aware that a switch could be used. But sometimes the
conditions consists of more parts and therefore a switch might
not apply)
With nested ifs:
if (bob1){
//dothing
break;
} else {
if(bob2) {
//dothing
break;
} else {
if(bob2) {
//dothing
break;
}
}}
Conventionally, "else if" is treated as if it were a specific
keyword in itself. The grammar of C++ allows this (and in
languages where it doesn't work, such as Ada, there is always an
elseif/elsif/elif keyword).
This is *the* universal convention. Anything else is
obfuscation.
I prefer the while version event though it might be a bit
unorthodox.
Not just unorthodox. Pure obfuscation, which will confuse
anyone trying to read your code. ("while" means a loop. If you
use "while" when you can't loop, you're lying to the reader.)
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34