Write concise code with Boolean expressions

Write concise code with Boolean expressions

When setting a Boolean value based upon the result of an expression, avoid using an unnecessary If/Then/Else structure.

'Instead of using this lengthy syntax . . .If SomeVar > SomeOtherVar Then   BoolVal = TrueElse   BoolVal = FalseEnd If'Use this one, which is easier to read and executes faster. The'parenthesis are not necessary, but I use them to enhance 'readability when quickly scanning code.BoolVal = (SomeVar > SomeOtherVar)

The second method ran anywhere between 50% and 85% of the time required for the first method, depending upon which operations were performed in the expression. The parenthesis that I use make no difference in execution speed.

At times, it isn’t obvious that you can use this method to write more concise code. The point is that you should keep in mind that all comparison operators are just diadic operators that can return 0 (False) or -1 (True). So, for example the two following pieces of code are absolutely equivalent, but the second runs slightly faster:

' the standard wayIf SomeVar > SomeOtherVar Then    x = x + 1End If' the more concise way ' notice that we need a minus sign to actually add 1 to xx = x - (SomeVar > SomeOtherVar)

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes