Get into the Habit of Preventing Bugs in if Statements

Get into the Habit of Preventing Bugs in if Statements

Unintentionally using = instead of == in an if statement can prove a nasty source of bugs. In the following code, myFunc is called if i equals 0:

if (i==0) myFunc(i);  

Now suppose that in your haste to meet a deadline, you accidentally wrote:

if (i=0) myFunc(i);

This code would still compile (unless i is a const), but in this case myFunc(i) would always be called, because i=0 is an assignment that always evaluates to true, which is not what you intended. If i is an iterator, even worse things might happen.

To avoid these disasters, get into the habit of writing your tests like this:

 if (0==i) ...

This is a perfectly valid?albeit somewhat odd-looking?way of writing tests.

Suppose you forget the == and write =? The code looks like this:

if (0=i) ...

The compiler catches the error correctly. Get in the habit of writing the number on the left hand side of the == rather than the usual right hand side.

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