A lot of questions popped up while I was looking at my code. I would like to know what people think about some code snippets. For each one, I am wondering what is more optimized, and what is better practice or cleaner. Most likely, any possible optimization is on the scale of nanoseconds, I am just wondering theoretically. :)
1)The first one is a frequently called routine that needs to report an error. Right now it looks like this
if(success)
//Stuff
else
reportInternalError();
Is any benefit to changing it to
if(success) {
//Stuff
return;
}
reportInternalError();
2)reportInternalError() is supposed to only report the error the first time it happens, and it can potentially be called very frequently. Currently, it is like this
if(!reported) {
//Report error
reported = true;
}
I am thinking about changing it to
if(reported)
return;
//Report error
reported = true;
3)The last one is about duplicated code. I am using the exact same code twice, and I am trying to figure out a better way of writing it.
try {
//Stuff
} catch(){
}
while(loop) {
try {
//Same Stuff
} catch(){
loop = false;
}
}
Versus
first = true;
do {
try {
//Stuff
} catch() {
if(!first)
loop = false;
}
first = false;
} while(loop);
Thanks!