Is Leaving an Empty if Statement in a Recursive Function Bad Programming Style?

Click For Summary

Discussion Overview

The discussion revolves around the programming style related to leaving an empty if statement in a recursive function in C++. Participants explore whether this practice is considered bad programming style and if it poses any potential dangers.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant expresses unease about having an empty if statement in their recursive function, questioning if it is bad style or potentially dangerous.
  • Another participant argues that the original code is acceptable, noting that the comment "//do nothing" clarifies the intent.
  • Some suggest alternative structures, such as using a return statement or modifying the recurrence condition to eliminate the need for an else statement.
  • There is a viewpoint that in high-quality programming circles, the absence of an else statement might indicate a problem, and some would prefer to explicitly include a null else to demonstrate intent.
  • One participant mentions that the choice depends on personal style and acceptance within the programming community.
  • Another participant states a preference for having exit cases before recursive calls, indicating that they find the original structure suitable.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriateness of leaving an empty if statement in recursive functions. There is no consensus on whether it is bad style or acceptable, and multiple perspectives on programming practices are presented.

Contextual Notes

Some suggestions involve changing the recurrence condition or restructuring the code, but these are not universally agreed upon and depend on individual programming preferences.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
I just finished writing a little recursive function in C++ and when my stopping condition is reached, I need it to do nothing. So the if statement looks funny:

if (the condition is met)
{
//do nothing
}
else
{
call yourself with smaller arguments
}

It works fine, but I feel uneasy about having absolutely nothing in the
if{} portion.
Is this bad programming style? Is it potentially dangerous?
Thanks!
 
Technology news on Phys.org
IMO the original is OK - the //do nothing comment makes it clear it means what it says.

You could write

if (! the condition is met)
{
do something;
}

Or possibly

if (the condition is met)
return;
else
{
do something;
}
 
Thanks, AZ!
 
Math Is Hard said:
I just finished writing a little recursive function in C++ and when my stopping condition is reached, I need it to do nothing. So the if statement looks funny:

if (the condition is met)
{
//do nothing
}
else
{
call yourself with smaller arguments
}

It works fine, but I feel uneasy about having absolutely nothing in the
if{} portion.
Is this bad programming style? Is it potentially dangerous?
Thanks!

It might be better for you to make your recurrence condition slightly different, so that you don't even need an else statement. That way both the 'if' and the 'else' statements can have a purpose, you might not even need the else statement.
 
Tony11235 said:
It might be better for you to make your recurrence condition slightly different, so that you don't even need an else statement.

For example,
Code:
if (! done) {
   do_something_recursively;
}

However, in some circles that place an emphasis on high-quality programming, the lack of an else is seen as a sign that something may be awry. If no else is needed, such programmers would write the above as
Code:
if (! done) {
   do_something_recursively;
} else {
   ; /* Null else */
}
to show that the algorithm designer/programmer has indeed deemed that no else is needed rather than merely forgotten.
 
Sure. Either way. Depends on style and acceptance.
 
I like to have the exit cases before the recursive ones, so for me the original is perfect, just how I would have done it.
 
Thanks for your advice on this, everyone. I appreciate it.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
7
Views
6K
  • · Replies 140 ·
5
Replies
140
Views
14K
Replies
9
Views
4K
Replies
10
Views
5K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 5 ·
Replies
5
Views
13K