Short circuit evaluation and why it’s important

amrdeveloper

Amr Hesham

Posted on February 3, 2023

Short circuit evaluation and why it’s important

Hello everyone. In this article, I want to talk about a concept that is used in many programming languages and how to take advantage of it. It has many names like McCarthy evaluation or minimal evaluation, but the most popular name is Short circuit evaluation.

What is Short circuit evaluation?

Before we answer this question, let’s first think of two situations,

if (first && second) { }
Enter fullscreen mode Exit fullscreen mode

In this code snippet, if the first condition is evaluated as false, does the value of the second one matter to check if it should evaluate the if body or not? or what if the first condition is evaluated as true and the operator is Or (||) not and does we need to evaluate the second?

The answer for both questions is no because in And (&&) expression, if one of the values is false, that means the condition will be evaluated as false; no matter the other value is true or false, and in Or (||) expression if the first one is true no need to check the second value, in all cases it will return true

false && true   --> false
false && false  --> false
true || true    --> true
true || false   --> true
Enter fullscreen mode Exit fullscreen mode

In most programming languages, the process of evaluating the second argument depending on the first one is automatically handled for you and it called Short-circuit evaluation.

How can you take advantage of Short circuit evaluation?

If you have a validation function that depends on more than one condition, for example, you check that a user password has a length of more than 8 characters and then match it with the Regex pattern, the performance of this validation function can be different depending on the order of the conditions.

You can have two options to order those conditions like

public static boolean lengthFirst(String password) {
    return password.length() > 8 && pattern.matcher(password).matches();
}

public static boolean regexFirst(String password) {
     return pattern.matcher(password).matches() && password.length() > 8;
}
Enter fullscreen mode Exit fullscreen mode

If we pass a valid password, the two functions will be the same, but what if we pass the wrong one? which one can be faster, and how fast can it be comparing to the other one? let’s check!

String input = "one";

long lenStart = System.nanoTime();
lengthFirst(input);
long lenEnd = System.nanoTime() - lenStart;
System.out.println(lenEnd);

long regexStart = System.nanoTime();
regexFirst(input);
long regexEnd = System.nanoTime() - regexStart;
System.out.println(regexEnd);
Enter fullscreen mode Exit fullscreen mode

This code snippet will print the time of executing each validation function, the output on my machine is

6800
773900
Enter fullscreen mode Exit fullscreen mode

That means the first function is about 113 times faster, but why?

This difference is because checking string length is much faster than matching it with regex, try to compare them and check the differences you will get almost the same result.

That means to take advantage of Short circuit evaluation, you need to think carefully about the order of your conditions.

How to order conditions to get more performance?

When you write expressions with multi conditions, you should think about how you can avoid evaluating the big conditions or, in other words, try to evaluate the small conditions first so if they return false, you can avoid the others.

Summary
Short circuit evaluation, like many other useful programming concepts, is not hard and gives you much more performance

Enjoy Programming 😋.

💖 💪 🙅 🚩
amrdeveloper
Amr Hesham

Posted on February 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related