There should just be one source of truth for any logic or process. If you duplicate that then bad things will eventually happen.
There should just be one source of truth for any logic or process. If you duplicate that then bad things will eventually happen.
It's easy to shoot yourself in the foot with that. I spent a few years working with binary formats, where you have lots of constants (byte offsets, flags, etc).
There were two approaches to dealing with this:
1) Some people just put the raw numbers in the code, and if it wasn't clear added a comment
2) Other people used constants for everything, defined somewhere else, often through multiple layers of abstraction.
If you follow DRY, then you should always chose 2). But in my experience, this often makes the code extremely hard to read. You often have to look through a dozen header files to find the constants used in a 5 line bit of code, and it becomes really hard to reason about the code.
And in the end you almost always still need some duplication, because you have other files where you can't use the constants (eg. in sample files, docs, external libraries,etc.)
X = 5; // File header offset
vs. X = FILE_HEADER_OFFSET;
I don't think there's much value in the idea that just because someone can implement something badly, it validates the whole idea. You should always use constants instead of raw values if they have some meaning. If someone, somehow, manages to make that so complicated that it's a pain it's still not the fault of the concept.> And in the end you almost always still need some duplication, because you have other files where you can't use the constants
And those will invariably end up out of date and incorrect at some point.
But still your describing a bit of a wild setup. If the value of FILE_HEADER_OFFSET is 42, how did you come to calculating the value? What if MEMORY_BLOB_OFFSET is also coincidentally 42? If you have dozens of source files filled with literal magic values that's completely unmaintainable and inscrutable.
A constant doesn't have to be in a separate file if it's only used in one place.