tags:

views:

594

answers:

12

For example

int f(int a) {
   ...
   return a > 10;
}

is that considered acceptable (not legal, I mean is it ``good code''), or should it always be in a conditional, like this

int f(int a) {
   ...
   if (a > 10)
      return 1;
   else
      return 0;
}
+29  A: 

This is absolutely acceptable! In fact, Joel mentioned this on the latest stackoverflow podcast. He said it was the one thing he's had to show almost every programmer that starts at Fog Creek.

Greg Hewgill
When I found out that not everyone does it the first way, I was flabbergasted! The first way is so much more readable and concise, whereas the second is harder to understand because a single, simple concept takes four lines rather than one.
Chris Charabaruk
Where I did undergrad, if you did it the second way you lost points on homework. This was in the intro class (pre major).
hazzen
Especially if the return type is bool. I cringe when I see something like:if (a > 1) return true;else return false;
Ferruccio
hazzen: You did undergrad at a smart place, then. I went to a community college. I probably forgot more about programming there than I ever learnt.
Chris Charabaruk
+2  A: 

I don't see anything wrong with it. If anything it's more concise and I think most developers with moderate experience would prefer it.

Svet
+1  A: 

The first is much preferable to me, since it is more concise. (And it avoids multiple returns:)

Steve Fallows
A: 

I'll typically do the former over the latter.

itsmatt
+1  A: 

I think its perfectly acceptable, provided that you ensure that you make an extra effort to maintain readability. Like I would make sure that the method name is very unambiguous and you use good variable names.

The second alternative that you provided I think is almost worse because it involves a branch statement and multiple return statements and these things increase the complexity of the method while themselves reducing its readability.

pfranza
+35  A: 

It would be acceptable - if your return type was bool.

Mark Brackett
<Laugh> Good catch Mark!
Onorio Catenacci
+1  A: 

I'd rather write bool f(int); and the first form as bool is the boolean type in C++. If I really need to return an int, I'd write something like

int f(int) {
    ...
    const int res = (i>42) ? 1 : 0;
    return res;
}

I'd never understood why people write

if (expr == true)
    mybool = true ; 
else 
    mybool = false;

instead of the plain

mybool = expr;

Boolean algebra is a tool that any developer should be able to handle instinctively

Moreover, I'd rather define a named temporary as some debuggers don't handle function return values very well.

Luc Hermitte
I think you meant to return res, not 0.
David Schlosnagle
The bool type doesn't exist in C, but otherwise, yeah.
Chris Charabaruk
Okay, it does exist in C99... Never mind!
Chris Charabaruk
Indeed. Thanks, David.Regarding bool and C, isn't <stdbool.h> providing a bool type for C99?
Luc Hermitte
Yeah, kind of. It seems that C99's bool support is the antithesis of deprecation -- still a macro, but reserved in case it's upgraded to full type like in C++. http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html says more.
Chris Charabaruk
Surely I can't be the only one that defined BOOL, TRUE and FALSE prior to stdbool.h?
Mark Brackett
+6  A: 

The first case is perfectly good, far better than the second, IMHO. As a matter of readability, I personally would do

   return  (a > 10);

but that is a minor nit, and not one everyone would agree on.

Kevin Little
I don't agree. It makes it look like you think return is a function being called.
Trent
Only if you put spaces before your parentheses when calling functions, which I don't.
Michael Myers
+11  A: 
return a > 10 ? 1 : 0;

... makes more sense because you're returning an int, not a bool.

ilitirit
A: 

Not only is that syntax 100% acceptable, you should also feel free to use boolean expressions outside of if statements, i.e. int x = i && ( j || k ); (or returning values like that).

FreeMemory
A: 

I think part of it has to do with the style and culture of the language. The first example you have written is what would be expected from an experienced C programmer. They would much rather strangle themselves than put in an unnecessary block of statements.

I think it is perfectly acceptable when the language allows it and the usage is part of the paradigm of that language

brijwhiz
A: 

I just tried three different variants with GCC:

int one(int x) { return (x > 42) ? 1 : 0; }
int two(int x) { return x > 42; }
int thr(int x) { if (x > 42) return 1; else return 0; }

As soon as you enable some optimization, the generated code for all of them is the same. So you should use the variant that is easiest to read.

Roland Illig