5 min read

Engineer Blog - #4

Engineer Blog - #4
Photo by Nick Fewings / Unsplash

Looks like in c#, short circuiting is optional:

My brain started to hurt, so I did some thinking.

Is there any reason for a programmer to use normal OR, or AND, when short circuiting is available?

The code given by Patrick, in the question, makes for a good example:

if(myObj != null && myObj.SomeString != null)

if myObj is null, I'd want to know before referencing on it, not after.

So myObj being null, seems like a great case for short-circuiting.

For my non-IT readers:

In boolean logic, 2 values, with an AND in between, have the following result:

TRUE and FALSE => FALSE
TRUE and TRUE => TRUE
FALSE and FALSE => FALSE
FALSE and TRUE => FALSE

Basically, as long as one value is false, the whole thing is false.

Either something is TRUE, or FALSE, in the binary sense.

There is no I'm kind of Hungry for Paneer - either I am, or I am not.
In Shakespearean terms, there is either 2B, or Not2B.


Sadly, then, always being hungry is not an option, either.

So, you can see the brainwave of someone, somewhere realizing that electricity has only 2 states:

ON (electricity is there)
OFF (electricity is not there)

And so, boolean logic can be used to denote 'on' or 'off', combining electricity with math.


There is only 1 combination where we get a TRUE, with an AND gate - so short circuiting is the idea of abandoning the computation, when the first term is FALSE.

So, is there any reason for a programmer to not use short-circuiting?

I don't think so - the short circuiting is baked into the process of evaluation, and checking 1 clause is always faster than checking 2 clauses.

Then why do we have both & and && in c#?

Let's get back to that later.

Someone asked this, and here is Eric Lippert's answer:

There is a distinction between 'logical' operators, and 'conditional logical operators'.

He goes on:

......I thought conditional logical operators were conditional, because they were used in conditional statements.

Sounds like conditional logical operators are IF statements, in and of themselves:

IF ( clause A is true ) THEN, IF (clause B is true), clapYourHands


// because if you're happy, and you know it.....
// clap your hands!

which is condensed into:

IF ( A && B) { clapYourHands; }

That explains a lot....I thought the point of conditional logical operators was specifically for use in the IF clause, to short circuit.

While that remains a use case, fundamentally, the spirit of c# suggests that conditional logical operators and logical operators are related, but different tools in the engineer's tool set.



Why the brain hurts

"So, is there any reason for a programmer to not use short-circuiting?

I don't think so - the short circuiting is baked into the process of evaluation, and checking 1 clause is always faster than checking 2 clauses.

Then why do we have both & and && in c#?

Let's get back to that later." - Suman J., this article.


In a stand alone statement, I can see how short circuiting is faster than not short circuiting.

Coding with that speed-gain in mind, I am framing my perspective with this truth in the center - that speed matters.

Given my recent approach to leetcode with engineering in mind, I must have realized my own incomplete perception - there can be many ways to code the same thing.

In the end, an IF is just a branching command - IF you're happy, and you KNOW it, clap your hands.

If the array is already empty, just say so - don't sort it again.

Both of these perspectives are in misalignment:

One perspective wants a perfect solution, which involves speed and low memory.
Another perspective wanted to know how to short circuit, so I don't get confused in a potential interview.

Engineering, as a skill, is framed around the idea of problem solving, and there are usually many ways to solve the same problem.

This implies that in both leetcode, and in-person interviews, short circuiting IF clauses can be fast, and impressive, but knowing that c# as a language has several different ways of dealing with the same thing, is better.

"Technicians know how, engineers know why" - Professor Bernstein, paraphrased from my college days.

My brain hurt because my perspective flipped, again - enough to question why knowing short-circuiting is so important, when in the end, I just need to check something.

To short-circuit well, knowing the concept, and coding it, gets the job done. Googling can help here, too.

But an engineer needs to understand the system well enough to make a recommendation, which may still amount to doing what a technician could always do, by definition.

Conclusion

I am better than I thought I was, because I learned how my confusion came from being wrong, even though the spirit of inquiry was onto something wise, but I was still approaching computer science, wrongly, and bring wrong eventually makes us right.

If this article is any longer, all of us will get an experiential understanding of the meaning of getting short circuited.

Thanks for reading, and have a good day!

Appendix

Boolean logic is a form of algebra. It was devised by George Boole (1815–1864), who demonstrated that all logical relations can be expressed as a combination of AND, OR, and NOT operations.

In 1937, Claude Shannon applied Boole’s work to the design of switching circuits, and so Boolean logic became the foundation of all digital circuit design. - Ada Computer Science, edited for clarity.


Looks like the person who answered the question on Stack Overflow, wrote a whole series on it, in case you want to check it out:

When would you use & on a bool?
UPDATE: A commenter points out that today is the 200th anniversary of the birth of George Boole; I had no idea when I scheduled this article that it would be so apropos. Happy birthday George Boole…

Expressions, Statements and Operators, are fundamentally different for engineers. They are loaded terms.

Nice article on mastery: