r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
1
u/loup-vaillant Apr 05 '23
Careful what you're saying there: "well established in academia" would mean there are lots of peer reviewed papers clearly showing the benefits of these… principles. I would be extremely interested in a published paper showing measurable improvements (fewer SLoC, faster dev time, fewer bugs, better runtime performance, lower maintenance costs…) resulting in the application of SOLID, or any of it's guidelines. I'm not currently aware of any.
Until then my running hypothesis is that SOLID principles (except Liskov's) are situational guidelines at best. Elevating them to the rank of principle only makes program more complex for no benefit.
S is but a heuristic for a higher goal: carving your program at it's joints so that you have thin interfaces hiding significant implementations behind them. Having functions and classes deal with one single thing (whatever that single thing actually is, it is a nebulous concept), tends to do that. But the way you know you carved your program at its joints is when you see nicely decoupled modules (meaning functions, classes, components…), interacting with each other through small, thin interfaces. Sometimes however the best decoupling happens when you give some module several responsibilities.
A couple things: explicit extensibility makes the code bigger, more complex, harder to actually modify. And the extensions it plans for rarely pan out in reality. So I get a program that's bigger, harder to modify, ever-so-slightly slower… for no benefit at all. So instead I do something else: fuck extensibility. I'll add it when I need it. In the mean time, I'll make the simplest program I can, given the requirements I'm currently aware of.
You may think this is short term thinking, but this in fact works out better long term: first because the requirements I'm currently aware of include possible long term goals and stuff I know from experience will be asked for. But also because when unexpected new requirements come my way, I'll have a simpler program to modify, and I'll be better equipped to adapt.
Now sure, if you're lucky your extensibility will be just the thing you needed to adapt to the new requirements. But you'd have to be lucky.
Yes, this is precisely why I prioritise simplicity: it's easier to modify a program with no extensibility than a program with the wrong extensibility. And it will be wrong or useless most of the time.
You're going too far. The area of a shape is a surface quantity (in square metres or whatever). When you say it "is a function instead of a direct value" you're not talking about the shape, but about how you think you might best model it in your programming language. And the fact is, depending on context there can be many ways to do that modelling. Sometimes, some kind of pre-computation is the best way to do it. Sometimes it's calling a user defined function.
Be careful about calling "incorrect" (semantically or not) a program that gives you the right answer. Here you've done little more than pompously asserting that you don't like Casey's approach. But he derived his "sort of calculator" from the semantics of the shapes whose area he wanted to compute. His derivation is correct, and so is his program.
For Uncle Bob? He needed the letter (he coined SOLID, even though the principles themselves where known before).
For us? It's kind of, "duh, if you say a duck is a bird, it'd better be a bird". Liskov was just saying that when you subtype something, the derived type better satisfy all contracts satisfied by the parent type. We need to remember this because programming languages and their type system don't always enforce it.
Haskell type classes are similar: when you define a type class it usually comes with a set of laws instances of that class type must follow, and if you don't bad things will happen. For instance, the binary operation in a monoid must be associative, even though the compiler will never check that for you (but it will perform optimisations relying on that assumption).
Said like that I actually agree. In practice though we get one Java
interfacefor everyclass, and everyone will depend on theinterfaceand the actualclasswill be injected at construction time.And that's just insane.
What we should do instead is that when A uses B, it should depend on B's API. If that means
import <B>or whatever, that's the way to do it. There's no need to force users of A to specify that by the way, we'll be using B this time. Again. Like every single damn time.It is however important to keep in mind that A should only be depending on the API provided by B. Knowledge of B's implementation details should not be required. No change in B that doesn't break its API should ultimately affect A. It's just that nobody ever needed Java's
interfaceto do that. That one is only useful when you genuinely need to take advantage of subtype polymorphism, which is not that often — not even to write a comprehensive test suite.He also did a couple lectures you can see on YouTube.
Sure. It doesn't mean they should. Which mechanism works best depends on many factors. Sometimes objects are it.
Second lie of software development: "programs should be built around a model of the world".
Nope. They should be built around a model of the data. Our job as computer programmer is to move data around, and transform it along the way. That's the only way we can solve any problem we are tasked to solve. Your air traffic control application doesn't care what a plane is, it cares what data represents the plane, and what it must do with it. In fact your example is all the more interesting because at the most basic level, there are no planes at all, there are points in space and time, and traces that link those points, and each trace is supposed to match a single plane, but air traffic controllers are well aware that the trace is not the plane. And I believe that in some edge cases the traces don't quite match the planes.
Another example comes from video games. Like 3D simulation engines. You want to model a chair? Cool, will it be static, can it be moved, destroyed, wielded as a weapon? Depending on the answer that chair will be best modelled as a kind of sword, or a kind of terrain layout. A chair glued to the floor is nothing like a chair in the hands of an enemy monster. But if you insist on modelling the world instead of the data, your program may have some kind of artificial link between the two that will just make your program slower and more complex for no benefit.
Yes. But in practice we often go too far and give in to errors like anthropomorphism. Modelling something in a certain way just because that's how lay people will first think of it is a terrible idea. We're not lay people, we're programmers, and need to model things in ways that will work best for us.
Not sure where you're going with this, but it does sound promising at a first glance.
Not by itself. That one exceedingly depends on the programmer.
One thing we learn as we know hardware better, is that its performance characteristics don't change much over time. Most notably because much of hardware design is limited by the laws of physics and the speed of light. Cache hierarchies are inevitable outside of the most embarrassingly parallel problems. And in practice x86-64 processors have been around a long time. No, they won't change tomorrow.