Loading...

Yoda Notation: A Coding Style for Safety

Yoda Notation: A Coding Style for Safety

In this article we will talk about a serious error of misassignment in programming, the one of the main reason for introduction of bugs. Later, we discuss how to find this issue with yoda notation and get it fixed.

Have you ever gone through a problem of mistakenly use of assignment operator = instead of equality operator == in conditional statements or comparisons. I am pretty sure you are. This is common error which can lead to unintended behavior in the program, as the intended logic is not executed as expected. In this article, we will explore this error, its consequences and at last its fixes.

Understanding the Misassignment Problem

Consider the following example where a function checks if a given number is divisible by 2:

#include <iostream>

bool isEven(int number) {
    if (number = 0) { // Misassignment here!
        return true;
    } else {
        return false;
    }
}

int main() {
    int input;

    std::cout << "Enter a number: ";
    std::cin >> input;

    if (isEven(input)) {
        std::cout << "The number is even." << std::endl;
    } else {
        std::cout << "The number is odd." << std::endl;
    }

    return 0;
}

In the isEven function, there is misassignment problem in the conditional statement if (number = 0). Instead of comparing number with 0, the assignment operator = is used, which assigns 0 to number and then evaluates to false. As a result, the function incorrectly identifies all numbers as odd.

The expression if (number = 0) evaluates to false not because of the value being assigned but the value 0 is considered as false in C++. So, the condition evaluates to false regardless of the value of number.

If there was the expression if (number = 1) this would evaluates to true, because 1 is considered to be true in C++.

Yoda Notation: The Savior

Yoda notation aims to prevent misassignment problems by flipping the order of operands in conditional statements. Instead of writing if (number = 0), Yoda notation suggest writing if (0 = number). This way, if a single = sign is mistakenly used instead of ==, it will result in compilation error, as you cannot assign a value to a constant or literal.

Implementing Yoda Notation

Let's correct the misassignment problem in the isEven function using Yoda notation:

bool isEven(int number) {
    if (0 = number) { // Yoda notation prevents misassignment
        return true;
    } else {
        return false;
    }
}

In Yoda notation, the constant 0 is placed on the left side of the comparison, making it impossible to assign a value to it accidently. If the developer mistakenly uses = instead of ==, the compiler will generate an error, alerting them to the issue.

But still you have to use == to get it fixed, Yoda notation just help in finding the misassignment error, developer have to use == then to get it working.

bool isEven(int number) {
    if (0 == number) {
        return true;
    } else {
        return false;
    }
}

Benefits of Yoda Notation

  1. Prevention of Misassignment: Yoda notation helps catch misassignment errors at compile-time, reducing the likelihood of introducing bugs into the code.
  2. Improved Readability: Some developers find Yoda notation more readable, as it emphasis the constant or literal being compared.

Extras

What would be the Output of below Code:

#include <iostream>

int main() {
    int* person = nullptr;


    if (person = nullptr) {
        std::cout << "IF NULL" << std::endl;
    }
 else{
 	std::cout <<"ELSE"<< std::endl;
 }

    return 0;
}

Solution:

ELSE

In the statement if (person = nullptr), the value of person is assigned to nullptr, and the result of this assignment (which is nullptr). This is evaluated to false, as nullptr is treated as a false value in conditional statements. so the else block will always be executed.