Details
-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Assign this magic number X to a well-named (variable|constant), and use the (variable|constant) instead.
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Legacy Key:MagicNumber, COBOL.MagicNumberCheck
-
Targeted languages:Flex, PHP, Python, RPG, Swift, VB.Net
-
Covered Languages:ABAP, C#, C, C++, Cobol, Java, JavaScript, Objective-C, PL/SQL, TypeScript, VB6
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Scope:Main Sources
-
Checkstyle:MagicNumber
-
ESLint:no-magic-numbers
-
PMD:AvoidLiteralsInIfCondition
Description
A magic number is a number that comes out of nowhere, and is directly used in a statement. Magic numbers are often used, for instance to limit the number of iterations of a loop, to test the value of a property, etc.
Using magic numbers may seem obvious and straightforward when you're writing a piece of code, but they are much less obvious and straightforward at debugging time.
That is why magic numbers must be demystified by first being assigned to clearly named variables before being used.
-1, 0 and 1 are not considered magic numbers.
Attachments
Issue Links
- is duplicated by
-
RSPEC-1977 Named constants should be used for indicators
- Closed
- is implemented by
-
SONARJAVA-1388 Rule S109 (MagicNumber): Rule should not raise issue with fields from interfaces
-
- Closed
-
-
SONARSWIFT-343 Rule: Magic numbers should not be used
-
- Open
-
-
CPP-299 Rule: Magic number should not be spreaded everywhere in the source code
-
- Closed
-
-
SONARABAP-63 Rule : Magic numbers should not be used
-
- Closed
-
-
SONARVBSIX-163 Rule: Magic numbers should not be used
-
- Closed
-
-
CPP-707 Create Objective-C rules repository containing targeted rules
-
- Closed
-
-
SONARABAP-252 Magic Number rule should work on chained statements.
-
- Open
-
-
SONARJAVA-1079 S109 - Magic Numbers: add an "authorized numbers" parameter
-
- Closed
-
-
SONARJAVA-1235 S109 should not consider as magic number final (but not static) fields with initialization
-
- Closed
-
- is related to
-
CPP-2748 Rule S6164: Mathematical constants should not be hardcoded
-
- Closed
-
-
RSPEC-2480 "FOR" loop end conditions should not be hard-coded
- Active
-
RSPEC-1583 Magic literals should not be used
- Active
- relates to
-
RSPEC-6164 Mathematical constants should not be hardcoded
- Active
-
SONARCOBOL-997 Remove useless rule "Magic Literal"
-
- Closed
-
1.
|
RPG - Named constants should be used for indicators | RSPEC-2049 |
|
Active | Unassigned | |
2.
|
C-Family | RSPEC-2904 |
|
Active | Unassigned | |
3.
|
VB.NET | RSPEC-3103 |
|
Active | Unassigned | |
4.
|
Java | RSPEC-3161 |
|
Active | Unassigned | |
5.
|
JavaScript | RSPEC-3162 |
|
Active | Unassigned | |
6.
|
VB6 | RSPEC-3163 |
|
Active | Unassigned | |
7.
|
ABAP | RSPEC-3164 |
|
Active | Unassigned | |
8.
|
Cobol | RSPEC-3569 |
|
Active | Unassigned | |
9.
|
PL/SQL | RSPEC-3570 |
|
Active | Unassigned | |
10.
|
C# | RSPEC-3572 |
|
Active | Unassigned |