Re:Refactoring a 300-line if
by _asummers on Tuesday December 01, 2015 @02:36PM
(#10658093)
The reduce(..., True) function kind of bothers me in this code. Python has the all(...) function to achieve that functionality.
[ Parent
| on HN ]
It bugged me too. Here's the equivalent logic without even using all (which is definitely the right approach):
for rule in self.rules:
if not rule.canPost:
return False
return True
That's the same number of lines of code, doesn't require two imports, and is way easier to follow. I love functional programming but Python (intentionally, see reduce in py3) is not a language for doing it. return all(rule.canPost for rule in self.rules)
Re:Refactoring a 300-line if
by openasocket on Tuesday December 01, 2015 @03:49PM
(#10658637)
what bugs me is that his method means every single rule is always evaluated: no short-circuiting! So the code to sort the rules by weights, which is supposed to put stricter rules first to cut down on computing time, is completely useless.
Noticed the same thing, wondered if I was missing some niche side effect here. TBH, I've noticed there's almost never a reason for a general reduce() in Python (to my usual dismay, as an ex-Clojure programmer). It does result in clearer, less clever code for the most part.