alternatives to making blocks like { } or other ??

Williams, Timothy J Mr RDECOM CERDEC NVESD timothy.williams at nvl.army.mil
Wed May 19 11:01:21 EDT 2004



> -----Original Message-----
> From: Michael Chermside [mailto:mcherm at mcherm.com]
> Sent: Wednesday, May 19, 2004 9:09 AM
> To: timothy.williams at nvl.army.mil; python-list at python.org
> Subject: alternatives to making blocks like { } or other ??
> 
> 
> 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();

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

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

Looks like an argument for braces to me.


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

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?




More information about the Python-list mailing list