alternatives to making blocks like { } or other ??

Discussion in 'Python' started by Michael Chermside, May 19, 2004.

  1. 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.

    My second example was:
    > When I read through the code it LOOKED like it worked, but of course
    > the corrective action was taken even when it wasn't needed. I've also
    > seen more complex examples like this:
    >
    > if (condition_1) {
    > code_1;
    > } else if (condition_2)
    > if (condition_2_a) {
    > code_2_a;
    > }
    > // nothing to do for 2b
    > else {
    > code_3;
    > }
    >


    To which Tim replies:
    > Looks like an argument for braces to me.


    Not at all. If written in Python, we have this:

    if condition_1:
    code_1
    elif condition_2:
    if condition_2_a:
    code_2_a
    # nothing to do for 2b
    else
    code_3

    which is correct (not to mention being shorter).

    I then gave this example:
    > But the ones that I hate the MOST are the ones that my unit
    > tests can't
    > catch. A typical example looks like this:
    >
    > if (condition) {
    > code;
    > goes;
    > here;
    > } else {
    > more;
    > code;
    > }
    >
    > Notice how that one line ("here;") is indented wrong? Obviously, that
    > doesn't meet our coding standards, yet there's no way for the compiler
    > to catch it, because the compiler looks only at the braces and ignores
    > the indentation!


    And Tim replies:
    > Here again indentation doesn't matter. A smart editor could do a
    > "pretty-print" for you using the braces. I agree that *visually*
    > indentation w/o the braces are nice, but as a check for having code
    > in the right logical block, it's just too easy have a stray extra or
    > missing space (for me anyway) that would throw everything off. Isn't
    > just one space needed in Python to signify a different block?


    You see, this is exactly my point. Indentation DOES matter... even
    in C. It just doesn't matter TO THE COMPILER. But it still matters
    to those who have to read it and edit it. In Python, even a *dumb*
    editor will "pretty-print" for you.

    Your last question suggests a possible explanation. Perhaps you
    think that Python code will execute incorrectly if a single extra
    space is typed someplace. That simply isn't true. Python relies on
    two different cues to indicate a block... one is the indentation,
    and the other is the presence of a colon (and a statement like
    "if" or "def" which can introduce a block) on the previous line.
    If you accidently type an extra space, creating something like
    this:

    if condition:
    do()
    some()
    stuff()

    ....then the compiler will find a syntax error (at compile-time, not
    runtime) and complain about inconsistant indentation. This lets you
    fix indentation mistakes. If you WANT to write code like this:


    if (condition) {
    do();
    some();
    stuff();
    }

    then you're out of luck (and crazy to boot!). But if mis-indented
    code is just an accident of messed up typing, then Python helps you
    to catch the mistake. I could similarily complain about C breaking
    every time I type an accidental "{" in the middle of my code, but
    the argument would carry no weight because the compiler would
    catch such a mistake at the next compile. And that's just what the
    Python compiler will do for randomly typed spaces.

    -- Michael Chermside
     
    Michael Chermside, May 19, 2004
    #1
    1. Advertising

  2. Michael Chermside

    John Roth Guest

    "Michael Chermside" <> wrote in message
    news:...
    >
    > 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
    >
    >
     
    John Roth, May 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen
    Replies:
    3
    Views:
    450
    Scott Allen
    Feb 27, 2005
  2. CYBER
    Replies:
    32
    Views:
    711
    Ville Vainio
    May 25, 2004
  3. Michael Chermside
    Replies:
    0
    Views:
    291
    Michael Chermside
    May 19, 2004
  4. matt
    Replies:
    1
    Views:
    278
    George Ogata
    Aug 6, 2004
  5. Steven Taylor
    Replies:
    9
    Views:
    259
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page