5 ms·
duplicate code is not that bad. reduce duplication over time as you find the common patterns/abstractions, instead of trying to build abstractions too early
by kevinqi 1y ago
duplicate code is not that bad. reduce duplication over time as you find the common patterns/abstractions, instead of trying to build abstractions too early
- gwlperl 1y agoYes.
- ipnon 1y agoDuplicate or near-redundant functions are either no problem or even useful. But duplicate or near-redundant data structures are a plague on any codebase. The prophet Perlis declared: "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures."
- kevinqi 1y agointeresting, I didn't know that corollary. sounds about right though.
- aitchnyu 1y agoHave you set up a copy paste detector that fails the build if you write too many duplicates?
- socalgal2 1y agoMaybe obvious but also, don't deduplicate if it means adding options or making things too abstract. I don't know if I can think of a good example. Maybe you make a function that compares arrays of strings. Then you decide to add an option to make it case insensitive. Then someone comes along and decides they want to compare arrays of structs. So they had a comparison function option. Most times I've seen this pattern the code gets way harder to understand and more brittle. All the options etc cascade into spaghetti and changes start breaking things because so many paths expect some original behavior. Better to have just kept them separate, most of the time.
- roncesvalles 1y agoMost of the shitty code I've seen has been from poor attempts at reducing code duplication, usually resulting in some overfitted generalized solution or abstraction. Efforts to reduce duplication should be focused on chunking out reusable pure functions. Trying to dedupe the flow of logic is almost always a fool's errand.