Let's say I'm writing a method which can return two very different things based on parameters. I could write that bit of code like this:
function getMyThing(a) {
if (a == 0) {
// Do some stuff
return b;
} else {
// Do some stuff
return c;
}
}
This piece of code is clear and concise. It's easy to see the program flow. But it could also be written like this:
function getMyThing(a) {
if (a == 0) {
// Do some stuff
return b;
}
// Do some stuff
return c;
}
To me, this bit of code seems less clear, but it also seems slightly more efficient. While I understand that such a minor optimization is usually irrelevant, it would bug me if I didn't know the answer. Is this sort of thing optimized by the compiler? If not, which is the preferred method of writing code?
ifstatement. In this case omitting theelsestatement doesn't add much benefit. – Groo Jul 16 '12 at 22:20