A few words about Javascript

I’m a bit of a Javascript fan and since I don’t have anything better to do right now I though I could tell you about a few typical errors that even the guys over at Google, along with 99% of all pages I know, make.

The problem is that people learn Javascript as well, a Script language, not a proper programming language… which is a shame because Javascript itself is probably one of the most comfortable languages I know. And because they don’t see it as a proper programming language they just hack their code until it works… for now.

The worst abomination onto JS is probably browser sniffing: It’s a pretty simple technique that’s easy to understand which is probably why beginners tend to use it, however it’s also a technique that a) requires a lot of testing, b) requires a lot of updates and c) goes completely bonkers when new browsers are released.

What browser sniffing (a.k.a. useragant sniffing) does is “ask” the browser about itself and then taking appropriate measures. Seems simple, right?

Well if it is so simple, then what would happen if you ask a browser if it is a “Mozilla”… Surprise! Pretty much all major browsers (including Internet Explorer) claim to be Mozilla. So let’s ask about MSIE to make sure which Mozillas are actually Internet Explorer. Oops, Opera and a few others report that too. OK, then how about finding out which ones are really Mozilla by looking for “Gecko”. Oh, Safari says it’s “like Gecko”. If you want an almost complete list, have a a look here.

You see: it doesn’t work and it’s really a shame that people still use this first-grader technique if there’s a much easier alternative: Method sniffing.

In Javascript, every function or method that does not exist has the value “undefined”. So if you want to use something that you are afraid isn’t available everywhere, you just ask if the browser supports it directly, instead of asking for the browser and then assuming that a certain browser supports this or that.

For example, lets say we want to use the addEventListener method and as a fallback the attachEvent method, then we simply create a wrapper function:

function wrapperAddEventListener(obj,type,callback){
if(obj.addEventListener!=undefined) obj.addEventListener(type,callback,false);
else if(obj.attachEvent!=undefined) obj.attachEvent(“on”+type,callback);
else alert(“Sorry, your browser is not supported”);
}

And that’s it. And it works for pretty much everything, except for some strange HTML behaviours. Now my minions: Spread the word.

The next time I’ll be looking at the scope of Variables in JS… a topic that isn’t understood by more than a handfull of people, eventhough it’s not that difficult. See you next time.

Welcome everybody

I think it’s high time I start yet another blog, after the last one has disappeared in the great free.fr cleanup.

My name is Hans Schmucker and I’m a student at the University of Applied Sciences, Heidelberg, Germany… while I’m not in a job yet, there are already quite a few things I’ve worked on and the number of people using my software, or using software that’s based on my code has probably well surpassed 10,000 (my little Patience game alone has well over 6,000 users per day, so 10,000 is a safe bet).
What else… hmm… my specialty right now is Web Application Development (can you spell AJAX 😉 ), but I’ve also worked quite a bit with C on mobile platforms ( Quest for example uses some of my code for the PalmOS backend).

So, now I want to offer you a quick look into the mind of a developer and PalmOS enthusiast 🙂

What to start with? Ah, recent events: Microsoft‘s Steve Ballmer threatening Linux users, that they might get sued for violating Microsoft’s patents.

To tell you the truth: Linux almost certainly violates Microsoft’s patents. Why? Because nowadays pretty much anything you do violates some patent, due to the sheer number of patents. And Microsoft certainly violates a few itself. The difference? Microsoft has thousands of patents, so if somebody wants to sue them, they just threaten to sue that company for violating their patents. It’s a pretty good ecosystem, that is if you have the money to register thousands of patents just so you can defend yourself… or in other words, if you don’t care about a million dollar just for patents that you’re never going to use.

That’s why Microsoft is pushing patents wherever they can: They are the winner in a system where the number of patents you have equals power.

The alternatives? Well, for example you could make patents more expensive for bigger companies. If a company has to sacrifice 0.5% of their profits for a patent, then they’ve got to be more careful. A smaller company on the other hand could at least apply for 5 patents each year to protect real inventions that they often can’t afford to protect right now.

But let’s get back to the topic at hand: Microsoft vs. Linux.
As I said before, Linux almost certainly violates Microsoft’s patents, so why does Microsoft not sue? Well, because whoever they sue would certainly question if Microsoft’s patents are valid and Microsoft prefers potentially invalid patents, that they can use for spreading FUD to no patents at all. While I’m not a lawyer, I know a thing or two about patents and there are two interesting points that the patent office is supposed to check before they grant a patent (sadly they never do): Originality and Significance.

Basically that means that you can’t patent something that’s been used before (like patenting algebra, although that has already happened) and you can’t patent something that’s trivial (although that has happened countless times as well).

Both points are fairly dangerous to Microsoft, because they can’t really claim any new inventions as far as Operating Systems are concerned. WindowsNT (that’s the basis for NT, 2000, XP and Vista) is basically an attempt to port Windows95 to a unix-like environment. Yes. Unix. The same system that Linux is based on. And their user interface: Well I won’t even go into much detail, but for example GEM looks way to similar and it’s a lot older.

I think I’ll stop here and let you think about it yourself. See you next time.

Hans Schmucker