Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If there is any article that I absolutely hate it is 'Duplication is far cheaper than the wrong abstraction'. A somewhat minimal abstraction has very little chance of being wrong. And if it is wrong, is it really that difficult to know? It has a chance of needing some improvement, but what code does not have a chance of needing some improvement? If one goes the full monty and introduces three design patterns every time that two lines are duplicated sometimes one will certainly end up with, perhaps, not so much the wrong abstraction as an overly convoluted one. This acticle is the excuse for programmers everywhere not to fix their messes. It is 100% opposite to what programmers need to hear.


> It is 100% opposite to what programmers need to hear.

Maybe you know very different programmers than I have known, but if I had to compare what has been a bigger source of problems—missed abstractions, or abstractions that make things more difficult for no benefit, it's definitely bad abstractions almost every time.


Well, I worked for quite some time in a code base that was basically one big ball of missed abstractions. Maybe I was traumatized a bit by that. I also have seen wrong abstractions, but not because there should not be an abstraction. If people had actually written abstractions that removed duplication instead of the ones that they half-understood from the design pattern book, they would have had come up with better ones. Duplication is in fact the best way to find the correct abstraction. The article makes this sound suspect but it really is not. The whole literature of design patterns is basically unneeded. If one just follows the path of deduplication one will discover all of them one by one and one will have applied the right one in the right place. Maybe there is some value in noticing that one still has to keep thinking and keep an eye on the likely future and not blindly do this but that is no more than a small footnote to the main message which is that you will find all the right abstractions by removing duplication. It is quite right that in TDD the refactor step is often described as the removal of duplication.


You have not worked on bad enough code base.

Duplication is a problem as the different implementations inevitably drift and get repeated bugs, but simple reduplication results in the rather known problem of RavioliCode() of thousands low cohesion functions. Which ends up unreadable thus bug prone and slow to develop.

Use of the right patterns or rather paradigms reduces amount of code in general, thus reducing duplication.

Wrong patterns are hard to actually change especially on change averse projects. The more widely used the wrong design is, the harder it is to change as the hacks on it multiply. Even worse if the wrong patterns (not code) are duplicated.

These require in depth rewrites to which bosses are usually allergic, which are very hard to pull off on bigger teams too. Incredibly hard to coordinate.

-- () https://wiki.c2.com/?RavioliCode - can happen in functional and structural code too.


'You have not worked on bad enough code base.'

You know, it is a bit pretentious to read a few sentences that someone wrote and then conclude a lot about what they know or do not know.

And I actually do know what ravioli code is. I think ravioli code is mostly a good thing. Also the people on the c2.com page are not uniformly negative about it. Not all code should be ravioli code but in a project with complex requirements there should be quite a bit of ravioli code. It is true that ravioli code is not easy to understand if you are a newcomer to a project but really if the context is 'a project with complex requirements' why would anyone think that it is easy to get into, no matter how it is written?

Another thing is that ravioli code absolutely needs automated tests.

'This style maximizes maintainability by old hands at the expense of comprehensibility by newcomers.' Maintainability is exactly what I want maximized. It sounds a bit bad if there are no developers that are there for a long time. But in that case you are cooked anyway, I would say.


Main problem with the style is lack of overarching structure and cohesion.

Preferable state is high internal cohesion and low coupling. Most code is opposite. Ravioli is when you trade high coupling without introducing cohesion, which is structure. Typical state after dumb refactor rather than rework.

Simple extraction of functions does not get you anything (if done well within a module, maybe increases cohesion), while making them reusable modules trades reduced cohesion for increased coupling, which is bad.

If you deduplicate too much you suddenly cannot change anything simply... As every place that got deduped is now coupled to one implementation. Once you need some special care, you get to replicate it again oftentimes.

Only true primitives are really worth it to not replicate and parts of code that won't change. (Ask the crystal ball again.)

See if deduplicaton gets you any of useful high level designs, like MVC, event-driven, reactive, message based. Without overarching design, you end up with a mass of locally useful ones that together are incompatible thus requiring lots of different, unique glue code.

The big mistake people do is to equate design patterns with code patterns. Which is what the silly GoF book did a lot. For example "fire and forget background parallel tasks" is a design pattern. Reactor (executes Strategies to deal with Events) is one while Singleton or Context are not. Event also is not. Etc. Generally anything that doesn't actually structure anything is a code pattern.


It is quite possible to get an MVC by deduplicating things. Image we have two tables that display some data by the same means, e.g., a web page. This will be about the same code twice. For instance, both will iterate over rows and columns. Removing that duplication will give you an M and a V. Then you may also have to react to some events in about the same way. Remove that duplication and you have a C.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: