What is better: multiple "if" statements or one "if" with multiple conditions?

86,013

Solution 1

One golden rule I follow is to "Avoid Nesting" as much as I can. But if it is at the cost of making my single if condition too complex, I don't mind nesting it out.

Besides you're using the short-circuit && operator. So if the boolean is false, it won't even try matching!

So,

if (boolean_condition && matcher.find(string)) {
    ...
}

is the way to go!

Solution 2

The following two methods:

public void oneIf(boolean a, boolean b)
{
    if (a && b)
    {   
    }
}

public void twoIfs(boolean a, boolean b)
{
    if (a)
    {
        if (b)
        {       
        }
    }
}

produce the exact same byte code for the method body so there won't be any performance difference meaning it is purely a stylistic matter which you use (personally I prefer the first style).

Solution 3

Both ways are OK, and the second condition won't be tested if the first one is false.

Use the one that makes the code the more readable and understandable. For just two conditions, the first way is more logical and readable. It might not be the case anymore with 5 or 6 conditions linked with &&, || and !.

Solution 4

Java uses short-circuiting for those boolean operators, so both variations are functionally identical. Therefore, if the boolean_condition is false, it will not continue on to the matching

Ultimately, it comes down to which you find easier to read and debug, but deep nesting can become unwieldy if you end up with a massive amount of braces at the end

One way you can improve the readability, should the condition become longer is to simply split it onto multiple lines:

if(boolean_condition &&
   matcher.find(string))
{
    ...
}

The only choice at that point is whether to put the && and || at the end of the previous line, or the start of the current.

Solution 5

The first one. I try to avoid if nesting like that, i think it's poor style/ugly code and the && will shortcircuit and only test with matcher.find() if the boolean is true.

Share:
86,013
3rgo
Author by

3rgo

Computer Science Engineer, specialized in Web development. Work : 2011-2016: Intranet tools development for Airbus France (Single page JS apps, with a PHP5+Symfony 2 REST API ; custom made Ruby CI/CD pipeline). 2016-2018: Project Manager/CTO for Decasoft 2019+: Freelance web developer (Symfony, Laravel, ReactJS) and Trainer (PHP, OOP, Algorithmics, JS) Tech Stack: PHP 5 to 8 Symfony, Laravel Laravel Livewire PHPUnit Javascript ReactJS AlpineJS MeteorJS (in progress) jQuery, Underscore/Lodash, MomentJS, D3.js, Leaflet MySQL, PostgreSQL, mongoDB (in progress) Bootstrap, MaterialUI, TailwindCSS Jenkins, Git Apache, NGINX Windows, Ubuntu, macOS Python, Bash

Updated on December 15, 2021

Comments

  • 3rgo
    3rgo over 2 years

    For my work I have to develop a small Java application that parses very large XML files (~300k lines) to select very specific data (using Pattern), so I'm trying to optimize it a little. I was wondering what was better between these 2 snippets:

    if (boolean_condition && matcher.find(string)) {
        ...
    }
    

    OR

    if (boolean_condition) {
        if (matcher.find(string)) {
            ...
        }
    }
    

    Other details:

    • These if statements are executed on each iteration inside a loop (~20k iterations)
    • The boolean_condition is a boolean calculated on each iteration using an external function
    • If the boolean is set to false, I don't need to test the regular expression for matches

    Thanks for your help.