Wednesday, January 4, 2006

Ugly design tends to imply bugs

Quoting the `tcsh` manual page:

>

>
break

>
Causes execution to resume after the end of the nearest enclosing
> foreach or while.
> The remaining commands on the current line are executed. Multi-level breaks are thus possible by writing
> them all on one line.

>


"The remaining commands on the current line are executed".
I can't recall ever seeing this approach to multi-level
breaks elsewhere. Of course it breaks the promise of "C-like syntax" where white space does not matter
and syntactic elements have terminators---`tcsh` never attempted to emulate this aspect of C.

This is something different, though, it just doesn't feel right---and after a few minutes I know why. Guess what
is the output of this script:

>
  while (1)
> break; cat << EOF
> Line 1
> EOF
> end
> cat << EOF
> Line 2
> EOF


This shows that the `break` behavior is probably not an intentional design to allow multi-level breaks, it is a documented artifact of the implementation design (which also means we are stuck with the current design). After noticing this it is easy to find other examples of "unexpected behavior" caused by the design:

> foreach var (a b c); echo Look:
> echo $var
> end

For the record, the outputs are:

>
  cat << EOF
> Line 2


and

> Look:
> a
> b
> c

No comments:

Post a Comment