Code Smell 41 - Regular Expression Abusers
Maxi Contieri
Posted on December 3, 2020
RegEx are a wonderful tool, we should to use them carefully and not to look smart.
Problems
Readability
Maintainability
Testability
Intention Revealing
Solutions
Use regular expression just for string validation.
If you need to manipulate objects, don't make them strings.
Sample Code
Wrong
Right
Detection
Regular expressions are a valid tool.
There's not much automated way of checking for possible abusers. A whitelist might be of help.
Tags
Primitive Obsession
Abusers
Conclusion
Regular expressions are a great tool for string validation. We must use them in a declarative way and just for strings.
Names are very important to understand pattern meanings.
If we need to manipulate objects or hierarchies, we should do it in an object way.
Unless we have a conclusive benchmark of impressive performance improvement.
Relations
Code Smell 20 - Premature Optimization
Maxi Contieri ・ Nov 8 '20
More info
Credits
Photo by John Jennings on Unsplash
A Perl program is correct if it gets the job done before your boss fires you.
Larry Wall
Posted on December 3, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.