you are viewing a single comment's thread
view the rest of the comments
[–] 24 points 2 years ago (1 child)

Good job my young padawan, let me teach you about the modulo operator ...

  • source
  • hideshow 2 child comments
  • [–] 0 points 2 years ago (3 children)

    Actually the modulo operator is the wrong solution.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 27 points 2 years ago* (2 children)

    No its not the wrong solution! Premature optimization is a waste of time.

    Using if or case are not a solution because they are way too verbose and very easy to introduce an error.

    Modulo is a solution, and using bit-wise and is another faster solution.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 5 points 2 years ago

    It's only the wrong solution if you're writing something where every operation needs to be accounted for. Modulo is a great, easy, readable method otherwise.

    Not too certain on C++, but I think this would be the cleanest implementation that still somewhat optimizes itself:

    private bool IsEven(int number){
        return !(number % 2)
    }
    
  • source
  • parent
  • [+] -10 points 2 years ago (2 children)

    You call it premature optimization. I call it obvious.

    You use a flat head as a Phillip's.

  • source
  • parent
  • hideshow 4 child comments
  • [–] 17 points 2 years ago (2 children)

    You can call it whatever you like, the fact of the matter remains - code readibility is more important than most optimizations you can ever hope to make.

    Bad programmers optimize everything and produce code that is not understandabe and 0.001% "faster"

  • source
  • parent
  • hideshow 4 child comments
  • [–] 2 points 2 years ago

    Optimization isn't inherently better than Readable code.

    What's most important is what matters to your project.

    If your project manages itself with limited resources, Optimization is better than Readable code, and it should be supplemented with comments.

    If your project has a wealth of resources, readable code is probably the better option.

    The "This is the ONLY WAY" mindset in coding is the only thing that I would argue is completely wrong. The "One-size fits all" mindset in development is short sighted, sure. but what makes it the worst, is when it becomes "One-Size fits all ONLY".

    "One-size fits all" means the project runs well on everything. "Optimized for one" means the project runs exceptionally on one Architecture/OS.

    Basically, this situation is the highly unsatisfying "It's your preference".

    But given the context of Yandere Dev and the Target Audience of Yandere Simulator, his code is perfect. It's code that runs terribly on everything and the project is for no one.

  • source
  • parent
  • [–] 7 points 2 years ago (1 child)

    I call it making assumptions that may be incorrect, and do you know if the compiler will do the optimization anyway in this case?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 0 points 2 years ago* (3 children)

    What statement do you flag as assumption? Yes, I do. The modulo operator is only a subset of bit masks. It is more explicit to write:

    if ( (variable &EVEN_MASK) == 0) ...

    To act upon even numbers then:

    if ( (variable %2) == 0) ...

    How would you name the 2 in the above statement for more expressive power?

    EVEN_MODULO_OP ? That may throw more people off imo.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 1 point 2 years ago (1 child)

    I give up, I was wrong to even think about the modulo operator, you are clearly the master programmer. 🥇

    This reminds me of a discussion about the ternary operator ? :, some people think its the one true way of writing code because its just so clear what it does. And I say please use it sparingly because if you start doing nested ternary operators its very hard to unpack what your code does, and I prefer readability over compact code, especially with today's compilers.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 6 points 2 years ago (1 child)
  • [–] 3 points 2 years ago (3 children)

    Not neccessarily wrong, but you could also check the first bit. If it's 1 the number is uneven, if it's 0 the number is even. That seems to be more efficient.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 5 points 2 years ago (1 child)

    That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 2 years ago (1 child)
  • [–] 2 points 2 years ago

    That’s the main issue with premature optimization: do it the “optimized” way and it may still be inefficient, or do it the obvious way and let the compiler turn it into its most optimized form. (Of course, not the case with all languages, but most mainstream compilers optimize the code to a decent extent.)

  • source
  • parent