-
Notifications
You must be signed in to change notification settings - Fork 67
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
Magic numbers rule? #3296
Comments
currently no rule for that |
@DGRHicron the examples are when the values are used in compare conditions? how about passing a value to a method, are these also considered magic? |
I found a piece in Clean Abap just about comparisons and naming constants meaningful. But if you ask me, any hardcode is bad. Where used is very powerful and useful function. I hate scanning code by rs_abap_source_scan to find if a "constant" is used as hardcode. Of course this rule should have the possibility to disable it for test classes. |
Ok. I see that I am mixing two rules. This example for me is ok. I tried to come up with a counter-example as the wrong one, but couldn't find one that was convincing enough. The second rule is for checking hardcodes of text.
|
I'm not convinced a rule is necessary, or would be very difficult to get right as it can be subjective and context-dependent. I also don't believe hardcoding is bad. We used to go by the principle of wrapping everything in constants, but with refactoring tools this is not necessary. A clear single-use value doesn't need a constant.
|
@pokrakam, I assumed that abaplint's origin is somehow connected with Clean Abap guide. That's why I opened this issue. Here is the link to the constants section. https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#constants |
abaplint pre-dates SAP's Clean ABAP guide, abapOpenChecks pre-dates abaplint |
@DGRHicron I am not contradicting Clean ABAP. A Magic Number is a literal token (not necessarily a number) where it is not obvious why that value is used or where it comes from, it appears derived by magic. The Clean Code book also explains that constants are not needed if it's clear: Some constants are so easy to recognize that they don’t always need a named constant to hide behind so long as they are used in conjunction with very self-explanatory code. For example: Martin, Robert C.. The Robert C. Martin Clean Code Collection (Collection) (Robert C. Martin Series) (p. 435). Pearson Education. Kindle Edition. A method name can also be a valid way of describing a literal. METHOD get_root_node.
result = nodes[ id = '0000' ].
ENDMETHOD. All of which brings me to the opinion that it would be very difficult for abaplint to handle magic numbers properly and I'd probably just disable such a rule. |
It is implemented in code_pal for only few cases: |
I can't find a rule which checks if one uses hardcodes such as magic number comparisons.
For example:
Here value 2 is magic. Noone knows what it is. Is there a rule for that?
another example:
IF moo = 'SOME_TEXT'
The text was updated successfully, but these errors were encountered: