Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on What is the meaning of "short circuit" operators?

Parent

What is the meaning of "short circuit" operators?

+6
−0

When reading about various operators used by programming languages, the term "short circuit behavior" is often used. For example in this C code:

int a = 0;
a && b++

Someone explained that b++ is never executed because the logical AND operator "short circuits". What do they even mean with this?

Assuming I'm a layman at electronics (but not necessarily at programming), the association I get when hearing "short circuit" is something like connecting + directly to - on a battery, resulting in a spectacular failure such as cables burning up. And that doesn't seem like something I would want to happen to my program...

Why is it called "short circuit behavior"? What's the analogy and how is it helpful in understanding how certain operators work?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Cables don't blow up from a short (5 comments)
Post
+2
−0

It means the program can give up early if checking the rest of a boolean expression is pointless.

For example, naively to evaluate p and q you must check the value of both p and q, and then do the and operation.

However, if you are a bit more clever, you'll see that when p is False, the result cannot be True no matter what q is. It's doomed already, so there's no point checking q. So we say p has short-circuited q.

A very common use is when people write things like if foo() and bar():. Both operands are executing some non-trivial code. If bar() happens to be a very computationally expensive function, it's useful to avoid running it when not necessary.

Besides performance, short circuits can reduce nesting. Another common idiom is: if x is not Null and x.baz == "something":. Without short circuit you would need two nested ifs, because the second expression would error out if x is indeed null. But with short circuits, if it is null, the program won't even attempt to check the second part so you can get away with this statement.


In electrical circuits, a short circuit is when electricity is provided a new (often unintended) path that it can use to bypass the real load (such as a lightbulb). Since it takes the path of least resistance, if you give it a shortcut, it will go through the shortcut without bothering to power your device. This is analogous to how the program skips the remainder of your expression when provided an easier way of completing evaluation.

Also, there are electronic versions of boolean expressions, such as AND gates. These use current yes/no as True/False. When building electronic circuits that evaluate booleans, you can have a literal short circuit mechanism.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

AND gate analogy (1 comment)
AND gate analogy
Lundin‭ wrote 8 months ago

I'm not sure if the last AND gate analogy is relevant. These work with 2 or more inputs that are evaluated simultaneously at the edge of a clock pulse, whereas software has to evaluate each operand one at a time. And there is hopefully no such thing as a short circuit anywhere in sight when dealing with an actual AND gate.