if condition {
frobnicate_batch(walruses)
} else {
transmogrify_batch(walruses)
} match (condition_a, condition_b){
(true, true) => fn_a()
(true, false) => fn_b()
(false, true) => fn_c()
(false, false) => fn_d()
}
isn't preferable to this: if condition_a {
if condition_b {
fn_a()
} else {
fn_b()
}
else if condition_b {
fn_c()
} else {
fn_d()
}
(Assuming the syntax is available)For instance
if !condition_a && !conditon_b {
return fn_d() // probably an error condition ?
}
if condition_a && condition_b {
return fn_a()
}
if condition_a {
fn_b()
} else {
fn_c()
}https://godbolt.org/z/a1Yq9rceE
Note: Inverting and rearranging conditions changes what LLVM decides to output, sometimes for the worse. --opt-level=s is counterproductive here.
https://github.com/titzer/wizard-engine/blob/master/src/engi...
This would be much, much better if Virgil had pattern matching on tuples of ADTs.
The important thing to learn about all “rules of thumb” and “best practices” is the WHY behind them. Programmers especially should not copy/paste these things and apply them by rote.
RoT and BP blindly applied may well not be a good idea. As with everything “it depends”.
walruses.apply(condition ? frobnicate : transmorfify)
where apply does the looping. // There are better ways to accomplish this, depending on the language
func frobnicate(walrus) {
return walrus.frobnicate();
}
func transmogrify(walrus) {
return walrus.transmogrify();
}
// Somewhere high in the code
if condition {
processing_function = frobnicate
} else {
processing_function = transmogrify
}
// Somewhere else in the code
for walrus in walruses {
processing_function()
}
If the decisions are made as early as possible, they do not need to be littered throughout the code. The guts of the code can run without branches, performing the same operations every time, the output only modified through the construction graph.Of course, this is not a new idea: https://www.youtube.com/watch?v=4F72VULWFvc
It was old 15 years ago.
In an extreme case that I've seen in real code, processing_function() is called send(t: Object) -> Object or recv(u: Object), and all non-trivial control flow in the system is laundered through those two function names.
So, you have 100's or 1000's of send, recv pairs, and grep (or even a fancy IDE) can't match them up.
That being said, yeah, its possible to just layer unnecessary abstraction and way over complicate things, making the layers utterly incomprehensible. But that seems to be the result of a misunderstanding- the point here isn't to _abstract_, it's to properly model polymorphic behavior. Any abstraction that takes place to do that is suspicious at best.
I think the performance aspect is negligible for 98% of programs. The compiler may or may not be able to inline the underlying function, depends on the compiler. Depends on the optimizations. And in almost all cases, if you're relying on compiler optimizations to fix your performance problems, you're looking in the wrong place. Not to say such cases don't exist, and there's a slew of people who work in those areas, but I trust they know their business well enough to evaluate the trade offs here and make the right decisions.
Godbolt.org has a great example for Swift. Switch the editor to swift mode, load the "Sum over Array" example, and compile it with defaults. It produces reams and reams of assembly code. Now, pass -O in as a compiler flag to enable optimization, and it produces extremely tight code. (Swift 5.0 even noticed the two functions are semantically identical, and deduped them. 5.9 doesn't, but it also produces more compact code.)
It's a similar story for rust, since the example is written in a functional style with an iterator. The C and C++ versions only have an imperative for loop variant, so the optimizer doesn't matter as much for them. (Though you could easily produce a C++ version of the code that takes a sum() function, and has similar bloat until optimization is enabled.)
Yes.
I for one am happy such articles exist. Thus one articulated something I've run into quite a few times without being able to fully articulate the issues. It seems like a nice mental model to have in my back pocket.
That said, I appreciate comments like your because I hope the dogmatic junior dev comes across it and hopefully becomes a little more nuanced.
* you already have the intuitive understanding of problems being described
* but you haven’t quite yet verbalized the problem and connected the dots
This clicked for me yesterday, happy that it helped you to articulate what you’ve already knew today!
I’ve seen dogmatic junior devs turn into dogmatic senior devs, but I’ve never seen a dogmatic junior dev turn into a pragmatic senior dev.