So I might just be a bit of an overzealous code nerd, but whenever I download a snippet of code, I always analyze it, and try to make sure it’s up to snuff. So it was no surprise that I did this when I was looking at an email newsletter plugin that I was thinking about using. The problem this time was I didn’t even get to the code before stopping.
- email@example.com,firstname.lastname@example.org, (Comma at the end)
- email@example.com,,firstname.lastname@example.org (Two comma)
Why is this bad? It’s called Normalization, people…
…use it. Being the devoted coder that I am, I managed to figure out the solution to this dilemma, in less characters than it took to type that instruction to the end user.
$email_list = str_replace(',,',',', trim($email_list, ','));
This uses trim to strip away any leading or trailing commas–the first incorrect format–and then uses str_replace to replace any commas that are doubled up into a single comma–the second incorrect format. Simple, efficient, and a single line of code. It can be run when you validate that you actually have a value in that field… I mean…you do validate that, correct?
Why is normalization so important?
To put it simply, the end user doesn’t always know what they’re doing. To make a truly usable product with a wide appeal, you need to normalize the input so you can reasonably predict what’s going to be there. In the example above, the developer ignores this aspect of development, and instead puts the responsibility of input sanitation on the end user. This isn’t good design, it’s also not good customer service.
The end user doesn’t care what’s entered into the box, as long as what comes out on the other side is correct. Good software design should be able to interpret user input in such a way that it can say, “Hey, you probably didn’t mean to do that, so let me fix that for you. It’s okay, no big deal, it’s what I’m here for.”