Big difference between "I don't think has bearing on the job" and "is illegal to ask about". I wouldn't work for a company that wanted to drug test me either, out of principle. On the other hand, if I'm an EMT, it would be arguably reasonable to drug test me, but still illegal to ask if I'm married.
But its possible that it has an immense bearing on their work. Likewise having 10 years experience doing one thing could have either incredible relevance or none at all. Its a matter of likelihoods and what society has deemed acceptable for making hiring decisions.
Well, almost anything is possible; your argument is specious. Drug tests shouldn't rationally tell you anything more than marital status because any reasonably intelligent drug user can beat a simple piss test. If anything, a drug test is testing for stupidity more than drug use, it just so happens that those have a tendency to go hand-in-hand with respect to low-wage workers.
But programmers? If Google drug tests people I'm sure they do so for liability reasons, not because they care what Johnny Genius does in his free time.
Of course, it's absurd to suggest companies make sure every worker isn't high on the job every single day, but that doesn't change the fact that it's judging someone based on something which could have absolutely no bearing on their work.