alternatives to making blocks like { } or other ??

John Roth newsgroups at jhrothjr.com
Wed May 19 12:41:36 EDT 2004


"Michael Chermside" <mcherm at mcherm.com> wrote in message
news:mailman.80.1084981815.6949.python-list at python.org...
>
> I (Michael Chermside) wrote:
> > a brace and either way it throws the logic off. I created a
> > bug in one of
> > my programs recently just by adding logging. It looked like this:
> >
> >     if (some_complicated_condition)
> >         log_message("Error has occurred: taking corrective action");
> >         take_corrective_action();
>
> Tim Williams replies:
> > But here if there were braces, the logic would be right. Indentation
> > wouldn't matter.
> >      if (some_complicated_condition) {
> >          log_message("Error has occurred: taking corrective action");
> >          take_corrective_action();
> >      }
>
> If you were writing in Python the logic would be right also. I suppose
> you have one valid point... a language which *required* braces
> everywhere wouldn't be susceptable to this kind of error. But that
> rules out C, C++, Java, even things like Pascal.

When I was studying Ruby, I thought about this a lot. The
conclusion I came to is that Ruby actually did it right.

There are two things about braces. One is that the languages
that use them (mostly derived from Algol or Pascal) are
written so that you can do one-liners. You can put a program
with 10k of code onto one line! The only thing the compiler
uses lines for is to issue diagnostics. (Of course, it wouldn't
be readable).

The second thing is that many of them try to optimize out the
common case of zero or one statements in a block so you don't
have to use braces for that. Unfortunately, that seems to cause
as many problems as it solves, at least from the number of errors
standpoint.

If you think about it, the only thing you need to know is where
the end of a block is. You only need to know where the beginning
is if it's completely optional - think of Ruby's method of injecting
a block into a method call. There are no cases in Python where
the block is optional, so the situation doesn't arise. If I have an if
statement, the current block (in Ruby) ends with one of three things:
an elsif, an else, or an end. What you get for that extra end is a
cleanly nestable way of going back to statement syntax in the
middle of an expression, and no arguements about indentation
sensitivity.

John Roth




>
>
> -- Michael Chermside
>
>





More information about the Python-list mailing list