Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Empty if statement

This is more of an entry level question, but I'm wondering if it's good practice to have an empty if statement.

Consider this code:

void RabbitList::purge()
{
    if(head == NULL)
    {
        //cout << "Can't purge an empty colony!" << endl;
    }
    else
    {
        //Kill half the colony
        for(int amountToKill = (getColonySize()) / 2; amountToKill != 0;)
        {
            RabbitNode * curr = head;
            RabbitNode * trail = NULL;

            bool fiftyFiftyChance = randomGeneration(2);

            //If the random check succeeded but we're still on the head node
            if(fiftyFiftyChance == 1 && curr == head)
            {
                head = curr->next;
                delete curr;
                --size;
                --amountToKill;
            }
            //If the random check succeeded and we're beyond the head, but not on last node
            else if(fiftyFiftyChance == 1 && curr->next != NULL)
            {
                trail->next = curr->next;
                delete curr;
                --size;
                --amountToKill;
            }
            //If the random check succeeded, but we're on the last node
            else if(fiftyFiftyChance == 1)
            {
                trail->next = NULL;
                delete curr;
                --size;
                --amountToKill;
            }
            //If the random check failed
            else
            {
                trail = curr;
                curr = curr->next;
            }
        }
        cout << "Food shortage! Colony has been purged by half." << endl;
    }
}

As you can see, the if statement on line 5 is currently commented out; this was more of a debug text and I don't want to send any feedback to the console anymore. I'm pretty sure it would be considered bad practice to have the if statement do nothing. I know I could have return;

but since my return type is void it gives me an error. What if my return type is not void for example?

like image 223
Marcan Avatar asked Dec 02 '22 20:12

Marcan


2 Answers

Even if your return type is void it is legal to return there, and certainly since the if has braces, atleast this isnt a bug waiting to happen. However it is not pretty and involves a little more work to read/understand.

You could reword it to

if(head == NULL) // or if(!head)
    return;

....

This should remove the need for the else, and the rest of the code is now inside the function and not a nested scope, a happy perk.

like image 83
Karthik T Avatar answered Dec 13 '22 04:12

Karthik T


For a single branch, just write it directly:

if (head != 0) {
    // whatever
}

For multiple branches, sometimes having an empty first branch can simplify the conditions that follow:

if (head == 0) {
    // nothing to do
} else if (head->next == 0) {
    // whatever
} else {
    // whatever else
}

Yes, you could write that last one with an additional layer:

if (head != 0) {
    if (head->next == 0) {
        // whatever
    } else {
        // whatever else
    }
}

but the first form is clearer, especially when the second form would end up with three or four levels of if.

Oh, and

if (head == 0)
    return;

can sometimes be difficult, since it introduces an extra point of exit to the function. In the past I was a fan of this form, but in the past few years I've found that I end up removing it pretty consistently.

like image 35
Pete Becker Avatar answered Dec 13 '22 05:12

Pete Becker