-3

Let's say I have three conditions: Condition1, Condition2, Condition3. If all the conditions are fulfilled then the method/function returns true else false.

Technique one:

     function check(){
        if(Condition1 is true AND Condition2 is true AND Condition3 is true){
           return true;
        }
        return false;
     }

Technique two:

      function check(){
         if(Condition1 is false){
             return false;
         }
         if(Condition2 is false){
             return false;
         }
         if(Condition3 is false){
             return false;
        }

         return true;
      }

Which of the techniques would be better?

Harke
  • 1,279
  • 4
  • 25
  • 30
  • It depends on your context. In this particular case I think the first technique is better. – MeLight Oct 04 '11 at 16:17
  • Better in which way? Both blocks do exactly the same. – Dennis Oct 04 '11 at 16:18
  • Can `return condition1 ? (condition2 ? (condition 3 ? true : false) : false) : false` be technique 3? – Xavi López Oct 04 '11 at 16:22
  • 1
    @XaviLópez Sure, it could, if you want to make people's eyes bleed. – Dave Newton Oct 04 '11 at 16:23
  • In return (Condition1 && Condition2 && Condition3); all the conditions has to be checked. But in technique 2, if a false condition arises then the number of conditions to check can vary from 1 - 3. So, in that situation won't technique 2 be better? – Harke Oct 04 '11 at 17:35
  • @Harke: You assumption that all the conditions have to be checked is not true. Logical operators are short circuited in javascript, meaning that unnecessary conditions will not be evaluated when calculating boolean operators. – recursive Oct 04 '11 at 17:55
  • @Harke: See this example: http://jsfiddle.net/PpAfb/ – recursive Oct 04 '11 at 18:02

8 Answers8

5

"Better" is subjective.

return (Condition1 && Condition2 && Condition3); // "Better" yet?

Depends on what's more readable, and maintainable, in the non-contrived code.

Personally, I'm a fan of returning as early as possible, when it makes sense to do so and it increases readability.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • +1. Although this way would be objectively better taking into account fast-failing of the whole condition in conjunctive forms, as [@porco](http://stackoverflow.com/questions/7650907/which-of-the-following-technique-is-better/7650946#7650946) pointed out – Xavi López Oct 04 '11 at 16:32
  • @XaviLópez It's only "objectively" better if the conditions can actually be accurately represented as above and the speed/readability ratio is worth the tradeoff. If the conditions aren't neatly wrapped up in a single variable, I'd say it isn't better, since you'd need to encapsulate them anyway. It's hard to tell from contrived examples, though :) – Dave Newton Oct 04 '11 at 16:39
2
function check(){
    return (Condition1 && Condition2 && Condition3);
}

I think it's better to have a unique return statement in a method.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
  • 3
    Why do you think so? And there is no `AND` is JS. – svick Oct 04 '11 at 16:19
  • Oh yes, I reused his example, without even thinking :) It's && of course (now edited) – Guillaume Oct 04 '11 at 16:23
  • 1
    @svick I think *in general* it's true, but only when the method's structure and purpose is more readable with the single return. The more return points the higher the cyclomatic complexity, and the more difficult it is to reason about. On the gripping hand, *forcing* a single return no matter what is just as evil, and can make things just as difficult to think about. – Dave Newton Oct 04 '11 at 16:25
  • There is a lengthy discussion about multiple return points in methods here: http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement - Dave perfectly summarized it. – Guillaume Oct 04 '11 at 16:25
  • @svick I was writing as a pseudo code and not JS specific. But in using this type of return, all the conditions has to be check no matter what. What is only one conditions is false? – Harke Oct 04 '11 at 17:38
  • 2
    @Harke: "But in using this type of return, all the conditions has to be check no matter what." Wrong. If `Condition1` is false, no further conditions will be evaluated. – recursive Oct 04 '11 at 17:56
2

I would use technique 3:

 function check() {
    return Condition1 && Condition2 && Condition3;
 }

No need to compare to true or even have an if block.

recursive
  • 83,943
  • 34
  • 151
  • 241
1

The top one is better, it will short circuit anyway. That means that if condition1 is false it won't check condition2 or condition3, so you don't save anything with the second method.

Also, skip the "is true" bit, you can just do:

return (Condition1 && Condition2 && Condition3)
Porco
  • 4,163
  • 3
  • 22
  • 25
0
function check() {
  return condition1 && condition2 && condition3;
}

The simplest solution is to find the value of the boolean and express and return that.

What is probably the most optimum solution is the following

function check() {
    var usefulConditionName = /* long condition */;
    var secondUsefulConditionName  = /* long condition */;

    return (usefulConditionName &&
        secondUsefulConditionName);
}
Raynos
  • 166,823
  • 56
  • 351
  • 396
0

I usually think the less code and duplication, the better. I might go with

function check()
{
    return Condition1  AND Condition2  AND Condition3 ;
}
jeff
  • 4,325
  • 16
  • 27
0

Neither. The best option is to return the value of the expression directly. There is no need for more verbosity. Also, JavaScript doesn't have AND operator, it has &&.

function check() {
   return Condition1 && Condition2 && Condition3;
}
svick
  • 236,525
  • 50
  • 385
  • 514
0

IMHO I prefer the first technique, because : 1- Less line of code 2- Less return.

But everyone can have different opinion on question like this.

Nettogrof
  • 2,116
  • 2
  • 15
  • 22