MHN
Add a comment on Hacker News →
3 of 354 comments
  1. Too many abstractions are bad. Too many code duplication is bad.

    Part of being a good engineer is finding the right balance.

    I know engineers who would gladly duplicate code all over the code base to avoid creating a new abstraction.

    I know engineers who create polymorphic abstractions for a single caller with a very obvious set of parameters.

    So much of wisdom is in finding balance and not being dogmatic about rules.

  2. I believe that "single source of truth" is a principle that should always be followed. If there's duplicated code where it'd be a bug if they diverge, then you should refactor. It creates a long-distance coupling in your code that may be invisible to future developers until a bug emerges.

    But with that in mind, I mostly agree with the article: if it's not a violation of "single source of truth", then abstractions are just a convenience. If it starts being inconvenient, then it's not doing its job and there's no reason to use it. It's a serious code smell if a function needs several flags for custom behavior; that means it's probably the wrong abstraction or violating the single responsibility principle. If there is a legit need for lots of customization, an often-good way to handle is to take a function/functor as an argument for the customization. E.g., rather than `solve(f:double -> double, max_iters = 99, x_abs_tol = 1e-15, x_rel_tol = 1e-15, ...)` you can do `solve(f:double -> double, stopping_criteria: StoppingCriteriaClass)`

  3. I certainly learned this the hard way.

    When I started writing code 40 years ago, I used to over estimate my understanding and abstraction skills a lot. As a result I created overly complicated and difficult to maintain/evolve solutions.

    Turns out I need to see more examples of patterns before making good choices, which means becoming comfortable with seeing and tracking duplication over time.