Allow multiline conditions and the like

Y

Yingjie Lan

Hi,

This is a mini-proposal I piggy-tailed in the other topic:

Allow the conditions in the if-, elif-, while-, for-, and
with-clauses to span multiple lines without using a backlalsh
at the end of a line,
just like when you specify literal lists, tuples, dicts, etc.
across multiple lines (similar to comprehensions too).

My reasons:

because they all must end with a required colon ':',
so nobody will mistake it.

also, if we don't allow it, people just have to use
parenthesis around the expressions to make that happen.


Just a half-baked idea, appreciate all comments.

Yingjie
 
L

Lawrence D'Oliveiro

You can already do this with any expression: use parentheses.

It’s easy enough to do things like this:

if (
TheMesh.vertices[OtherVertex].select
and
OtherVertex != ThatVertex
and
OtherVertex != ThisLine[-2]
and
OtherVertex != ThisVertex
) :
...
#end if
 
S

Steven D'Aprano

Hi,

This is a mini-proposal I piggy-tailed in the other topic:

Allow the conditions in the if-, elif-, while-, for-, and with-clauses
to span multiple lines [...]
also, if we don't allow it, people just have to use
parenthesis around the expressions to make that happen.

You say that like it's a bad thing.

That is kind of like saying "We should allow people to speed through red
traffic lights, because if we don't, they'll just wait for the light to
turn green!". Er, yes, and the problem is?

If you need a multi-line conditional, wrap it in parentheses.
 
C

Chris Rebert

Hi,

This is a mini-proposal I piggy-tailed in the other topic:

Allow the conditions in the if-, elif-, while-, for-, and with-clauses
to span multiple lines [...]
   also, if we don't allow it, people just have to use
parenthesis around the expressions to make that happen.

You say that like it's a bad thing.

That is kind of like saying "We should allow people to speed through red
traffic lights, because if we don't, they'll just wait for the light to
turn green!". Er, yes, and the problem is?

If you need a multi-line conditional, wrap it in parentheses.

Or, if possible, refactor the conditional into a function (call) so
it's no longer multiline in the first place.

Cheers,
Chris
 
A

alex23

Chris Rebert said:
Or, if possible, refactor the conditional into a function (call) so
it's no longer multiline in the first place.

Or even simpler, assign the condition result to a variable:

a_b_positive = a > 0 and b > 0
if a_b_positive:
...
 
M

Mark Wooding

Chris Rebert said:
Or, if possible, refactor the conditional into a function (call) so
it's no longer multiline in the first place.

No! This /increases/ cognitive load for readers, because they have to
deal with the indirection through the name. If you actually use the
function multiple times, the mental overhead of forming the abstraction
and associating it with the function name is shared across the various
call sites and it's probably worth it. If it's only called once, leave
it inline.

-- [mdw]
 
C

Chris Rebert

No!  This /increases/ cognitive load for readers, because they have to
deal with the indirection through the name.

If it's well-named, then the reader can delay having to read the definition..
If you actually use the
function multiple times, the mental overhead of forming the abstraction
and associating it with the function name is shared across the various
call sites and it's probably worth it.  If it's only called once, leave
it inline.

I'd say it's a judgment call. If the condition is sufficiently
complicated and can be well-named, then I see justifying refactoring
it into a function even if it's only used once.
However, this is admittedly not a common situation.

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top