Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Empty if statement
    text
    copied!<p>This is more of an entry level question, but I'm wondering if it's good practice to have an empty if statement.</p> <p>Consider this code:</p> <pre><code>void RabbitList::purge() { if(head == NULL) { //cout &lt;&lt; "Can't purge an empty colony!" &lt;&lt; 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 &amp;&amp; curr == head) { head = curr-&gt;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 &amp;&amp; curr-&gt;next != NULL) { trail-&gt;next = curr-&gt;next; delete curr; --size; --amountToKill; } //If the random check succeeded, but we're on the last node else if(fiftyFiftyChance == 1) { trail-&gt;next = NULL; delete curr; --size; --amountToKill; } //If the random check failed else { trail = curr; curr = curr-&gt;next; } } cout &lt;&lt; "Food shortage! Colony has been purged by half." &lt;&lt; endl; } } </code></pre> <p>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;</p> <p>but since my return type is void it gives me an error. What if my return type is not void for example?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload