Short Circuit Evaluation: What You Need to Knowby@amrdeveloper

# Short Circuit Evaluation: What You Need to Know

February 8th, 2023

In most programming languages, the process of evaluating the second argument depending on the first one is automatically handled for you. To take advantage of Short circuit evaluation, you need to think carefully about the order of your conditions. 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.

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) { }
``````

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 do we need to evaluate the second?

The answer to both questions is no because in a And (&&) expression, if one of the values is false, that means the condition will be evaluated as false; no matter whether the other value is true or false.

And in an Or (||) expression, if the first one is true, there’s 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
``````

In most programming languages, the process of evaluating the second argument depending on the first one is automatically handled for you, and it is 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) {
}

public static boolean regexFirst(String password) {
}
``````

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 compared 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);
``````

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

``````6800
773900
``````

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 a Better 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 that 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 a much better performance.

Enjoy Programming 😋.

Also published here

L O A D I N G
. . . comments & more!