4 ms·
No, they're not saying it's wrong, but rather it's commonly misunderstood. From the motto alone you might think the point is to abjure all code duplication. But
by crispinb 7y ago
No, they're not saying it's wrong, but rather it's commonly misunderstood. From the motto alone you might think the point is to abjure all code duplication. But Hunt & Thomas' intent was instead to warn against duplicating sources of truth/knowledge - that is, all knowledge embedded in your code needs to have a canonical source, and all other references should derive from that source or you risk divergence (or in the best case must always remember to make necessary changes in multiple places).
So for example, documentation (truths about the code) should derive from the code (eg by doc generation). Otherwise the docs & code will drift apart. Or if you're passing domain information across the wire between client & server, you should derive the data structures at both ends from a common source.
- iSnow 7y ago>all knowledge embedded in your code needs to have a canonical source I don't get it. Code /IS/ knowledge and whenever I copy-paste code around, I duplicate not only code but also knowledge.
- crispinb 7y agoWell (and I'm elucidating, not necessarily defending) they mean knowledge somewhat specific to the project, not in an absolute philosophical sense. So you have an API that belongs to this project. When you change it, do so in one place, and then run your doc generator rather than change it in both function/method signatures and docs. Or you have domain knowledge embedded in classes, and a wire protocol between peers or client & servers using different languages. Derive the data structures in the two different languages from common source (either one of the languages, or both from metadata). I think the distinction between this kind of project-specific 'knowledge' and more abstract 'everything is knowledge' issues is clear enough in practice. But it is just a rule of thumb rather than a deep philosophical principle, and like all such will break down in individual cases. whenever I copy-paste code around But that's just one source of code duplication. Another might be (for example) duplicated code deriving from code generation. DRY might advocate this (as there's a clear canonical source of knowledge), whereas a generic rule against all 'duplication' wouldn't.
- godshatter 7y agoIt's true that code is knowledge, but I think it's talking more about not having slightly different versions of that knowledge floating around the same code base. For example, if there is one canonical way to do something, such as adding a table row and having to remember to update counts somewhere else, then that should be done in one place. That way, if that agreed-upon way changes you don't have to remember that it's done in 25 other places across your code base.
- dllthomas 7y agoSometimes, things happen to be the same now, but they got there for different reasons and are likely to evolve differently in the future. In that case, there is not a single piece of knowledge being duplicated, but rather two separate pieces of knowledge being possibly unified.