Skip to content

Commit

Permalink
Merge pull request #16 from awwad/master
Browse files Browse the repository at this point in the history
Add nesting and if/else clause guidelines
  • Loading branch information
vladimir-v-diaz authored Nov 27, 2017
2 parents 813df66 + 408d954 commit 34b7f41
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ Use blank lines in functions to indicate logical sections and help to offset
comments.


### Nesting ###

Where possible, for readability, avoid employing excessive levels of nesting
and `if` and `else` clauses separated by a lot of code.

No:

```python
if bizbaz:
bizbaz.frobnicate(flimflam)
print('Frobnication result: ' + repr(bizbaz))
...
...
...
else:
raise YamException(...)
```

Yes:
```python
if not bizbaz:
raise YamException(...)

bizbaz.frobnicate(flimflam)
print('Frobnication result: ' + repr(bizbaz))
...
...
...
```

If the not-bizbaz clause isn't going to return or raise an exception right away
and you therefore need both `if` and `else`, it's slightly preferable to keep
the shorter clause first, so that the `else` isn't too far from the `if`.


## Imports ##

Imports should usually be on separate lines, e.g.:
Expand Down

0 comments on commit 34b7f41

Please sign in to comment.