CodeSOD: Keeping Regular
Regular expressions can create new problems. Like an industrial drill, they're extremely powerful- and potentially destructive. They're expensive, they're cryptic, but can be extremely concise.
For example, Jessica is reimplementing some C# code written by another developer. This developer was never interested in being concise, and instead favored being "clever". For example, this developer had an array of strings, and needed to remove any non-word-characters from each string.
A regex-based solution might include something like "[^\w]" which is obviously incomprehensible nonsense that no one could be expected to understand without piles and piles of documentation.
Jessica's co-worker wrote something far more" elegant.
for (int i = 0; i <= name.Length - 1; i++) { String sTemp = ""; for (int j = 0; j <= name[i].Length - 1; j++) { if ("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789_".Contains(name[i][j])) { sTemp += name[i][j]; } } name[i] = sTemp;}
Here's a fun question. Given this input array (Malcolm,Ermelinda,Annika,Jesusa,Honey,Romelia,Dorene,Alvaro,Charmaine,Georgann,Troy), how many String instances does this code construct and throw away for concatenation?
Jessica's co-worker had a problem, so he avoided regexes. He still has problems.