Category Archives: Programming

What is a Valid Email Address?

I’m working on a side project and needed to validate if an email address format was valid.   I started doing searches and found a very interesting article by Ross Kendall.   His javascript implementation strictly implements RFC822, which although out of date [RFC2821 & RFC2822] is better than most alternatives that I came across.

My biggest issue with his script was that it didn’t require the domain to be at least two parts.  This meets spec and is certainly not a bug, however for all practical purposes online it would allow unsuitable email addresses.  I know just enough about regular expressions to realize it was a simple fix to make multiple domain parts required.  So I made the following modifications

I added an optional requiredFQDN parameter that’s intended to be a boolean value.

function isRFC822ValidEmail(sEmail, requireFQDN) {
   if(typeof(requireFQDN) == “undefined”)
      requireFQDN = false;

And then I changed the 0 to many identifier, * to the 1 to many identifier +.

if(requireFQDN)
    sDomain = sDomain.substr(0,sDomain.length-1) + ‘+';

You can download the entire file here.

Improve Code Quality by Disabling Copy and Paste

Integrated Development Environments (IDEs) should ship with Copy and Paste disabled by default in an effort to improve code quality. I acknowledge this is an extreme position that will not be popular by the majority of those who happen to read it.

Reasons why Copy and Paste should be disabled by default.

  1. It promotes what I have to believe is the most frequent code smell; duplicated code.

    Recently I was upgrading some commercial libraries and it was found to cause problems in numerous areas of the application. This is par for the course whenever you wait two years to upgrade libraries. I was appalled to learn that nearly character for character code had been repeated no less than 5 times within a single file. To better put this into perspective, the source file was 1800 physical lines when I checked it out and 1100 lines when I checked it back in; nearly a 40% reduction.

    I imagine the development went something like this. The programmer started by making the initial segment of code and got that working to their satisfaction. Once it was finished they went down to the next set of requirements and found that it was nearly identical, but with a few variations to the code they had just written. They took the code from before, duplicated it, and then modified it slightly to fulfill the second requirement. This repeated 5 or more times and allowed the author to finish their code quickly, which naturally makes most programmer’s bosses happy.

    I believe it’s natural for all people to want to do the least amount of work possible to accomplish a given goal. I jokingly call this being lazy and genuinely believe it’s a positive thing for a wide variety of careers; software developers included.

    The problem here is that it is easier to copy, paste, and then alter the already working code than it is to break it out into reusable functions, classes or extension methods. Imagine for a second that Copy has been disabled.

    It’s clear the author already recognized that the problems were similar enough to use the previous code as a starting point. Since they would be unable to duplicate the code via copy and paste it would be advantageous to them, at that very moment, to move that code into a common area.

    This is the main reason why disabling copy and paste in IDEs would encourage better quality code. It eliminates the requirement to have foresight to the future maintainability of the code or a desire to produce high quality code and transitions it to being the easiest way to do it right now.

  2. It would promote writing reusable code from the very first time you start to program.

    I think anyone who has been programming for any length of time can remember a time where design decisions they made have come back to bite them hard. This is frequently how I learn new programming techniques. I run into a problem that’s going to be difficult to fix due to poor design and I find a new technique that makes it easier. Mistakes are essential to learning.

    With that in mind it may seem contradictory that I want to discourage beginners from making this very common mistake. The thing with code smells, in particular duplicated code, is that the code will work perfectly fine and it is only when it is revisited that the problem becomes apparent. For beginning developers this may never happen until long after the bad habit has been learned.

I’m not suggesting that all ability of pasting code should be removed, just the repeated pasting of code. Cut and paste should remain as it’s a convenient way to move code around. I would just like to see the paste buffer cleared after code is successfully inserted.

I realize that even with the measure described here it’s still going to be possible to duplicate code via a plain jane text editor that you use as a scratch pad or simply by typing the same thing over and over again. My suggestion is that by making it inconvenient it would encourage the better practice and improve code quality.