if (e1) return e2; [else] s1;

John R. Dudeck jdudeck at polyslo.CalPoly.EDU
Mon Mar 26 10:50:02 AEST 1990


In article <887 at dino.cs.iastate.edu> hascall at cs.iastate.edu (John Hascall) writes:
>
>    Ok, style mavens, which is "better":
>
>    if (e1) return e2;                    if (e1) return e2;
>    else s1;                              s1;
I can't think of any reason why one is better than the other, but I think
that the second one indicates that you have thought through the logic of
the routine enough to realize that the 'else' is superfluous.  Whenever
I notice that I have done the first, I always change it to remove the else.
>
>---------------------------------------------------------------
>    I'm not sure which of the above is better, but I am sort of
>    "stuck on" the following convention:
>
>    if (e1) s1;        \
>      --or--            \
>    if (e1) {           | Does this seem reasonable?
>	s1;             | (ignoring whether or not the `{'
>    }                   | belongs on the next line or not)
>    --but never--       |
>    if (e1)             /
>	s1;            /
>
I discovered a good reason to use the third form.  When you are
debugging with CodeView, if the action is on a separate line from the
condition you can breakpoint on the action, and it will only break
if that branch is taken.  Similarly, you can single step and have it
stop on the action part.

Otherwise I always use the first two (including your placement of '{' ).

-- 
John Dudeck                           "You want to read the code closely..." 
jdudeck at Polyslo.CalPoly.Edu             -- C. Staley, in OS course, teaching 
ESL: 62013975 Tel: 805-545-9549          Tanenbaum's MINIX operating system.



More information about the Comp.lang.c mailing list