Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Apr 2011

Email Validation Validity

by in CodeSOD on

There are a lot of different ways to validate an email address input field. The easiest – and mostly correct – method is to use a regular expression. Two of the more complicated – and among the most incorrect – methods follow.

Jonasfound this in a custom-built web app at a customer site, and commented that it's emblematic of the quality of the whole codebase.


Units of Software

by in CodeSOD on

Marcus writes (via the Submit to The Daily WTF plug-in), "there are many days when I think that my company simply hired our development team because they were fast typists."

"It's not as if we're paid by the line of code, but our management team is run by a couple MBAs that love to quantify everything. And as we all know, nothing quantifies programmer productivity better than the 'units of software' delivered. And speaking of a unit of software, here's a valuable one put together by one of my more 'productive' colleagues.


The Nondeterministic Hash

by in CodeSOD on

The application that Sam F helps support acts as a sort of "parallel data entry" platform. Users from different parts of the world aggregate and enter all sorts of data from different sources. Duplicate data entry was fairly commonplace, but it was rarely a problem that running a few complicated scripts couldn't clean up later. And therein laid the problem: "later" sometimes meant days or weeks.

In an attempt to deliver more accurate data sooner, Sam's colleague was tasked with hashing the data upon entry and using it as a sort of "instant" comparison. The fix seemed to help: users were warned about duplicate data, and management was satisfied with the reduction in duplicate data.


A Char'd Enum

by in CodeSOD on

Ah yes, the enum. It's a convenient way to give an integer a discrete domain of values, without having to worry about constants. But you see, therein lies the problem. What happens if you don't want to use an integer? Perhaps you'd like to use a string? Or a datetime? Or a char?

If that were the case, some might say just make a class that acts similarly, or then you clearly don't want an enum. But others, such as Dan Holmes' colleague, go a different route. They make sure they can fit chars into enums.


The Qualities of Equality

by in CodeSOD on

With complex datatypes, objects references, internalization, and all the other intricacies that go into modern day software development, the matter of equality is often a bit more involved than simply ==. Or at least it can be made to be, as today's two examples demonstrate.

First up is Willy's specimen, who adds "I particularly enjoy understatement of the comments in the dateAreEquals() and equals() methods.


The Shortcut Guy

by in CodeSOD on

"My official title is Junior Developer," writes J Banic, "but I've come to become known as The Shortcut Guy. Why? Because I tend to know a lot of 'shortcuts' that the mid- and senior-level developers haven't yet discovered."

"To give you an idea at the type of 'shortcuts' I help them with, following is an instant message exchange between me ('jbanic') and a colleague ('mroot')."

mroot (10:03 AM): Got a problem for ya...
mroot (10:03 AM): Any idea how I can parse a string of email addresses like this: [email protected]@[email protected]?
mroot (10:04 AM): I need them separated in an array. I was thining of looking for .net, .com, etc.
jbanic (10:05 AM): That's not really feasible... you'd have to know *all* existing domains.
mroot: (10:05 AM): Is there a web service to do that?
jbanic (10:06 AM): Ehhh... no.
mroot: (11:18 AM): Okay. What if there was a comma between the addresses? For example, [email protected],[email protected],[email protected].
mroot: (11:19 AM): That should be easy to put in an array?
jbanic (11:22 AM): Waitasec... how did you add in the comma like that!?
mroot: (11:24 AM): I just copy/pasted them from debug console. That's what my string has.
jbanic (11:25 AM): Hmm... can you send me the code?
mroot: (11:25 AM): Sure, no problem...
mroot: (11:25 AM): Here it is.