- #1
Jamin2112
- 986
- 12
Suppose I have a function that gets called and performs a procedure provided that a certain condition is met. One way to do it is, of course, like the following real example from something I'm working on in JavaScript right now.
That's how they do it here http://www.htmlgoodies.com/beyond/j...e-javascript-filereader.html#fbid=gN9gIXxlV_E and of course it's perfectly valid.
However, to me, it seems weird to have it written that way, because the way I think about things like this is, "Check that I'm not prevented from doing X, and if I am, make a note about it, otherwise continue onto doing X." So I prefer to use an early
statement in languages that support it, such as JavaScript. I do this:
I'm just wondering whether one is definitely better than another or whether my mind is starting to become poisoned with religion after reading too many threads here haha. I also realize that the first piece of code is faster because
is, on average, executed faster than
.
Should I even be asking this question or am I becoming overly concerned about things that don't matter?
Code:
if (window.File && window.FileReader && window.FileList && window.Blob) {
//do your stuff!
} else {
alert('The File APIs are not fully supported by your browser.');
}
That's how they do it here http://www.htmlgoodies.com/beyond/j...e-javascript-filereader.html#fbid=gN9gIXxlV_E and of course it's perfectly valid.
However, to me, it seems weird to have it written that way, because the way I think about things like this is, "Check that I'm not prevented from doing X, and if I am, make a note about it, otherwise continue onto doing X." So I prefer to use an early
Code:
return
Code:
if (!window.File || !window.FileReader || !window.FileList || !window.Blob)
{
alert('The File APIs are not fully supported by your browser.');
return;
}
else
{
// do the procedure
}
I'm just wondering whether one is definitely better than another or whether my mind is starting to become poisoned with religion after reading too many threads here haha. I also realize that the first piece of code is faster because
Code:
window.File && window.FileReader && window.FileList && window.Blob
Code:
!window.File || !window.FileReader || !window.FileList || !window.Blob
Should I even be asking this question or am I becoming overly concerned about things that don't matter?