Reputation: 3
Is there a pro or con to doing simple validation one way or another? What is the best practice? When would you use one over the other?
first method:
if ( <validate> ){
submitForm();
} else {
alert('Error');
exitFn();
}
second method:
if ( !<validate> ){
alert('Error!');
exitFn();
}
submitForm();
Upvotes: 0
Views: 251
Reputation:
You're actually asking two separate questions:
In an if-then-else structure, in general should I handle error conditions first and normal flow second, or vice versa?
If I handle error conditions first, should I return after handling, or put the normal flow logic in an else
block?
Both of these are matters of personal taste. Having said that, with regard to the first point, I think you would find that many programmers would put the error handling first. On the second point, opinions are split. Here is a question on this topic.
By the way, I don't know what you mean or intend with exitFn()
. There is no such capability in JS.
My personal preference, and that's all it is, would be:
if ( !<validate> ) {
alert('Error!');
return;
}
submitForm();
The advantage here is that the error handling is usually shorter and it's better to get it out of the way as soon as possible, rather than force someone reading your code to go down a dozen or two dozen lines to see how (or if) errors are being handled; also, putting the normal flow within the if
will add an extra level of indenting.
Upvotes: 1
Reputation: 815
if you want to exit the function in case not validated then it would be optimized
if ( !<validate> ){
alert('Error!');
return;
}
submitForm();
Upvotes: 1
Reputation: 2221
The second method is called a guard. It's useful for delimiting a clear block of condition checking and error handling at the top of a function.
It's a common pattern which has the benefit of being recognisable to people reading your code.
It is important that guard checks leave the function if they fail, otherwise those two code blocks are not equivalent.
Example:
function a(b, c) {
if (b.length == 0) {
alert("can't proceed, first argument is empty");
return;
}
if (c.length == 0) {
alert("can't proceed, second argument is empty");
return;
}
doFancyStuff();
}
If you were to do this in the first form it would look like this.
function a(b, c) {
if (b.length == 0) {
alert("can't proceed, first argument is empty");
} else if (c.length == 0) {
alert("can't proceed, second argument is empty");
} else {
doFancyStuff();
}
}
Upvotes: 1
Reputation: 1447
Generally, it would depend on the context - it can be useful to rely on an 'if' on its own if you're just processing a string, but it is better to handle a more boolean function by capturing both results. Any situation where you have an 'else' or an 'else if' (for multiple conditions) means you're filtering more reliably, rather than just rejecting an input
I'm not sure I'd feel comfortable with the second example, but that might just be down to how I've been conditioned with regards to conditional statements.
It does strike me as a matter of opinion, rather than anything strictly right/wrong.
Upvotes: 0