Code Comments
Programming Forum and web based access to our favorite programming groups.
<benmarco7@mail.com> wrote:
> CODE COMPLETE, page 356
>
> if(StatusOK)
> {
> if(DataAvail)
> {
> ImportantVar = x;
> goto MID_LOOP;
> }
> }
> else
> {
> ImportantVar = GetVal( );
> MID_LOOP;
>
> /* lots of code */
> . . .
> }
>
> * * * * * * end fragment * * * * *
>
> The idea here is to rewrite the code without the goto LABLE.
I'd say, encapsulate the "lots of code" as a function, since it's
called under at least two different contexts. Pass it enough
arguments (perhaps pointer arguments if it needs to modify its
inputs) that it can operate in all the contexts it needs to.
(And perhaps if it's really *LOTS* of code, break it up still
further into sub-sub-routines.)
Yes, I'd recommend that even if the original author's "intent"
was something quite different. It's important to *understand*
his/her intent, yes; but you need not be bound by it. (Unless
the person is your boss. ;-) )
My general philosophy in C (or C++, or Perl) is, keep all functions
(subroutines) quite short, and never duplicate anything. If I
see the same block of code being used even TWICE, I cut it out,
put it in its own function, and call it, rather than trying to
either duplicate it or jump to it with goto.
(I sometimes do use goto, but mostly for error handling inside
nested loops.)
--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.