CodeSOD: Keeping Regular |
Regular expressions can create new problems. Like an industrial drill, theyre extremely powerful- and potentially destructive. Theyre expensive, theyre 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.
Jessicas 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;
}
Heres 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?
Jessicas co-worker had a problem, so he avoided regexes. He still has problems.
[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!
| Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |