-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
no_magic_number - Ignore numbers involved in bit-wise operations (like << or >> or | or &) #5171
Comments
So I think excluding Do we need to worry about the other bitwise operators? I think |
I'd agree that |
@SimplyDanny - Very much agree that the most common use case if for bit shifting (<<). It does feel weird to declare a variable something like "let the4thBit = 8" or "let allBitsFor8Bit = 255". But, I'm also seeing that binary notation (0b11111111) apparently does NOT trigger the no_magic_number, so that's probably a reasonable way to handle the bit-wise & or |. |
@mildm8nnered - Thanks for getting out a PR so quickly! Any reason you think we shouldn't do the right shift (>>) also? I'm assuming @SimplyDanny mentioned left shift "<<" but that he meant left or right shift are both totally okay...?? (but...maybe I misunderstood). -Thanks! |
Actually, looking through my codebase, I have some |
Thanks, @mildm8nnered! |
Could you list a few (obfuscated) examples here, @mildm8nnered? I try to understand if the I understand that the rule is annoying for the |
|
I think the above example will actually trigger on the |
This could be an example for the "bare computations" I mentioned above. I wonder if this is really so common that it justifies the rule to ignore it completely. In the example, one could also comfortably live with a Right now, I see a few possibilities to let the rule handle magic operands of bit-wise operators by some means:
I'm not yet sure what's the right way to go. |
Probably obvious... But my vote would be for option 1 or 2... I feel like the heart or idea behind not having magic numbers is that you don't want to be reading through code and just see a "42" somewhere, with no justification for what it's doing or why. But (at least for me), shift operations tend to be a block of code where it's obvious what is going on (usually setting up values for an enum, or performing an encoding/decoding type of operation. So, even if someone says that these are just "magic numbers", I think the heart of the rule is still preserved by make these changes. -Thanks! |
Makes sense. I think you convinced me. 😅 In case of complaints, we may hide this behavior behind an option later. |
New Issue Checklist
New rule request
(I debated if this is technically a bug or request, but it's definitely not a "new" rule, more of just improving an existing rule.)
What I'm running into is the "no_magic_number" rule triggering on bitwise structs or enums that we have in our code -
Obviously, the bit-wise left shift operators appear to have "magic numbers", but being part of a left (or right shift) operation kind of excludes it from being "magic".
To make it helpful, could enforce that the number is less than 32 (or 64) (or whatever number of bits are in that specified storage type), depending on how smart the rule can be.
An additional idea would be to just make it a configuration point on the no_magic_number rule. Along the lines of "ignore_bitwise_operations: true"
Hopefully that makes sense. As you can see from the code sample...it's not the hardest thing in the world to just disable the rule for the lines where it applies. But it makes the code messier than it needs to be for cases that (I think) are universally okay for having "magic numbers" (as long as they are less than 64).
The text was updated successfully, but these errors were encountered: