I'm disappointed that of all the patterns presented, most of them actually increase the complexity to some extent, and none of them are the use of a lookup table/array. IMHO that is the "real anti-if pattern" here, and it can immensely simplify code. I've taken long, convoluted nested if/else chains with plenty of duplication and turned them into a single array lookup. A bonus is that performance and size often benef…
Anti-if: The Missing Patterns
31–40 of 74 posts
Re: Anti-if: The Missing Patterns
#32This recent article seems relevant: http://www.sandimetz.com/blog/2016/6/9/make-everything-the-s...
This is actually a better read than the OP. Thank you.
Re: Anti-if: The Missing Patterns
#33One thing I've always done with if statements is when I have an if with a negated condition and no else case if (!condition) { // do stuff } When I want to add an else case, I swap the order so the condition is no longer negated. if (condition) { // do other stuff } else { // do stuff } To me it's easier to read a non-negated condition and the else case then feels more natural. (It avoids having to internally think a…
Re: Anti-if: The Missing Patterns
#34> Patten 3: NullObject/Optional over null passing The solution here disperses the responsibility of keeping sumOf safe to its callers, all over the place, instead of a single location in sumOf.
Agreed. The sumOf function is now made more brittle by that fact, and would be unworkably so in a publicly-available API. This is easily fixed by a try-catch block if one finds that they are indeed allergic to if statements, although that too is a form of flow control. If you are programming in Go, you're going to have to bite the bullet and use an if. (I suspect the author would prefer jumping in a lake over program…
https://docs.oracle.com/javase/7/docs/api/java/util/Collecti...
No dynamic heap allocation necessary.
Re: Anti-if: The Missing Patterns
#35I'm disappointed that of all the patterns presented, most of them actually increase the complexity to some extent, and none of them are the use of a lookup table/array. IMHO that is the "real anti-if pattern" here, and it can immensely simplify code. I've taken long, convoluted nested if/else chains with plenty of duplication and turned them into a single array lookup. A bonus is that performance and size often benef…
I do this a lot too. Often long chains of if statements are really performing a manual mapping from one set of values to another. Just keep a map and perform a lookup! Easier to read, easier to change, and easier to extend since the mapping is a data structure instead of code.
Re: Anti-if: The Missing Patterns
#36Re: Anti-if: The Missing Patterns
#37IMHO These are by far the two most important ways to get rid of complex code embedded with ifs.
One very good example of this is erlangs factorial method.
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
There are many other nice examples at the erlang documentation:https://www.erlang.org/course/sequential-programming#funcsyn...
Re: Anti-if: The Missing Patterns
#38I had a professor in university who was frequently saying he developed a thousand application and he didn't use any if. I know it's extreme but is there any way to reduce (lets say 5 to 1) conditions? I think he was developing Fortran apps. Anecdote: This very same professor asked us to do a matrix operation (I don't remember what) without using if once. He said it would be faster than using ifs. Many of us couldn't.…
It however, do represent branching, and in a hotspot it can be more efficient to simply calculate both the if and the else and just choose the right value, rather than having a branch misprediction.
In any case, always profile, and identify issues before doing any optimization.