DEV Community

Discussion on: My Misconception when Learning Web Dev

Collapse
 
ashleyjsheridan profile image
Ashley Sheridan

You're lucky, the interviewer was completely wrong. We should never be using regular expressions to validate email addresses. The format for an email address is so complicated, that even the most profienct regex is only 99% accurate and is absolutely not the sort of thing I'd expect any developer to be able to write out in an interview (if you're interested, they have expressions for most of the common languages at emailregex.com/ )

Just some of the many assumptions that people tend to make about emails:

  • They're only a max of about 50 characters
  • They can only have one asperand (@)
  • They only use ASCII
  • The domain only uses ASCII
  • The local part can only contain letters, numbers, and basic punctuation

The absolute best way to test if an email address is valid is to send an email to it. Even if you validate that an email address is formatted correctly, there's no guarantee the user didn't mis-type it in the first place.

Collapse
 
jfsoft profile image
Jonatan R. Fischer • Edited

Dude, you are checking valid email adresses, not user ability to type, that's checked when you confirm your email. Finally, 99.99% != 99% (edit: it seems that perl/ruby developers are regex purists)

Collapse
 
ashleyjsheridan profile image
Ashley Sheridan

Did you look at those regular expressions? Nobody, not even a senior, should be expected to rattle that off in an interview.

Besides, a regex like that in any codebase is a sign of something wrong. Code should be human readable. Obviously regular expressions fly in the face of that, but there's surely a limit to how far one goes with them in production-worthy code?

Thread Thread
 
jfsoft profile image
Jonatan R. Fischer • Edited

But you can make it human readable when wrapping it using a function, once the regex is tested in isolation, it can be used in production code without problems. I'm not auditing python regex engine each time I want to use the re module, and still that module is used in production code. If you restrict the solution space of a problem to what you can read and understand at the lowest level possible, maybe software would took ages to get into production... Of course, you can't solve every problem in sw using regexes, sometimes a simple looping solution solves the problem well enough to move on in development.

Thread Thread
 
ashleyjsheridan profile image
Ashley Sheridan

But there's still no way anyone would be able to write anything close to it in an interview.

Thread Thread
 
jfsoft profile image
Jonatan R. Fischer

I agree, and if you find yourself having to write that nonsense perl regex to get a job, it will be better for your mental health not getting it.

Thread Thread
 
tanami profile image
Tanami

I had this funny experience once when I was applying for a Perl job and they asked me to analyse a 300 line sort routine... I guess that's their version of the regex