• Lifter@discuss.tchncs.de
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 day ago

    Regarding the “bad code”. It’s more readable though to keep the full limit for each elif case, which is most often way more important than performance, especially since than logic with the age can be easily optimized by any good compiler or runtime.

    • BassTurd@lemmy.world
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      14 hours ago

      Code readability is important, but in this case I find it less readable. In every language I’ve studied, it’s always taught to imply the previous condition, and often times I hear or read that explicitly stated. When someone writes code that does things differently than the expectation, it can make it more confusing to read. It took me longer to interpret what was happening because what is written breaks from the norm.

      Past readability, this code is now more difficult to maintain. If you want to change one of the age ranges, the code has to be updated in two places rather than one. The changes aren’t difficult, but it would be easy to miss since this isn’t how elif should be written.

      Lastly, this block of code is now half as efficient. It takes twice as many compares to evaluate the condition. This isn’t a complicated block of code, so it’s negligible, but if this same practice were used in something like a game engine where that block loops continuously, the small inefficiencies can compound.