alternatives to making blocks like { } or other ??

Michael Chermside mcherm at mcherm.com
Wed May 19 09:08:43 EDT 2004


Tim Williams writes:
> I have to put my 2 cents in here. I love Python, but the one thing I
> miss is using {} or something to enclose blocks. I edit in emacs
> python-mode, and believe in indentation, but sometimes I'll
> inadvertently change a line of code's indentation and it throws the
> logic off. I found a bug in one of my programs recently where a line
> of code should have been outside of an 'if', but wasn't because I hit
> TAB one too many times. A {} block would've caught that. I know that's
> just being careless, but I need all the help I can get!

I don't mind using C-derived languages occasionally, but one thing that
bothers me is having to use {} or something to enclose blocks. I use
various editors, and I believe in both delimiting and indenting, but
sometimes I'll inadvertently change indentation or move the position of
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();

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;
    }

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! I know that's just being careless, but I need all the
help I can get! If only there were a better language I could use.

-- Michael Chermside





More information about the Python-list mailing list