0

Just curious about the good practice of writing codes in C++, take the following codes as an example:

int do_something()
{ 
   bool bHas;
   ....
   ....
   if (bHas)
   {
     ...
     return 1; 
   }

   ... do something else
   ...
   return 2;


}

In the above codes, we can see if bool bHas is set true, then the function will be finished in the middle; otherwise it will continue to execute the rest codes. It is also possible to write the above codes in the following way:

   int do_something()
    { 
       bool bHas;
       ....
       ....
       if (bHas)
       {
         ...
         return 1; 
       }
       else
       {

       ... do something else
       ...
       return 2;
       }


    }

As you can see, this time 'else' is used to exactly tell what will be done depending on the value of bool bHas. My question here is: which practice is better? thanks.

feelfree
  • 11,175
  • 20
  • 96
  • 167
  • 2
    The generated code will be identical, so it's up to personal preference. Hence, it's off-topic. (I omit the `else`, because it's introducing another layer of indentation unnecessarily) – stefan May 28 '14 at 08:31
  • Like the first one better (too many indentations make code less visible) – zoska May 28 '14 at 08:33

1 Answers1

-2

At a first glance, the second once looks better because of it's uniformity in returns statements. To me, using a single point of return in methods is more preferable. It makes identifying statements, debugging and extending the code easier and less error prone. I would write something like

int do_something()
{ 
   bool bHas;
   ....
   ....
   int retValue;
   if (bHas)
   {
     ...
     retValue = 1; 
   }
   else
   {

   ... do something else
   ...
   retValue = 2;
   }

 .....
 return retValue;
}
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • Do you have evidence that a single return statement produces better code? – stefan May 28 '14 at 08:34
  • @stefan, did I say anything close to this? – Rakib May 28 '14 at 08:35
  • I tend to agree with you, but I can see exceptions; I'll use multiple returns if the function ends with a `switch` or an `if/else if` chain, and _all_ of the branches clearly end in a `return`. (This is necessary if the returned type doesn't have a default constructor.) The important thing is that it be "obvious"; each branch must be extremely short, for example. – James Kanze May 28 '14 at 08:35
  • @RakibulHasan No and that's the point. All you state is opinion which doesn't make this a good answer, in my opinion. – stefan May 28 '14 at 08:36
  • i like to handle it the same way and it has the benefit of `assert`ing the value before returning it – Zaiborg May 28 '14 at 08:36
  • The problem with this to me is that it's quite easy to miss out a branch in which `retValue` gets set, and thus `return` the default value. I prefer to write my code in a way that will cause the compiler to complain about a missing `return` if I forget a branch, which necessitates the multiple-return style. – Matthew Walton May 28 '14 at 08:37
  • @stefan, correct, the OP asked for opinion. This question can be closed for being opinion based, but it has nothing to do with quality of answer. – Rakib May 28 '14 at 08:37
  • 1
    @stefan All of the literature about verifying program correctness is based on the single entry/single exit principle. I'll use multiple returns in some cases, but only when they can easily be assimilated to a single entry/single exit (i.e. a single branching structure in which every branch leads to a `return`). – James Kanze May 28 '14 at 08:38
  • 1
    @stefan There are proofs that single point of return produces better code (better = more readable, error prone, etc.). For example book Code Complete is 1.) explaining reasons, 2.) mentioning the proofs. –  May 28 '14 at 08:39