Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-style for indention of multi-line 'if' statement? [duplicate]

When indenting long if conditions, you usually do something like this (actually, PyDev indents like that):

if (collResv.repeatability is None or     collResv.somethingElse):     collResv.rejected = True     collResv.rejectCompletely() 

However, this puts the block started by the if statement on the same indentation level as the last part of the if condition which makes it very ugly/hard to read in my opinion as you don't immediately see where the block starts.

Some other styles I thought about:

if (collResv.repeatability is None or         collResv.somethingElse):     collResv.rejected = True     collResv.rejectCompletely() 

This looks pretty inconsistent as the second line is indented much more than the first line but it's readable.

if (collResv.repeatability is None or   collResv.somethingElse):     collResv.rejected = True     collResv.rejectCompletely() 

This is also more readable than the first example, but the indentation is not a multiple of 4 anymore and besides that it looks wrong as the second line has less indentation than the beginning of the condition in the first line.


So, my main question is: Is there a suggested indentation style for cases like that which do not require overly-long lines (i.e. a single-line condition)? If not, what do you prefer for cases like that?

like image 478
ThiefMaster Avatar asked Feb 25 '11 12:02

ThiefMaster


People also ask

When writing if statements in Python The second line of code is indented?

After the if-statement, a section of code to run when the condition is True is included. The section of <code to run> must be indented and every line in this section of code must be indented the same number of spaces. By convention, four space indentation is used in Python.

How do you write multiple lines in if condition Python?

In Python, I prefer to use vertical space, enclose parenthesis, and place the logical operators at the beginning of each line so the expressions don't look like "floating". If the conditions need to be evaluated more than once, as in a while loop, then using a local function is best.

How do you break a long if statement?

Long if statements may be split onto several lines when the character/line limit would be exceeded. The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)

How do you indent else if?

If the if statement is indented with spaces, use the same number of spaces for else . If the if statement is indented with one or more tabs, use the same number of tabs for the else statement. Don't mix spaces and tabs for indentation.


2 Answers

Often I work around this problem by calculating the condition in an own statement:

condition = (collResv.repeatability is None or              collResv.somethingElse) if condition:     collResv.rejected = True     collResv.rejectCompletely() 

Though, for a still relatively short condition as in your specific example I'd go for nosklo's solution - the extra statement used here is more suited for even longer conditional expressions.

like image 79
Oben Sonne Avatar answered Oct 02 '22 15:10

Oben Sonne


This is what I do:

if (collResv.repeatability is None or         collResv.somethingElse):     collResv.rejected = True     collResv.rejectCompletely() 
like image 28
nosklo Avatar answered Oct 02 '22 15:10

nosklo