Mark code duplication with a GUID

25 Mar 2020

It's always a trade-off when choosing between code duplication or adding a dependency. The conflict of redundancy vs dependencies.

As it says in The Pragmatic Programmer (one of the best programming books) on "DRY":

DRY (Don't Repeat Yourself) is almost a mantra for developers. But if applied indiscriminately, it leads to abstract code that’s hard to read & understand. It also stops different parts of the code to evolve to their full potential. Do not follow the DRY principle blindly. 

Sometimes it's an easy decision, e.g. when the duplication would be inside a single class, then making a new private method inside that class is almost always better than the duplication.

When the decision isn't so easy, e.g. because you'd need to add a new dependency across modules, then you need to think about it a bit more.

If you do decide that duplication is the right thing to do here, then you can mark the duplicated code with a GUID. Doing that means that it's easy to search for that GUID to find the other copies of that duplicated code. Making your life much easier later on when you come back to make another change.

// Duplicated VIP condition code 6b012ccc-2069-466c-8717-2c5999c96eb2
if(AccountIsVip(user))
{
    // ...
}

As well as making the duplication easy to find, it also makes it clear that the duplication was intentional, not accidental.

We should all try to avoid unnecessary duplication, but keep in mind that the wrong abstraction can be worse than duplication.

comments powered by Disqus