1

I saw the following code snippet in some C program:

char *x;

if (s == NULL)
    return(NULL);
if ((x = strdup(s)))
    return x;

What's the purpose of redundant parentheses in return(NULL)?

yl35
  • 111
  • 6
  • possible duplicate of [Parenthesis surrounding return values](http://stackoverflow.com/questions/161879/parenthesis-surrounding-return-values) – Stephen Canon Jan 18 '12 at 17:55

5 Answers5

5

Judging from the if statement below, the author simply likes writing parentheses.

Theoretically, since NULL is a preprocessor macro you could define it to something that is not enclosed in parens, and therefore might have different behavior depending on which expression it is part of due to operator precedence rules -- in which case, wrapping it in extra parens would be "safer".

Practically, this isn't going to happen. Even contemplating such a scenario makes me feel dirty, so I cannot imagine what would be the fate of someone who actually did this.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 2
    In the `if` statement, parentheses are usually a good practice when assigning a value, as in the example. – Macmade Jan 18 '12 at 17:45
  • @Macmade: Sure, but... there's nothing else in there apart from the assignment! I did write parens around assignments myself back when I still thought that being terse was good, but not when there's nothing else to mix in the expression. – Jon Jan 18 '12 at 17:48
  • 2
    The parenthesis in the `if` prevent some compilers from issuing a warning about using an assignment in an if. – Stephen Canon Jan 18 '12 at 17:49
  • 2
    The redundant parentheses in the if statement will suppress a warning from gcc about using '=' instead of '=='. – William Pursell Jan 18 '12 at 17:50
  • @StephenCanon: In which case you should take the message and stop using assignments in conditionals. This is not 1980. :-) – Jon Jan 18 '12 at 17:50
  • 2
    @Jon: "People who don't know the language find it confusing" is not a good reason for avoiding use of a language feature. – Stephen Canon Jan 18 '12 at 17:52
  • @StephenCanon: Sorry, but who said anything about people finding it confusing? The point is readability and maintainability. Moreover, "the compiler allows it" is a really bad reason for *making* use of a language feature. – Jon Jan 18 '12 at 18:14
4

Two reasons:

  1. Some people really like extraneous parentheses. I don't, but this harms no one.
  2. Some people like to be able to define return as macro when they are debugging such that it does some form of additional logging before returning normally. These people are nuts.
Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
  • 3
    +1 for the second item, and especially for the last sentence. – Jon Jan 18 '12 at 17:49
  • +1 anyway, but actually, the "harms no one" thing is not quite true. Some readers (myself included) have a really hard time following and matching excessive nested parentheses, and greatly appreciate efforts to avoid increasing the nesting with redundant ones... – R.. GitHub STOP HELPING ICE Jan 18 '12 at 19:02
2

Some people like parens on their returns. My only quibble is that the code is inconsistent.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
2

Some people view return as a function so they put the extra parentheses. Even if return is actually a statement.

Note that some other people also view sizeof as a standard library function and like to put extra parentheses when the operand is an expression. Of course sizeof is an operator and not a function.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

There is no specific purpose for them in this case. Usually I prefer putting parenthesis for return statements.
You should either consistently put them or not put them at all. One possible explanation for the inconsistency here is that NULL is a macro, and some people have a habit of surrounding macros with parenthesis to make sure they are properly contained.

MK.
  • 33,605
  • 18
  • 74
  • 111