← Back to context

Comment by saltcured

5 hours ago

Written exactly like that, yours is obviously cleaner.

But, if that original code had comments and traceability of each condition and return to a specific domain scenario, you would be doing a disservice by collapsing it to the one flat boolean expression. In that case, it may be better in its expanded form, and you should let an optimizing compiler do the collapsing.

There were no comments.

If there were comments for each conditional, it should still be refactored as

  return a || b // comment 1
    || c // comment 2
    // long comment 3
    // on multiple lines
    || d;

Many years ago, "lines of code" was the classic example of nonsense management metrics. Today, there are somehow HN users who argue that lines of code is indeed a good metric and ask "But what if the code had comments?" as if they have never seen comments interleaved with code.

> In that case, it may be better in its expanded form, and you should let an optimizing compiler do the collapsing.

This is nonsense. This optimization is not about compiler optimization for efficiency. It's an optimization for human readability and maintainability.

  • FWIW, I'm no advocate of "lines of code", or really any KPIs at all. All I'm saying is context matters.

    Such branches could make sense if the conditions have to do with underlying domain concepts, but you expect the outcomes to be revised. It could just be a moment-in-time accident that they are all returning True right now.

    This kind of tension is also where you often see indirection via configuration files or other auxiliary data structures. Or in the old days, things like bit fields instead of booleans, so that merging the conditions would encode different small integers to use as lookup table indices.