4 ms·
IMO the first example is a good use of a linter, the second should be caught by coding style: I keep the following rule with closures: 1.) Never go more than t
by m_mueller 10y ago
IMO the first example is a good use of a linter, the second should be caught by coding style: I keep the following rule with closures:
1.) Never go more than two layers deep, so you have one "def" inside another "def" and that's it.
2.) Always keep the closures at the top of the containing function - no further nesting.
Kids today coming from Javascript seem to be way too much in love with their closures.
Example:
def my_routine(flag, foo):
def my_little_helper(thingy)
if flag:
return thingy
return thingy + foo
return my_little_helper(42)
- chrisseaton 10y agoWhat do you think is the problem with deeper nester closures? Do you really think that a little extra lambda or something inside your helper function would suddenly make it unreadable?
- m_mueller 10y agoI just think multiple levels of capturing scope into closures is a bad idea - if something with that state goes wrong it will be hard to debug. I also hardly see any benefit in doing it.
- nilliams 10y agoNo need to pick on 'kids' from JS thanks, and actually in my interviewing experience a lot of JS devs don't actually know what a closure is. Worrying, but true.
- m_mueller 10y agoYeah, sorry for the sarcastic tone. It's just that JS/Ajax comes to mind when I think about deeply nested closure pasta. Here's hoping that ES201x will teach async/await to a wide audience soon.
- itamarst 10y agoThat's a good idea, but you shouldn't assume too much about why people are making mistakes, I've been working with and on Twisted since 2001, so not a JS kiddy :) I made this mistake fair and square even though I knew better. Which is why I like having a tool that'll catch it for me. My typical approach is `lambda x, y=y: f(x, y)`. Inline functions are much more readable, is the thing. But I can see the argument for this coding style, yes.
- m_mueller 10y agoSorry, didn't want to attack you personally with the 'kiddy' comment. I think my proposed style mashes well with yours though.