0

Consider the following bar functions

#include <iostream>
void foo(){
 std::cout << "Hello" << std::endl;
}
void bar1(){
 return foo();
}
void bar2(){
 foo();
}
void bar3(){
 foo();
 return;
}
int main()
{
 bar1();
 bar2();
 bar3();
 return 1;
}

These functions do exactly the same thing, and actually godbolt produces the same code for all three (as one would hope). The question I have is simply whether there are any software engineering paradigms/guidelines that advocate one form over the other, and if there are any reasons why you would prefer one over the other. They seem to produce the same machine code, but I am imaging that one might be viewed as "easier to maintain", or something like that.

asked Apr 10, 2019 at 9:22
5
  • I understand that the question of 'why' is opinion based. This is why I asked about which paradigms might have anything to say on the subject. Presumably they have some rationale that we can weigh objectively. Commented Apr 10, 2019 at 9:25
  • A return in a void function is immediately suspicious to me. I say treat void functions like Pascal's procedures. Commented Apr 10, 2019 at 9:25
  • 1
    I would surely go with number 2 since the 1 is misleading, when reading the body i'm supposing to return something but the function is actually void, also the number 3 explicity states that we are returning but don't see any added value since its at the end of the function body. Commented Apr 10, 2019 at 9:26
  • 1
    I won't say bar3 is universally good, but sometimes an explicit return can tell far more than simply flowing off the end. Kate Gregory incorporated this point into her CppCon 2018 talk. You can get a lot of food for thought about such matters from the talk, and I highly recommend it. Commented Apr 10, 2019 at 9:47
  • Interesting talk. 23:46 is the time marker you are talking about. She gives an interesting take. . Commented Apr 10, 2019 at 10:01

3 Answers 3

4

This is quite opinion-based. Though I'd say the general consensus is to write it like bar2(). Don't return explicitly unless you have to return early and don't do return func() if func() returns a void, that just confuses readers because you're not actually returning a value.

answered Apr 10, 2019 at 9:25
Sign up to request clarification or add additional context in comments.

Comments

1

I totally agree with Sombrero Chicken's answer. But I'll also add that the construct like

void bar1(){
 return foo();
}

doesn't make much sense for ordinary functions that return void, but may be useful for template code when you don't know the actual return type, e.g:

template <typename T>
auto SomeTemplateFunction(...)
{
 // do some works
 ...
 return SomeOtherTemplateFunction<T>(...);
}

This will work regardless SomeOtherTemplateFunction<T>'s return type is void or not.

answered Apr 10, 2019 at 9:32

2 Comments

One perceived advantage of writing something like this would be that I can easily change how the code structurally works. The cost is ambiguity... reading the code, it isn't clear what exactly will be returned.
Yes it doesn't have to, but for non-template code if your return value is void personally I would rather not write return statement in the end of the function. For template code it's sometimes necessarily to do it.
1

It's quite opinion based, what I can say is that (3) is tagged by clang-tidy rules as part of the readibility-redundant-control-flow.

The idea is that the control flow here is already defined, the return is superfluous and should then be removed.

answered Apr 10, 2019 at 9:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.