Article 13CNC CodeSOD: Keeping Regular

CodeSOD: Keeping Regular

by
Remy Porter
from The Daily WTF on (#13CNC)

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.

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zAchU7x3zCHDI
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments