if statement - if and else if do the same thing -
i'm trying refactor if-else chain doesn't particularly good. common sense telling me should able call method once can't figure out elegant way it. have is:
if(condition1) method1; else if(condition2) method1; which looks hideous. there's repeat code! best can come is:
if(condition1 || (!condition1 && condition2)) method1; but looks bad, since i'm negating condition1 after or, seems unnecessary...
i made truth table this:
c1| c2| r 0 | 0 | 0 0 | 1 | 1 1 | 0 | 1 1 | 1 | 1 and in case interested, real-life problem i'm having got 2 intances of fancytree in javascript , want set rules transferring nodes between them. tree can transfer lone nodes tree b, while tree b can reorder freely, put on tree b's dragdrop event:
if(data.othernode.tree === node.tree){ data.othernode.moveto(node, data.hitmode); } else if(!data.othernode.haschildren()){ data.othernode.moveto(node, data.hitmode); }
you can simplify more - if first condition true, method should invoked regardless of second condition. !condition1 in refactored code redundant. instead, have:
if(condition1 || condition2) method1; in modern programming languages if condition short circuit. means when first condition evaluated true, second condition won't evaluated.
Comments
Post a Comment