* Detecting superfluous "else"
@ 2018-07-19 9:12 U.Mutlu
2018-07-19 13:11 ` Paul Koning
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: U.Mutlu @ 2018-07-19 9:12 UTC (permalink / raw)
To: gcc
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Detecting superfluous "else"
2018-07-19 9:12 Detecting superfluous "else" U.Mutlu
@ 2018-07-19 13:11 ` Paul Koning
2018-07-19 13:48 ` Jonathan Wakely
2018-07-19 15:59 ` Eric Gallager
2018-07-21 21:09 ` Didier Kryn
2 siblings, 1 reply; 6+ messages in thread
From: Paul Koning @ 2018-07-19 13:11 UTC (permalink / raw)
To: GCC Mailing List
> On Jul 19, 2018, at 4:49 AM, U.Mutlu <um@mutluit.com> wrote:
>
> 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.
I don't see any reason to warn about that code. It's perfectly valid, and in my view is clearer than the alternative. I've written both but I most often write the "else" variant for the reason that it expresses the semantics explicitly.
Warnings are appropriate for code that is known to be a source of bugs, or where there is a reasonable chance that the intent of the programmer doesn't match what was actually written. That's not the case here.
paul
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Detecting superfluous "else"
2018-07-19 13:11 ` Paul Koning
@ 2018-07-19 13:48 ` Jonathan Wakely
0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2018-07-19 13:48 UTC (permalink / raw)
To: paulkoning; +Cc: gcc
On Thu, 19 Jul 2018 at 14:01, Paul Koning wrote:
> Warnings are appropriate for code that is known to be a source of bugs, or where there is a reasonable chance that the intent of the programmer doesn't match what was actually written. That's not the case here.
Agreed. This seems to be a purely stylistic preference.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Detecting superfluous "else"
2018-07-19 9:12 Detecting superfluous "else" U.Mutlu
2018-07-19 13:11 ` Paul Koning
@ 2018-07-19 15:59 ` Eric Gallager
2018-07-21 13:18 ` Daniel Letai
2018-07-21 21:09 ` Didier Kryn
2 siblings, 1 reply; 6+ messages in thread
From: Eric Gallager @ 2018-07-19 15:59 UTC (permalink / raw)
To: U.Mutlu; +Cc: gcc
On 7/19/18, U.Mutlu <um@mutluit.com> wrote:
> 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
>
Semi-related: bug 81851: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81851
(my disagreement with that request is similar to my disagreement with
this request)
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Detecting superfluous "else"
2018-07-19 15:59 ` Eric Gallager
@ 2018-07-21 13:18 ` Daniel Letai
0 siblings, 0 replies; 6+ messages in thread
From: Daniel Letai @ 2018-07-21 13:18 UTC (permalink / raw)
To: gcc
On 19/07/2018 18:56, Eric Gallager wrote:
> On 7/19/18, U.Mutlu <um@mutluit.com> wrote:
>> 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
>>
> Semi-related: bug 81851: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81851
> (my disagreement with that request is similar to my disagreement with
> this request)
Your example might be worth a warning, to allow the developer to refactor
> int g (int i)
> {
> if (i == 0) // no warning
> return 0;
> #if SOME_OTHER_PLATFORM
> if (i == 2)
> return 1;
> #endif
> return 0;
> }
to
> int g (int i)
> {
> #if SOME_OTHER_PLATFORM
> if (i == 2)
> return 1;
> #endif
> return 0;
> }
It is purely stylistic, but if one is worried about dup branch at all, than it's reasonable to assume one would want this warning too.
It would require distinguishing between multiple possible branches by the preprocessor, rather than the compiler. No idea who emits the dup branch warning.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Detecting superfluous "else"
2018-07-19 9:12 Detecting superfluous "else" U.Mutlu
2018-07-19 13:11 ` Paul Koning
2018-07-19 15:59 ` Eric Gallager
@ 2018-07-21 21:09 ` Didier Kryn
2 siblings, 0 replies; 6+ messages in thread
From: Didier Kryn @ 2018-07-21 21:09 UTC (permalink / raw)
To: gcc
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-07-21 16:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-19 9:12 Detecting superfluous "else" U.Mutlu
2018-07-19 13:11 ` Paul Koning
2018-07-19 13:48 ` Jonathan Wakely
2018-07-19 15:59 ` Eric Gallager
2018-07-21 13:18 ` Daniel Letai
2018-07-21 21:09 ` Didier Kryn
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).