| I've said it before and I'll say it again - I should encapsulate it as a law. BeetleB's Law of DRY: Every article that complains about DRY will be a strawman argument. The DRY acronym came from The Pragmatic Programmer, and almost every instance of DRY people complain about is not at all what is advocated in the book. There are different ways of interpreting what he wrote, but my version is: "If you have two separate requirements that are very similar, keep them separate in code. If your duplicated code is one requirement, then DRY it into one location in your code." So this: > Following “Don’t Repeat Yourself” might lead you to a function with four boolean flags, and a matrix of behaviours to carefully navigate when changing the code. Is not DRY. In fact, having boolean arguments is almost a guarantee that you've violated DRY. Another way to know you've violated DRY: If one requirement changes, do I need to add if conditions to the DRY'd function to ensure some other requirement doesn't break? If yes, you're in violation. Never tie in multiple requirements into one function. Or rather, do it but don't call it an application of DRY. |