Short-circuiting in conditions in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use conditions that stop checking early, it can change how long the code runs.
We want to see how this early stopping affects the time it takes as inputs grow.
Analyze the time complexity of the following code snippet.
function checkTransactions(transactions) {
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].isValid && transactions[i].isConfirmed) {
return true;
}
}
return false;
}
This code checks a list of transactions and stops as soon as it finds one that is valid and confirmed.
- Primary operation: Loop through the transactions array.
- How many times: Up to the length of the transactions list, but may stop early.
As the number of transactions grows, the loop might check fewer items if it finds a match early.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Between 1 and 10 checks |
| 100 | Between 1 and 100 checks |
| 1000 | Between 1 and 1000 checks |
Pattern observation: The loop can stop early, so the work done depends on where the first valid transaction appears.
Time Complexity: O(n)
This means in the worst case, the code checks every transaction once as the list grows.
[X] Wrong: "Because of short-circuiting, the code always runs fast no matter the input size."
[OK] Correct: Sometimes the matching transaction is at the end or missing, so the loop checks all items, making time grow with input size.
Understanding how early stopping affects time helps you explain code efficiency clearly and shows you think about real-world data behavior.
"What if the condition checked two separate arrays instead of one? How would the time complexity change?"
Practice
&& and ||?Solution
Step 1: Understand short-circuiting concept
Short-circuiting means the program stops checking conditions as soon as the final result is known.Step 2: Apply to
For&&and||&&, if the first condition is false, no need to check further. For||, if the first condition is true, no need to check further.Final Answer:
It stops checking further conditions once the result is decided. -> Option AQuick Check:
Short-circuiting = Stops early [OK]
- Thinking all conditions always run
- Confusing
&&and||behavior - Assuming short-circuiting only applies to numbers
Solution
Step 1: Identify correct AND operator syntax
The short-circuit AND operator is&&, which checks the second condition only if the first is true.Step 2: Compare options
if (condition1 && condition2) { execute(); } uses&&, correct for short-circuit AND. Options B and D use single & or | which are bitwise operators, not short-circuit. if (condition1 || condition2) { execute(); } uses OR operator.Final Answer:
if (condition1 && condition2) { execute(); } -> Option DQuick Check:
Short-circuit AND = && [OK]
- Using single & instead of &&
- Confusing AND (&&) with OR (||)
- Using bitwise operators by mistake
bool a = false;
bool b = true;
if (a && (b = false)) { }
return b;
What is the returned value?
Solution
Step 1: Analyze short-circuit AND behavior
Sinceais false, theb = falsepart is not executed due to short-circuiting.Step 2: Determine final value of
bbremains true because the assignment did not happen. So,return b;returns true.Final Answer:
true -> Option CQuick Check:
False && skips second = b stays true [OK]
- Assuming b becomes false anyway
- Ignoring short-circuit effect
- Thinking code throws error
if (isValid || checkStatus()) {
execute();
}where
checkStatus() is a function that must run always.Solution
Step 1: Understand short-circuit OR behavior
IfisValidis true,checkStatus()will not run because of short-circuiting.Step 2: Identify problem with function call
SincecheckStatus()must always run, using||causes it to sometimes skip, which is an error.Final Answer:
FunctioncheckStatus()may not run due to short-circuiting. -> Option AQuick Check:
OR short-circuit skips right if left true [OK]
- Assuming function always runs
- Confusing syntax error with logic error
- Using AND instead of OR without reason
isOwner and hasBalance. You must run logAccess() always, but only execute transfer() if both conditions are true. Which code correctly uses short-circuiting?Solution
Step 1: Identify when
logAccess()must runlogAccess()must run always, so it should be called outside the condition.Step 2: Check condition for
transfer()transfer()runs only if bothisOwnerandhasBalanceare true, so use&&inside the if.Step 3: Analyze options
if (isOwner && hasBalance) { logAccess(); transfer(); } putslogAccess()inside the if, so it only runs if conditions true. if (isOwner && hasBalance && logAccess()) { transfer(); } callslogAccess()inside condition, so it may not run due to short-circuiting. if (isOwner & hasBalance) { logAccess(); transfer(); } uses single & which is bitwise, not short-circuit. logAccess(); if (isOwner && hasBalance) { transfer(); } callslogAccess()first always, then conditionallytransfer().Final Answer:
logAccess(); if (isOwner && hasBalance) { transfer(); } -> Option BQuick Check:
Call always outside, conditionally inside [OK]
- Putting always-run function inside condition
- Using bitwise & instead of &&
- Calling functions in wrong order
