Re: What do you think about the code?



Andrew Poelstra wrote:
On 2006-07-01, Richard Heathfield <invalid@xxxxxxxxxxxxxxx> wrote:

In practice, status objects are cheap and readable. If they are becoming
unreadable, it's a sign that your function is too big. One huge advantage
of a modular design with strict avoidance of goto, non-documentary
continue, and switchless break, is that it is really easy to refactor a
function that has outgrown its readability. Just chop out the innermost
loop, dump it into a new function, and that's typically almost all that you
have to do. When the control flow is hopping all over the place like a frog
on his wedding night, code simplification is far more difficult to achieve.



Since as a maintenance programmer you've probably had to deal with a lot of
similar code, suppose you were writing a parser and you did this:

c = get_input();

if (we_are_in_a_quote && c != '\"')
goto skip_parsing;

switch (c)
{
/* Parsing code here */
}

skip_parsing:
/* Return parsed text to callee. */


Would that be an appropriate use of goto? Otherwise I've found that I end up
with if statements around most every single case block. (I can't do it around
the whole switch because I don't care if I'm in quotes when checking for the
quote character and the escape character).

I agree 100% with Richard's comments, the cleaner the code structure,
the easier it is to refactor. That's why my last team banned goto outright.

In the parser example, I would call a function for each case and include
the test for quotes explicitly where required. I think this makes the
code clearer.

--
Ian Collins.
.



Relevant Pages

  • [PATCH 1/4] Char: rio, fix cirrus defines
    ... switch ) { ... "command blk %p\n", SubCmd.Addr, CmdBlkP); ... ** If the port is set to store or lock the parameters, ... goto close_end; ...
    (Linux-Kernel)
  • Re: question?
    ... in general, every guideline will have ... Take for example the infamous goto statement. ... early returns can sometimes hurt code readability... ... In fact 'flags' are ...
    (comp.lang.java.programmer)
  • Re: function pointers
    ... The simple goto. ... fixed target label. ... The computed goto, aka switch, select, or caseof, depending ... In C it is the switch. ...
    (comp.lang.c)
  • Re: batch file to install printer
    ... IF NOT EXIST C:\TEMP\%P1%.TXT GOTO:I1 ... SET PNAME1=%"printer name from inf file IN QUOTES" ... SET PDRIVER1=%"driver file path IN QUOTES" ... GOTO:LOOP1 ...
    (microsoft.public.windowsxp.print_fax)
  • [PATCH 15/15] audit: validate comparison operations, store them in sane form
    ... err = -EINVAL; ... goto exit_free; ... switch { ...
    (Linux-Kernel)