Wednesday, May 23, 2007

Bullet Proof code using RegExp.

Regular expressions is a great way to find those hard to find strings.
Suppose you have a bunch of old code and you want to bullet proof it or you might be interested in auditing it and for starters you want to find those uninitialized variables using eclipse.

the pattern of uninitialized variable could be in the following format

(Var)(space or more)(Alphanumeric word)(Possible space or more)(semi-colon)

to search for (space or more) we use expression
\s means space
+ means one or more

to search for(Alphanumeric word) we use expression
\w means Alphanumeric character
+ means one or more

to search for (Possible space or more) we use expression
\s means space
* means zero or more

so the regular expression would be int(\s+\w+\s*); it will return all uninitialized ints.

It would be great if we collect those regular expressions and keep them in a library to be our arsenal towards bad code.

do u have more regular expressions to share?


Seba3y said...

actually this was very helpful some time ago.

Hamdy K. said...

I use this

and my current petite arsenal
=(\s+\n) (No line breaks after operator)
(\w+)(\s+\s)(\w+) (No 2 spaces between words)
(\s+); (No Spaces before semicolon)

Mahmoud Al Gammal said...

Speaking of bullet-proofing code, I recommend this article: . A very interesting read.

M. Shalaby said...

I tried the plugin you use (RegEx tester for eclipse) and it's really great.