Search for a regular expression for identifying hardcoded magic numbers in source code

A common problem in code reviews is whether a numeric value should be hardcoded in the code or not. Does anyone know of a good regex that can catch magic numbers in code, for example:

int overDue = 30; Money fee = new Money(5.25D); 

without also receiving tons of false positives, for example, for a loop initialization code?

 for (int i = 0; i < array.length; i++) { } 
+2
java c # regex code-review
Dec 03 '08 at 15:07
source share
5 answers

The best question is to ask which tools do this. And the answer will be as follows:

  • Checkstyle
  • Fxcop

And many more tools for analyzing static code.

+12
Dec 03 '08 at 15:36
source share

Besides using a pre-built code analysis tool, a general approach is to search for all numbers outside a certain range. For example, the whole number is greater than 5 and below -5. You will find that this eliminates most of the false positives. If you want to be more aggressive, you can use 3 instead of 5, but you will get more false positives ...

+1
Dec 03 '08 at 15:35
source share

For Java, I would get FindBugs and then write a custom error detector to perform this check. For more information on writing a custom error detector, see this link .

0
Dec 03 '08 at 18:58
source share

Here's a simple regex that I use to scan magic numbers in a large PHP project:

[^ '"\ w] - [1-9] \ d * [^'" \ w]

This will include any number! = 0 that is not surrounded by single or double quotes or letters. Customize for your needs as you wish.

0
Jul 26 '09 at 1:33
source share

SD source code is an interactive tool for finding source code for many languages ​​(C, C ++, C #, Java, PHP, COBOL, FORTRAN, Python, ...). He understands the lexical syntax of each language at the same level of detail as the corresponding language compiler, so he knows and can easily distinguish between keywords, identifiers, numbers, operators, punctuation marks and spaces.

The search engine can be asked in terms of these entities and restrictions on their values ​​and will search for the code for all matches, display matches, and then allow you to check the source code for each match with one click. Since he understands the lexical syntax, he is not tricked by comments, spaces or content.

For example, you can find all identifiers containing the letters TAX by writing a search with identifier (I) with open icons:

 I=*TAX* 

You can find all numbers in a file greater than 50 and less than 72:

 N>50<72 

and he will find them regardless of the radix or syntax, since he knows the langauge syntax.

You can find all for loops with an upper bound of 50 or more:

 'for' ... I '<' N>50 

If you want to just find all the constants in conde, just write an unconditional search for numbers:

N

The logger can record all calls to the XML file for later processing if you wish.

0
Mar 09
source share



All Articles