assignment expression peeve

Paul Rubin http
Wed Oct 15 13:26:35 EDT 2003


Ben Finney <bignose-hates-spam at and-benfinney-does-too.id.au> writes:
> The example you gave was contrived, sure.

It wasn't contrived, it was from a real program I was writing at the
time, except I abstracted out some of the code.

> But even it was getting complex enough that it was hard to parse
> visually -- and using a switch or case structure wouldn't have made
> it easier.  The problem was the fact that each case was doing
> something completely different.

Well that's normal with case structures.  If the cases are all doing
the same thing, there's no need for a case structure.

> Once each case is more complex than a smple statement or two, it makes
> more sense to handle them in separate functions.  If a task expands to
> several statements, the likelihood is that it will keep expanding.
> Better that it do so in a separate function rather than bloating some
> monstrous switch structure.

Four cases with 2 to 5 lines of code on each case is not a monstrous
structure.  Making a bunch of separate named functions and a dispatch
system is far more bloated and confusing than an if/elif chain.

> > But the if/elif/elif idiom exists because a lot of the time, it's the
> > most natural and clear and correct way to write the code.
> 
> Yes, when each case is trivially simple and easy to read along with all
> the others, it does make sense to keep them together.  The if/elif/else
> structure works fine there.
> 
> That's not so for the example you gave, where each case was more complex
> and differed sufficiently from the others that they were hard to see as
> a single structure.  Thus why I recommended using separate functions if
> you want to do lots of different things.

If/elif was fine for what I was doing.




More information about the Python-list mailing list