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

In summary, the programmer is uneasy about having nothing in the if{} portion of the code, but it might be better for them to make their recurrence condition slightly different.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
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;
}
 
  • #3
Thanks, AZ!
 
  • #4
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.
 
  • #5
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.
 
  • #6
Sure. Either way. Depends on style and acceptance.
 
  • #7
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.
 
  • #8
Thanks for your advice on this, everyone. I appreciate it.
 

1. What is a recursion stopping condition?

A recursion stopping condition is a condition that is used to terminate a recursive function. It is necessary to prevent the function from running indefinitely, which can result in an infinite loop.

2. Why is a recursion stopping condition important?

A recursion stopping condition is important because without it, a recursive function may continue to run forever, which can cause a program to crash or use up all available memory. It is necessary to ensure that the function stops running once it has completed its task.

3. How do you determine the recursion stopping condition?

The recursion stopping condition is determined by identifying the base case, which is the simplest form of the problem that can be solved without further recursion. Once the base case is reached, the function will stop running and return the desired result.

4. Can a recursion stopping condition be changed?

Yes, a recursion stopping condition can be changed. In fact, it is often necessary to modify the stopping condition in order to solve different variations of the same problem. However, it is important to ensure that the new stopping condition is still valid and will result in the function terminating correctly.

5. What happens if there is no recursion stopping condition?

If there is no recursion stopping condition, the function will continue to run indefinitely, resulting in an infinite loop. This can cause a program to crash or use up all available memory, making it impossible to obtain a result. It is important to always include a valid stopping condition in recursive functions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
18
Views
5K
  • STEM Academic Advising
Replies
13
Views
2K
  • Quantum Interpretations and Foundations
4
Replies
138
Views
5K
Replies
10
Views
2K
Replies
7
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Beyond the Standard Models
Replies
11
Views
2K
  • STEM Academic Advising
Replies
2
Views
1K
  • General Discussion
Replies
4
Views
666
Back
Top