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:
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”
You shouldn’t rename 2 at all. “Even” has a commonly understood meaning that is instantly recognizable from (variable %2) == 0. The bitmask is an overgeneralization.
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.
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.
Good job my young padawan, let me teach you about the modulo operator …
Actually the modulo operator is the wrong solution.
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.
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) }
You call it premature optimization. I call it obvious.
You use a flat head as a Phillip’s.
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”
Okay wow no need to get personal.
I call it making assumptions that may be incorrect, and do you know if the compiler will do the optimization anyway in this case?
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.
You shouldn’t rename 2 at all. “Even” has a commonly understood meaning that is instantly recognizable from
(variable %2) == 0
. The bitmask is an overgeneralization.Psh screw those people, they need to see my massive coder wang
if(!i&1){ ... }
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.I feel you said about JavaScript as a whole.
Wrong means that it doesn’t produce the right output.
How is the modulo operator the wrong solution?
You are right. I have been biased.
https://realpython.com/python-modulo-operator/#how-to-check-if-a-number-is-even-or-odd
I just wonder why module is the wrong solution.
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.
That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically.
Modern compilers and interpreters are smart enough to figure out what you’re trying to do and automatically do that for you.