25 ms·
The square roots of all evil
- btilly 2y agoRule of thumb. Don't try to generalize until you have at least 3 distinct examples. This does a lot to find pragmatic tradeoffs between performance and abstraction. It also pushes you away from the over-abstraction that leads to the Second System Effect, as coined by The Mythical Man-Month.
- pjz 2y agoThis is an off-by-one interpretation of the zero-one-infinity rule :)
- dr_dshiv 2y agoThe Dao created the One The One created the Two The Two created the Three The Three created the Ten Thousand Things Chapter 42 of the Daodejing
- klik99 2y agoI call this WET, Write Everything Twice, as opposed to DRY, Don’t Repeat Yourself. If you need to repeat yourself, first copy and paste. If you need to a third time, then you can pull it into a function to generalize. But like nearly everything with programming, hard and fast rules are bad, but it’s a good rule of thumb
- tejtm 2y agoMaybe I am out of the lingo loop but I always thought of this as "We Enjoy Typing"
- reshlo 2y agoIn most places I’ve worked, there’s a big problem with this approach. If you have three distinct examples, and you need to do something that requires a fourth, you won’t be able to justify spending time writing an abstraction. You should just copy it again. The three existing examples don’t use an abstraction, and they work just fine, so there’s clearly no need for an abstraction… or so the argument goes. In the unlikely event that you’re allowed to write the abstraction, you definitely won’t be allowed to reimplement the existing examples to use it now, because those modules are not in scope and we don’t have the budget to test them again. If you don’t create the abstraction up front, you’ve lost the chance to have an abstraction.
- swatcoder 2y agoThat's a direct outcome of the contemporary fashion that code changes should be as narrow as possible and only touch existing code where its strictly necessary to their implementation. It's not the only way of working, but it tends to suit the challenges faced by very large teams with high turnover, diffuse/negligent code ownership, many juniors/early-career contributors, and low trust. It also produces software that resembles papier maché -- increasingly thick, disordered, and glutinous, with poor visibility and low durability. If your team isn't subject to the challenges that make papier maché coding necessary, encourage people to escape its cargo cult. Small teams of people who trust each other, where code has long-term owners who can speak to its purpose and direction, can dynamically sculpt and rework code more like clay. And you get better software when that can be done.
- pornel 2y agoA fourth hack may still be better than having to develop the four features on top of a wrong abstraction. If you end up having a poorly fitting abstraction, then you'll be spending extra effort on doing things "properly", but not really getting any benefit out of it. You just loose the agility of hacking things quick'n'dirty, or you end up hacking around the bad abstraction, and get worst of both worlds. The rest depends on having management that understands trade-offs between doing things properly and efficiently long-term vs getting stuff out of the door quickly (if you're a startup with a short runway, or racing to be the first to win some opportunity, shipping ugly hacks now may be the only way to survive to even have "later" to worry about).
- reshlo 2y ago> A fourth hack may still be better That may be true, but it’s still a problem if you can never create new abstractions, because that prevents you from creating good abstractions too, not just bad ones.
- lll-o-lll 2y ago> It also pushes you away from the over-abstraction that leads to the Second System Effect, as coined by The Mythical Man-Month. The Second System Effect has nothing to do with over-abstraction of code - it has nothing to do with code at all. The basic premise is: First System: We barely did anything we dreamed of and can see 100s of ways to improve things, but we had to get something to market. Second System: We’ve learned so much! Revenue enables big picture thinking! Everything we ever dreamed of let’s design and build! It’s fundamentally a mistake at a business/product design level. That feeling of success leads to a release of constraints and then trying to bite off far more than can be chewed.
- hikarikuen 2y agoI wonder if they were thinking of the inner platform effect
- btilly 2y agoThe second system effect is the same fundamental mistake repeated at every level. From code to business. The fact that the business mistakes are easiest to spot from outside doesn't mean that the coding mistakes aren't there. Even though that level of detail is only visible from inside of the system.
- vunderba 2y agoThe point about building the perfect ontology is something I think we all struggle with. I feel like the long lived Cyc project is a classic example. https://en.wikipedia.org/wiki/Cyc https://en.wikipedia.org/wiki/Cyc I'm also surprised that they didn't clarify that the true square root of all evil is 25.8069758011.
- deleted 2y ago[deleted]
- chrismorgan 2y ago> the true square root of all evil is 25.8069758011. Ah yes, 665.99999999856098676121, such evil! :P
- vunderba 2y agoHaha, but it's a well known fact that Satan rounds up.
- Nevermark 2y agoIt's actually 14.6969385, but you are going to have to think about it! It requires understanding.
- vunderba 2y agoToo easy. ;) Square root of 216 or 6 x 6 x 6.
- revskill 2y agoYou're misunderstanding "thinking before you code" vs premature blahblah. It shares the same mindset as the saying: Fail to prepare is prepare to fail ! The challenges and the evils is in the details of the process of optimization, not that premature optimization is evil.
- FrankWilhoit 2y agoWe have seen generalization too late -- rather, we have seen the need for it, but by definition, if it is left too late, it is impossible. I personally have not seen premature optimization, but I may have led a charmed life.
- spauldo 2y agoI've always considered premature optimization to be a different problem than premature abstraction. Premature optimization is something I struggle with - "hey, if I do this a different way it'll be faster/smaller!" And then I end up spending too much time optimizing one data area of code when I should just do it the easy way and move on to the next problem. I can always come back and optimize later when everything is working and I can see where the problem areas are, but the temptation is often hard to fight. Premature abstraction is harder because abstracting existing code can be problematic and there's more of a chance to introduce bugs. It takes intuition and planning to get it right the first time around, and by the time you realize you should have abstracted something it's often too late to fix without rewriting a large chunk of your code and your teammates' code.
- doug_durham 2y agoTruth! Many developers believe the coming up with abstractions is the pinnacle of sw engineering. The amount of time and money wasted on arguing about the right abstractions, following by massive refactoring needed when you discover that reality doesn't match your abstractions is immense. I have had engineers abstract all of the way down to Turning machines in an effort to find a model that would fit all of their use cases.
- karmakurtisaani 2y agoI have encountered the following interface in a real productive code base: interface ILoadData<In, Out> { Out Get(In input); } So essentially this says that to implement this interface you must have a function that takes in something (you decide!) and outputs something (you decide!) and it's called Get. Totally useless.
- dragontamer 2y agoDonald Knuth wrote 4 textbooks in assembly language with advice about why decrement then jump saves 1 assembly instruction per loop iteration. The fact that everyone uses his premature optimization is the root of evil remark is hilarious to me. The level of optimization in Knuths writing makes it evident that upfront efficiency effort is worthwhile. Just don't overdo it. But don't trust me on that quote. Just open book 3 (chapter 5 and 6, sorting and searching) and go read the tape drive simulations of merge sort yourself. You can't miss the printouts, they're huge.
- zahlman 2y ago"book 3" isn't enough for me to identify the textbook you refer to. Could we have a title, perhaps?
- elpocko 2y agoThis is like asking for the title of the Bible after a psalm was quoted on a forum for religion. It's "The Art of Computer Programming", volume 3 (Sorting and searching) by Donald Knuth. HTH
- zahlman 2y agoThis is Hacker News, not Computer Scientist News. Knuth's work wasn't part of my programming curriculum in an engineering program, either.
- elpocko 2y ago>This is Hacker News, not Computer Scientist News. Or Basic Internet Search News.
- karmakurtisaani 2y agoExcuse me, but we are hackers, not some fancy elitist people who think for themselves and find out things on their own using some modern internet search tools.
- dr_dshiv 2y agoDonald Knuth: “premature optimization is the root of all evil” The article suggests that premature specialization and premature generalization might, instead, be the root of all evil. Really! Well-written.
- PaulHoule 2y agoWonder what he'd think about my very lispy chess engine I'm writing in Python. A player like that might look like opener(ender(mcts(ender(ply1(pieces+squares,temperature=25)) the goal is minimum code not through code golf but by design by composition.
- dade_ 2y agoThe team that wrote the Windows 11 file explorer are clearly scared to death of any optimization whatsoever.
- pron 2y agoI agree that premature generalisation is a problem, but to tell whether anything is premature, you must have all the information as the person who decided to generalise or optimise. Put simply, these are rules best applied to your own projects, not to others'. Often I see people criticise some project for being overly generalised or "over-engineered" simply because they don't have as full a picture as the project's maintainers.
- Terr_ 2y agoI feel like we can trace further back from premature optimization and premature generalization to: 1. A fear that it won't be possible to alter the program later based on better use-case information. (Sometimes justified, sometimes not.) 2. Excitement, engagement, and the ego-trip of Making A Cool Solution. Part of the reason I prefer languages with strong static checking is that it makes those kinds of (1) rewrites easier to do.
- tbrownaw 2y agoAren't all of these just variants of incorrect (often due to not being explicitly validated) assumptions?