Why not allow empty code blocks?

BartC bc at freeuk.com
Sun Jul 24 10:44:37 EDT 2016


On 24/07/2016 13:17, Steven D'Aprano wrote:
> On Sun, 24 Jul 2016 08:35 pm, BartC wrote:

>> Given an otherwise correctly typed program that compiles with no errors,
>> then it is very easy (if Backspace or Delete is inadvertently pressed
>> for example), for an indent to disappear without your noticing,
>
> Not really. It depends on the editor, but typically you need to have your
> text insertion point somewhere in the indent.
>
> And when you do accidentally press Delete, what happens? The high-level
> structure of the code changes. Typically things will no longer align:
>
> def f():
>     for x in seq:
>         do_this()
>     do_that()
>         do_more()
>
> which is a SyntaxError.

Unless it happened on the do_more() line.

> It requires quite the coincidence before you can
> accidentally delete an indent and have the code still run.

On my editor, if I press the cursor up and down keys, the current column 
moves to the left if I pass a blank line and stays there. In Python 
code, then the cursor will often end up at the start of an indent.

  Far more likely
> is that accidentally pressing delete will break the code in a way that
> can't be detected until runtime:
>
> def f():
>     for x in seq:
>         do_this()
>         d_that()
>         do_more()

Yes, I mentioned that; it will cause some exception. But moving 
do_more() out of the loop above might not do so.

>
> Conclusion: if you're the sort of person who habitually presses the Delete
> or Backspace key without paying attention, you're going to have a bad time.

It happens in all sorts of ways.

Your attention is diverted, you're doing something on your desk, but you 
hit one of the keys by mistake. You might have pressed Delete or you 
might not. You look at the screen which has a 5000-line program open, 
and you see this (borrowing your example and with the cursor at "_"):

  def f():
      for x in seq:
          do_this()
          do_that()
  _   do_more()

Did you just unindent do_more(), or is that where it's meant to be? Undo 
may or may not help (or it may undo something is needed).

If you see this however:

  def f():
      for x in seq:
          do_this()
          _o_that()
          do_more()

You can see that o_that() doesn't look like its neighbours, and you can 
verify there's no o_that() in scope.

-- 
Bartc



More information about the Python-list mailing list