guettli
(Thomas Güttler)
1
I am reading code which looks like this:
def foo():
if ...:
...
...
...
I prefer this:
def foo():
if not ...:
return
...
...
...
In above example I used three “…” lines. As soon as there are more then 5 lines I would like to use “early return”.
Is there any tool in the Python galaxy which does this:
- detects a missing “early return”
- and/or updates the code to use “early return”
Related: My guidelines: use early return
1 Like
scenox
(Scenox)
2
You could use the inspect
module, e.g.:
src=inspect.getsource(foo)
if src[src.find('if'):src.find('return')].count('\n') > 5:
...
1 Like
Asday
(Asday)
3
Check to see if flake8 has a plugin for your desired check, and if not, you could write it yourself.
1 Like
guettli
(Thomas Güttler)
4
Sorry, I fail to understand how your can work reliably. There can be several “if” and several “return” in the source code.