TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Refactoring a 300-line if

61 pointsby 6502nerdfaceover 9 years ago

19 comments

amichalover 9 years ago
... into 1000-ish lines in python and some xml dialects spread over 20ish files that may or may not do the same thing... covered by 5 tests when there are clearly more then 5 initial states driving the original if....<p>I would have left it as the big if.<p>Source: <a href="https:&#x2F;&#x2F;github.com&#x2F;groupserver&#x2F;gs.group.member.canpost" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;groupserver&#x2F;gs.group.member.canpost</a>
评论 #10658735 未加载
评论 #10658806 未加载
Scarblacover 9 years ago
That was an exciting ride! First an if statement that was longer than one would want, then he was splitting things off into small bits and a loop to make it look neat and dynamic, and then it <i>exploded into bullshit</i> and stopped looking like Python altogether.<p>Guess it happens.
评论 #10659232 未加载
评论 #10659236 未加载
iamleppertover 9 years ago
Wow, everything I hate about &quot;Enterprise Software&quot; in a single if statement. Have you ever noticed there are entire businesses built on fine-grained access control like this, that are utterly pointless bullshit? Probably full of security holes, leaky abstractions, poorly tested and responsible for creating a lot of support cases and manual intervention.<p>Figure out what the trust modality in your application is, and normally it&#x27;s a simple graph or can be managed with a straight-forward application of world&#x2F;group&#x2F;user UNIX-style permissions.
评论 #10659229 未加载
tcdentover 9 years ago
I don&#x27;t care how &quot;broken&quot; my multi-line if statement is, I would <i>never</i> add Zope as a requirement.<p>Also, singleton (yuck) accessed via a helper function: yuck. Just import it.<p>This starts to make sense where OP professes fondness of XML: we have a Java developer!
评论 #10658620 未加载
评论 #10658482 未加载
mmaginover 9 years ago
That&#x27;s not a 300 line if statement, it&#x27;s broken down into many functions.<p>But anyway, I see the world of enterprise Java has reached Python.
评论 #10658841 未加载
评论 #10660048 未加载
评论 #10659432 未加载
inceptedover 9 years ago
He replaced a huge if&#x2F;else with a rule engine.<p>The idea is sound since that&#x27;s exactly what rule engines are for but I feel that if you don&#x27;t also post the final listing of the rules you end up with, you haven&#x27;t really made the case that you improved the code.
bcoatesover 9 years ago
If you want to actually solve this problem without doing the thing in the slideshow or shooting yourself, when you see<p><pre><code> if(self.predicate_function()) foo() else bar() </code></pre> and self.predicate_function() is only called once in the program (this is the case several times in the slideshow example), try pulling the if statement into the function (and simplify), making a<p><pre><code> self.handle_case_something() </code></pre> function instead. If the predicate is called more than once, consider subclassing. You&#x27;re already doing OO, may as well do it right.
评论 #10660045 未加载
mtdewcmuover 9 years ago
Btw, here is a simpler technique I&#x27;ve used for shortening over-long if-statements:<p>Replace<p><pre><code> if(A) { &#x2F;&#x2F; 300 lines of code... } else { return FALSE; } </code></pre> with<p><pre><code> if(!A) { return FALSE; } &#x2F;&#x2F; 300 lines of code...</code></pre>
评论 #10662665 未加载
shanemhansenover 9 years ago
Huh. Started out with a valid problem, but the solution was terrible. The validity of the question doesn&#x27;t prove the correctness of the answer.
评论 #10659401 未加载
_asummersover 9 years ago
The reduce(..., True) function kind of bothers me in this code. Python has the all(...) function to achieve that functionality.
评论 #10658171 未加载
评论 #10658637 未加载
评论 #10658172 未加载
justizinover 9 years ago
I tried several times to refactor a 10k+ line switch statement which had a particular case that by all logic should be unreachable, but had a comment something like this:<p><pre><code> &#x2F;* I don&#x27;t know why, but if you remove this everything breaks *&#x2F; </code></pre> I wonder if that is still there, it&#x27;s been over ten years.
评论 #10658105 未加载
评论 #10658714 未加载
评论 #10658230 未加载
评论 #10660783 未加载
roma1nover 9 years ago
Is the logic equivalent though? In the initial example, there were at least four outcomes (post, notify not a member, notify not a posting member, notify group closed) which seems too many to map on a boolean.
exploriginover 9 years ago
Perhaps a better tool to refactor any such large if-statement: <a href="http:&#x2F;&#x2F;drakon-editor.sourceforge.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;drakon-editor.sourceforge.net&#x2F;</a>
fleitzover 9 years ago
Typical case of code providing policy instead of mechanism, the solution of course being to provide policy over numerous classes, rules engines, etc instead of writing a few methods that provide mechanism.<p>eg. Why is this method concerned with whether the notification is email or something else...<p>Are persons not also the case of a group with one member?<p>It seems less like refactoring and more like hiding complexity in frameworks.
Raphmediaover 9 years ago
Did anyone figure out how to zoom the slides?
评论 #10659720 未加载
评论 #10658720 未加载
gravypodover 9 years ago
If all you are doing is trying a bunch of cases (or filters) on some data, why not just use all and a list comprehension on functions.<p>all([postAllowFilter() for postAllowFilter in filters]) Then just take a list of all of your functions.<p>Really, there just should have been a restructuring of the initial code flow. It didn&#x27;t look too messy.
mtrnover 9 years ago
I noticed a variable named retval in a few slides and was irritated for a moment.<p>Maybe because I stopped using variable names like &quot;retval&quot;, &quot;data&quot; or even &quot;result&quot; - because they convey so little meaning.
评论 #10659730 未加载
donarbover 9 years ago
Then there&#x27;s the problem of parens on every `if` statement, not needed in Python unless logic requires them.
qnaalover 9 years ago
&quot;should have just used perl&quot;