Why not allow empty code blocks?

Antoon Pardon antoon.pardon at rece.vub.ac.be
Tue Aug 2 03:31:22 EDT 2016


Op 30-07-16 om 05:49 schreef Steven D'Aprano:
> On Sat, 30 Jul 2016 04:32 am, Antoon Pardon wrote:
>
>
> Perhaps its *you* who doesn't understand. The subject line talks
> about "empty code blocks", and Bart has suggested that instead of having to
> write
>
> ...
>
> So now you're changing your story, and claiming that you're not actually
> replacing an existing except block with pass (because I think any
> experienced Python developer, which Bart is not, realises that's not likely
> to work). Now you've got a use-case where we start with:

I am not changing my story, I am describing it from a different view point.

> # before
> some code
> more processing
>
> and add an entire try...except around it:
>
> # step 1
> try:
>     some code
> except SomeException:
>     debugging code
> more processing
>
> *and then* comment out the debugging code and replace it with pass:
>
> # after, as stated by you
> try:
>     some code
> except SomeException:
>     # debugging code
>     pass
> more processing

No I start with writing the above code and the debugging code uncommented.

> Of course it's legitimate to wrap some failing code in a temporary
> try...except block for debugging purposes. But that's not an empty code
> block, and its not "pass", and it's not relevant to Chris' question.

You still don't seem to understand. This code doesn't fail. SomeException
can normally safely be ignored. However some of the called functions might
throw it wrongly.

> And what happens once you have finished debugging the code? According to
> your post, you comment out the debugging code, replacing it by "pass". And
> you do this *frequently*.

That depends what you mean with *frequently*. It depends on the nastyness
of the bug I am chasing. 

> Okay, if you say you do that, I have to believe you. If we take you at your
> word, then we can only conclude that your code is littered with the
> detritus of old debugging code and unnecessary try...except blocks which do
> nothing. That's practically the definition of *poor programming*, leaving
> old code commented out to rot, but if you say that's what you do, who am I
> to argue that you don't?

You are not taking my word for it, you are jumping to conclusions. The
fact that I do this at some point, doesn't mean I leave it in indefenatly.

> Since I don't actually believe you are that poor a programmer, I suspect
> that what you actually do is not replace the debugging code with "pass",
> like you claim, but that once you have finished debugging that section of
> the code you remove the unneeded try...except block and dead, unused
> debugging code, returning it to the "before" state.

But it isn't an unneeded try ... except block. I need the try ... except
block to ignore an exception that should be harmless at that point.

> D'Arcy's suggestion of leaving the debugging code in with a runtime switch
> to turn it on and off also makes sense. 

Yes that makes sense. However if I think that is worth the while I usally
just start using the logging module.

-- 
Antoon






More information about the Python-list mailing list