The DRY Principle: Benefits and Costs with Examples

The DRY Principle: Benefits and Costs with Examples

Once upon a time, a fearful young developer (me), wanted to write magnificent code. I was seeing generations of developers speaking about it, as we speak about pyramids two thousands years after their construction. I wanted to let my mark in the world!

[…]

I understood that principles should be used depending on the context.

[…]

Therefore, the logic of this shipment should only appear once in the application.

[…]

Another quick example: imagine you coded a fancy class to parse B-trees. This can be considered as well as knowledge: it’s an algorithm which should be defined once. The representation of that knowledge should be used everywhere without repeating the knowledge itself.

So DRY is all about knowledge? All about business logic?

[…]

Dave, your colleague developer, thinks it does. Seeing this code you wrote, he comes at your desk and screams:

[…]

Dave, full of himself, claims: “You little pig! This code is not DRY!”. And you, aware of what the DRY principle is really about, answer: “But the business logic, the knowledge, is still not repeated!”.

[…]

Code reuse and code duplication are two different things. DRY states that you shouldn’t duplicate knowledge, not that you should code to be able to reuse everything.

[…]