• kubica@fedia.io
    link
    fedilink
    arrow-up
    45
    ·
    3 days ago

    You could save about half the code by only listing one boolean value and having the other as a default return at the bottom.

    Sometimes my genius is almost frightening.

    • prime_number_314159@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      2 days ago

      If small numbers are much more frequent, it’s better to return early. Really, you should gather statistics about the numbers the function is called with, and put the most frequent ones at the top.

    • ExLisperA
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      3 days ago

      So to see what’s returned for 3 for example you would have to check the entire function to see if it’s not matched somewhere below and that if actually gets to the default return. This way the first time it matches 3 you can stop reading. It’s has better maintainability.