if - else

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Geary

    #16
    Re: if - else

    > > | if (10 == sum) {...}[color=blue][color=green]
    > > |
    > > | rather than
    > > |
    > > | if (sum == 10) {...}[/color][/color]
    [color=blue][color=green]
    > > Yes, there's such a style, and it's considered by experienced C
    > > programmers to be as weird as if( (a<b) == TRUE )...[/color][/color]
    [color=blue]
    > Dunno about that, I've been using C since 1986 and every C
    > project I've worked on since 1991 (about half a dozen biggish
    > ones) has mandated the
    >
    > if (CONST == variable)...
    >
    > style of if statement check, and most of the recent books
    > recommend it. So an awful lot of experienced C
    > programmers use it day in day out - and I haven't heard
    > any loud complaints.
    > Generally anything that saves bugs in C is "A Good Thing",
    > its like running lint - you just build it into the make rules
    > so that you never forget...[/color]

    The problem with "if( CONST == variable )" is that it reads unnaturally,
    creating a mental speed bump when someone reads your code. Admittedly it's a
    fairly minor speed bump, and avoiding coding errors is a good thing.
    However, this contrivance is unnecessary with modern C compilers, which
    issue a warning if you code "if( variable = CONST )".

    With Microsoft Visual C++, I always set my project options so that warnings
    are errors. If I code "if( variable = CONST )", it won't compile. Thus, I'm
    able to use the more natural "if( variable == CONST )" notation without fear
    of error.

    It's certainly pleasant that Python sidesteps this issue completely! :-)

    -Mike


    Comment

    • Jay O'Connor

      #17
      Re: if - else

      Michael Geary wrote:
      [color=blue][color=green][color=darkred]
      >>>| if (10 == sum) {...}
      >>>|
      >>>| rather than
      >>>|
      >>>| if (sum == 10) {...}
      >>>
      >>>[/color][/color]
      >
      >
      >[color=green][color=darkred]
      >>>Yes, there's such a style, and it's considered by experienced C
      >>>programmer s to be as weird as if( (a<b) == TRUE )...
      >>>
      >>>[/color][/color]
      >
      >
      >[color=green]
      >>Dunno about that, I've been using C since 1986 and every C
      >>project I've worked on since 1991 (about half a dozen biggish
      >>ones) has mandated the
      >>
      >>if (CONST == variable)...
      >>
      >>style of if statement check, and most of the recent books
      >>recommend it. So an awful lot of experienced C
      >>programmers use it day in day out - and I haven't heard
      >>any loud complaints.
      >>Generally anything that saves bugs in C is "A Good Thing",
      >>its like running lint - you just build it into the make rules
      >>so that you never forget...
      >>
      >>[/color]
      >
      >The problem with "if( CONST == variable )" is that it reads unnaturally,
      >creating a mental speed bump when someone reads your code. Admittedly it's a
      >fairly minor speed bump, and avoiding coding errors is a good thing.
      >However, this contrivance is unnecessary with modern C compilers, which
      >issue a warning if you code "if( variable = CONST )".
      >
      >[/color]

      I was on a TCL project (ugh..one reason I don't use TKinter is simply
      guilt by association...I learned to hate TCL on that project) where I
      inherited code from a developer who did a lot of that "if 10==x" kinda
      stuff. Basically we said the same thing; "it doesn't read naturally"

      [color=blue]
      > It's certainly pleasant that Python sidesteps this issue completely! :-)[/color]

      True, but I'd still rather allow the assignment and catch the error by
      not allowing boolean comparison of non-boolean values. This would break
      a lot of other basic stuff in Python, though, such as using "if seq:" to
      determine if a sequence is empty, but I've never been a big fan of that
      shortcut (althuogh I use it myself). I'd rather the code have to be a
      bit more explicit.



      Comment

      Working...