DEV Community

Discussion on: Unconditional Challenge: FizzBuzz without `if`

Collapse
 
defenestrator profile image
Jeremy Jacob Anderson • Edited

I can't bring myself to be terribly interested in re-implementing language features like boolean or ternary. I loved seeing some of the really outlandish solutions like that un-sane RegEx that was posted. I enjoyed seeing the contortions done to avoid conditionals, too, but that's not something I'm into for it's own sake. I'm happy to fail at it.

There is a meaningful distinction between ternary and if.

if is a wilderness with few boundaries, as a form of Many-Valued Logic with an arbitrary number of values. Ternary is a specific subset of Many-Valued Logic; Three-Valued Logic. The Three-Valued Logic of ternary operators provides specific limitations that are not necessarily implied by the N-Valued Logic of the if statement. In most programming languages ternary is an operator, and if is a control structure block. Operators can be used in expressions, which is generally not true of if statements. Ternary is usually slightly computationally more expensive than if but has some advantages in terms of limiting complexity.

In real-world code I'm going to write an if (without else if or else if I can help it) about 95% of the time. It is both more readable and more performant in most cases. But when I need an expression that can make a decision between boolean and a maybe, I reach for ternary.

If you wanted to argue that switch is basically goto but potentially worse due to unintended fallthrough, then we'd have something to agree on.

The magic of the fizzbuzz problem is, of course, that it helps us understand in more granular detail that our design decisions, and design non-decisions, come with unavoidable trade-offs. It is a meditation on (a horrible term) non-functional requirements.