top | item 13808561

(no title)

MrDosu | 9 years ago

I always wonder how these "five conditions ANDed together in an if" make it past code review in general (i know thats not where the actual bug was). I don't care how brilliant a programmer you are, but mistakes in those are very difficult to grasp.

discuss

order

jacquesm|9 years ago

That's the equivalent of a 32 branch switch statement in one line.

So only 31 other cases to test for, now let's hope they did all those tests.

dtran|9 years ago

I'm definitely not advocating for unnecessarily complex or long conditionals on a single line or any other hard-to-grok code, but this comment reminded me of something I just learned recently! Someone on my team introduced me to property-based testing, which generates ranges of test cases that would otherwise be very repetitive to write manually. We've used http://hypothesis.works/ in a few places recently and caught bugs that we might have missed if we had just written tests the usual way.

anjc|9 years ago

As convoluted as they can be, what alternatives are there other than nested conditions, which are equally convoluted? Surely if you need to logically compare five variable then you'll be doing it in some variation of 5 chained operations.

MrDosu|9 years ago

There is a couple of ways to do it. From storing your conditions in meaningful variables outside the conditional to pattern matching and more. If conditions aren't the only branching operators.

dagw|9 years ago

I always wonder how these "five conditions ANDed together in an if" make it past code review in general

Easy. Either there is no code review, the code review is done by someone who has far too much on his plate a works on the theory that as long as it compiles and runs it's probably fine or the code review is done by someone likes writing code with five conditions ANDed together in an if and thinks it's fine