views:

138

answers:

6

Hey all,

Is it good practice to use "return false;" to basically say do nothing in an if statement? For example:

if (navigator.userAgent.match(/iPad/i) != null) {
    return false;
} else {
    //Usual script here
}

just wondering if there are any downfalls to this. I can use the if statement without the else but i'm just wanting to get insight on this. I have a plugin that i do not want running on iPad and so I'm wrapping it in the conditional. any comments would be appreciated!

+3  A: 

In my experience only if you were actually looking for the false to be returned.

snkmchnb
In other words, if you want to "do nothing", it's better to actually "do nothing" than to "get out of here and tell them it is false". When "doing nothing" it's also good to make it explicit you're "doing nothing" and not just "forgetting to put code in" with something like `// Do nothing`
Martinho Fernandes
Thanks for the info this certainly helped me gain the insight I'm looking for.
designerdre101
A: 

You should only return a value if a caller is going to do something with that value. If you want to do "nothing" in an if statement, it's a sign that your logic is wrong. Change your statement to this:

if (navigator.userAgent.match(/iPad/i) == null) {
    //Usual script here
}

This way you don't need to "break" out of your function with the return (not a good practice in this scenario).

Vivin Paliath
I actually prefer to "do nothing" in an `if` statement sometimes to make my code look cleaner (instead of indenting further).
palswim
I don't get that though. Why have `if(condition) {} else { //do something }` when you can have `if(!condition) { //do something }`
Vivin Paliath
It's never with a single `else`, but more like `if(!condition) {} else if(condition 2) { /* Do something */ } else { /* Do something else */ }` instead of `if(condition) { if(condition 2) { /* Do something */ } else { /* Do something else */ } }`. It makes my code use less indentation, and I figure a compiler will optimize that however it sees fit.
palswim
I prefer indentation over empty `if` blocks. That speaks of a problem with logic to me :)
Vivin Paliath
Thanks for the info this certainly helped me gain the insight I'm looking for.
designerdre101
+1  A: 

You can actually simplify it further, like this:

if (navigator.userAgent.match(/iPad/i) != null) return false;
//Usual script here

Is it "good practice"...sure, if it works for you and your team. Jumping out of a function as soon as you have nothing more to do there is a very efficient way to do things, as long as it's easy to understand and maintain for whoever is working on it.

Whether you want false specifically depends on the situation, for example if you want to return but not prevent other event handlers from running later, you may want return true; instead.

Nick Craver
Thanks for the info this certainly helped me gain the insight I'm looking for.
designerdre101
+5  A: 

Group 1 will say it is horrible practice since it is hard to follow.

Group 2 will say do it.

Group 3 will say do it, but in 1 line

Group 4 will say do not use the else

Group 5 will say to do not use the return, just use the if around the code you want to run. AKA:

if (navigator.userAgent.match(/iPad/i) === null) {
    //Usual script here
}
epascarello
What happens if all 5 groups walk into a bar?
Peter Ajtai
You'd have one heck of a fight. Bud Spencer style!
cwap
Group 6 will say you're not using the correct bracing style
Stephen P
A: 

I agree with snkmchnb, otherwise just negate the condition. You can negate long expression using these:

!(a && b) = !a || !b
!(a || b) = !a && !b

And use these several times to get what you want. For example, negating a long expression

!( (a && b || c) && (d || e) || f) =
    !((a && b || c) && (d || e)) && !f =
    (!(a && b || c) || !(d || e)) && !f =
    (!(a && b) && !c || !d && !e) && !f =
    ((!a || !b) && !c || !d && !e) && !f

This looks ugly now, but negating most times doesn't mean over-complicating. For example negating "<=" results in ">"

So never use !(long_expression) neither:

if (long expression)
{
}
else
{
  //do stuff here
}
SinistraD
For reference, these are called the DeMorgan laws (http://en.wikipedia.org/wiki/De_Morgan's_laws). And you have a typo in the second law: you want bangs, not pipes.
Martinho Fernandes
@Martinho - bangs? --- oh you mean exclamation marks... `not` s? ==> `!`
Peter Ajtai
@Martinho Fernandes right, thx
SinistraD
Thanks for the info this certainly helped me gain the insight I'm looking for.
designerdre101
A: 

firstly it is very good practise, take this example

var window.__page_loaded__;
var Loadpage = function ()
{
    if(window.__page_loaded__ != undefined)
    {
         return; //The page has already laoded
    }

    //Proceed to load the page
}

by using the return; you doing the same as you would with an else statement but without the extra block, and as Loadpage() would normally not return any data its perfectly fine to short cut your code.

RobertPitt
Thanks for the info this certainly helped me gain the insight I'm looking for.
designerdre101