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

AI Thread Summary
The discussion centers on the use of an empty if statement in a recursive C++ function, where the stopping condition leads to no action being taken. The original poster expresses concern about the style and potential dangers of having an empty block. Responses suggest that while the current implementation works, it may be stylistically better to modify the condition to avoid needing an else statement altogether. Some programmers advocate for clarity in coding practices, recommending the use of a null else statement to explicitly indicate that no action is required. Ultimately, opinions vary on the importance of style versus functionality, with some preferring to keep exit cases clear and upfront in the code structure.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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.
 
Back
Top