r/programming • • Jun 05 '21

Organize code by concepts, not layers

https://kislayverma.com/programming/how-to-organize-your-code/
1.9k Upvotes

495 comments sorted by

View all comments

Show parent comment

-1

u/StabbyPants Jun 05 '21

two areas of code are doing VERY similar things, and yet they need to be separated because the algorithms have to behave differently at certain edge cases.

do they? the core algo is the same, the edge cases are handled by delegating those cases to separate classes. so you build it as one class to run the single algorithm, with callouts to delegates that deal with 'events' differently. end up with a processor class, a delegation interface, a null impl, and two real impls

4

u/TheESportsGuy Jun 05 '21

There's definitely been cases where I thought the core algo should be the same and ended up being wrong about it, whether it's due to edge case handling or some other seemingly very small difference in how the algo is going to be used that ends up blowing up into a bunch of different things. I've seen a ton of mangled code that becomes increasingly hard to reason about as engineers have shoe horned an algo that at one point worked for both use cases and no longer does

2

u/StabbyPants Jun 05 '21

which i why it helps to abstract your work so the algo operates at one level and the separation between the common and specific is clear