This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Detecting superfluous "else"


Le 19/07/2018 à 10:49, U.Mutlu a écrit :
Hi,
it makes me 'crazy' when I see such if-else constructs:
  if (x)
    return 7;
  else
    return 4;

(Of course in this case one better would use the shorthand "return x ? 7 : 4;", but that's not the issue here)

The 'else' is obviously superfluous/redundant, ie. unneeded at all:
  if (x)
    return 7;
  return 4;

Is it possible to warn about such unneccessary occurances of "else"?
If not, then I suggest to add a new warning code -Wsuperfluous-else or -Wredundant-else or so.

Thx

    Let me express the point of view of an old C programmer (and also programmer in other languages).

    From a semantic point of view  (if I can dare to use this word) the two forms are different.

    The first one, which you dislike, is appropriate in the general situation of an alternative in which the two cases have the same order of probability to happen. The form you propose is more appropriate to catch exceptions while leaving straightforward the "normal" execution thread.

    Although the C language is very loose about semantics, leaving it to the appreciation of the programmer, a well written program is one in which the author takes care of semantics. Some programmers find it stylish to minimize the character count of expressions and the size of the source file. My opinion is that size doesn't matter; the important is to favour the readibility, which includes making as clear as possible what the intent of the author is. Often, this is better achieved by the way the instructions are written than by adding comments.

    Thanks.

                    Didier




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]