Categories
Mozilla Programming Rants

Object-Oriented Masturbation

Doing some research for an upcoming installment of an infamous series of blog posts (to be released at an undetermined date) I’ve come to notice this annoyance. In general I like object-oriented programming. I think it allows you to take a complicated problem and produce simple, more reusable and easier to maintain code. Assuming the programmer is sane individual, and that sometimes is a leap of faith.

I’m not sure if there are programmers who feel the need to complicate things just for the sake of showing off (oh, look at me!), or if they legitimately don’t know any other way. I suspect a little of both. Perhaps the programmer thought it was possible the code would grow in complexity in the future, and just wanted to prepare in advance. I don’t know. But it annoys me.

I consider this abuse of Object-oriented programming to be Object-oriented masturbation since the only one who gets any enjoyment out of it is the developer who does it. Here’s a slightly exaggerated (though not far off) example of Object-oriented masturbation typical of what I’ve seen many times before:

Objective

Sum two numbers and print the result in the format “The answer is: X” where X is the sum of the two numbers.

Correct (sane) Answer

// This is the simple, obvious answer
function sum(x,y){
    document.write(‘The answer is: ‘ + (x+y));
}
 
// To run:
sum(1,1);

Arguably, you could do the math and store in a variable if you are squeamish to doing math in any line containing an output method. Regardless, this is dead obvious and simple. And yes, this technically still does use OOP since it uses document.write().

Insane (masturbatory) Answer

function MathLib(){
    this.answer = null;
}
 
MathLib.prototype.sum = function (x,y){
    this.answer = x + y;
}
 
MathLib.prototype.getAnswer = function(){
    return this.answer;
}
 
function Printer(){
    this.preText = ;
}
 
Printer.prototype.setPreText = function(str){
    this.preText = str;
}
 
Printer.prototype.out = function (str){
    document.write(this.preText + str);
}
 
// To run
var math = new MathLib();
var print = new Printer();
math.sum(1,1);
print.setPreText(‘The answer is: ‘);
print.out(math.answer);

I did the exact same thing in 1 logical line of code (3 if you include the function itself) up above. What was gained from the object-oriented design? I argue nothing. Rewriting the first example would take a minute should something need changing. If there were a need to localize that string, I could pull it out and either make that a 3rd parameter, or some global string the function can use. There’s no benefit here. This is over engineering. There’s nothing truly reusable here. The closest is the Printer.out() method, and in reality it’s a wrapper for document.write().

The bonus joke is that the object-oriented implementation is significantly slower.

Run 1: 0.0266, 0.0451
Run 2: 0.0314, 0.0464
Run 3: 0.0329, 0.0462
Run 4: 0.0268, 0.0468
Run 5: 0.0274, 0.0475
Avg: 0.02902, 0.0464

The first example is also 62.5% faster in Firefox 3.0.4 since the all that overhead is gone. If you have FireBug installed you can check that out here.

object-oriented masturbation
ob-jikt | o·ri·ent-ed | mas⋅tur⋅ba⋅tion

  1. the stimulation or manipulation of one’s own ego by way of using object-oriented code in places where it has no advantage instead resulting in unnecessary complication and bloat.

If your guilty of doing this, please cut it out. It’s annoying. Thank you.

No I will not single out links to the pages that inspired this, but rest assured it litters the web.

Categories
Programming Web Development

PHP Namespacing

The PHP folks have finally announced that PHP will get namespacing in the form of ‘\‘. The universal escape character. They really should have went with the standard ‘::‘ or ‘:::‘. Using ‘\‘ is going to work well.

I was thinking something like this would be more appropriate (background on compatibility here):

......................................__................................................
.............................,-~*`¯lllllll`*~,..........................................
.......................,-~*`lllllllllllllllllllllllllll¯`*-,....................................
..................,-~*llllllllllllllllllllllllllllllllllllllllllll*-,..................................
...............,-*llllllllllllllllllllllllllllllllllllllllllllllllllllll.\.......................... .......
.............;*`lllllllllllllllllllllllllll,-~*~-,llllllllllllllllllll\................................
..............\lllllllllllllllllllllllllll/.........\;;;;llllllllllll,-`~-,......................... ..
...............\lllllllllllllllllllll,-*...........`~-~-,...(.(¯`*,`,..........................
................\llllllllllll,-~*.....................)_-\..*`*;..)..........................
.................\,-*`¯,*`)............,-~*`~................/.....................
..................|/.../.../~,......-~*,-~*`;................/.\..................
................./.../.../.../..,-,..*~,.`*~*................*...\.................
................|.../.../.../.*`...\...........................)....)¯`~,..................
................|./.../..../.......)......,.)`*~-,............/....|..)...`~-,.............
..............././.../...,*`-,.....`-,...*`....,---......\..../...../..|.........¯```*~-,,,,
...............(..........)`*~-,....`*`.,-~*.,-*......|.../..../.../............\........
................*-,.......`*-,...`~,..``.,,,-*..........|.,*...,*...|..............\........
...................*,.........`-,...)-,..............,-*`...,-*....(`-,............\.......
......................f`-,.........`-,/...*-,___,,-~*....,-*......|...`-,..........\........ 
Categories
Mozilla Open Source Programming

Benchmarking And Testing Browsers

When people talk about open source they often talk about the products, not the process. That’s not really a bad thing (it is after all about the product), but it overlooks some really interesting things sometimes. For example open source tools used in open development.

A few months ago Jesse Ruderman introduced jsfunfuzz, which is a tool for fuzz testing the js engine in Firefox. It turned up 280 bugs (many already fixed). Because the tool itself is not horded behind a firewall it’s also helped identify several Safari and Opera bugs. It’s a pretty cool way to find some bugs.

The WebKit team has now released SunSpider a javascript benchmarking tool. Something tells me this will lead to some performance improvements in everyone’s engine. How much will be done for Firefox 3.0 is a little questionable considering beta 2 is nearing release, though you never know. There’s been some nice work on removing allocations recently. So just because it’s beta, you can’t always assume fixes will be minor in scope.

Another test that many are familiar with is Acid 2 which essentially is checking CSS support among browsers. Ironically this one too was released when Gecko is somewhat late in the development cycle.

Efforts like this really help web development by allowing browser developers to have a baseline to compare their strengths and weaknesses. Having a little healthy competition as motivation can be pretty helpful too 😉 .

Categories
Internet Open Source Programming Web Development

Wikipedia Infrastructure

Here’s a great read on Wikipedia’s Infrastructure. Two excellent sets of slides. A lot can be done with a LAMP stack. The common theme: caching and careful optimization. There are some really impressive stats in there.

Categories
Programming Software Web Development

Coda Coolness

So I downloaded and tested out Coda a little bit today. My initial impression is that I’m very impressed. It could be the new standard. A few like Alex King point out that it uses Safari as the default engine. I don’t think that’s so bad. Nothing stops you from using 2 windows one being Firefox (obviously running Firebug). I think the editor itself is rather well polished and very refined. The use of Bonjour to allow for collaboration makes it perfect for multi-developer environments. And yes, you can have more than two developers collaborating in 1 document.

My typical workflow is very abrupt. I tend to have Firefox, IE, and an IDE open at all times when coding pages. Not to mention a KVM switch to go between platforms. This still consolidates several things, and with a much nicer UI.

I’ll need to try it (for actual development purposes) for a few days before I could tell if it really works as well I hope. Panic is one of my favorite Mac developers. Really well polished products are their specialty.

Categories
Politics Programming Tech (General)

Time Sucks

One of the hardest things to program is the Date and Time. This is especially true when your doing it on the web. Why is that? Using a unix timestamp is immensely helpful and resolves many of the complexities, but it does have some issues (besides the Y2K38 bug). Well lets take a look at some of the “typical” things you need to be aware of:

  • Your server is in one timezone, your users are in 23 others.
    Users don’t care what the time is at the site. They want things in their time.
  • Does your server even know your users timezone?
    You could do this with JavaScript, and send it to the server, but that’s a mess. Or send a timestamp to the client, and let JavaScript print it out. But that’s still messy.
  • Timezones aren’t obvious (think Indiana).
    Did you know some even use :30 such as UTC-3:30 for Newfoundland Standard Time.
  • Looking back in time (or forwards) is difficult (how many hours between X and Y accounting for leap years, and DST changes)?
    This is a mess, enough said. And just in case you have a formula, did you account for the conversion between Julian and Gregorian calendars? Don’t forget not everyone switched in 15 October 1582 / 4 October 1582. Going forward remember we’ll eventually have another leap day, since the Gregorian calendar isn’t perfect.
  • Your server observes DST. Does your user? When?
    Get the picture? Remember most states do, except for Hawaii (yea, that’s another Timezone) but Arizona doesn’t either, except for Navajo Nation. Again Indiana!
  • The Politics of Time…
    If you call UTC+2 Israel Standard Time, you upset visitors from Muslim nations like Egypt. Call it Central Africa Time, or Egypt Standard Time and your considered anti-semitic. Same goes for UTC+8, is it Chinese Standard Time or Hong Kong Time? Most avoid this by just listing UTC±N. Unfortunately this confuses people, especially Americans who only refer to timezones as “Eastern” (UTC-5), “Central” (UTC-6), “Mountain” (UTC-7) “Pacific” (UTC-8). Note these American names aren’t so common in all of North/South America.
  • Daylight Savings Time for 2007+
    Then you have a bunch of clowns who voted for the Energy Policy Act of 2005, creating the Y2K7 bug. The idea was an extra hour of daylight in the evening would reduce electrical use. What they didn’t realize is that it cuts daylight from the morning. My guess would be a follow up bill may fine the sun for failing to provide adequate light, and eventually include economic sanctions. 😛

I thought a while back this could suck. Think about all the time/money that goes into updating and testing systems for these few extra weeks of DST. What a drag.

Swatch Internet Time was an obvious bust, but perhaps we could all just use UTC?

Categories
Programming

Getting A Random Row In MySQL

This is a great page on RAND() in MySQL. Very nice breakdown of how to gain performance with one of the biggest “it should be simple but it isn’t” problems in DBMS’s.

Categories
Mozilla Programming Web Development

Komodo Convert

After reading about Myk’s use of Komodo, I decided to give it a try. I’ve yet to find the truly perfect IDE, so I’m always open to good alternatives.

For years I used BBEdit Lite, which was good enough for my somewhat basic needs, TextWrangler came and I moved over. On the Windows side of things, I had tried numerous editors for years, ultimately using Eclipse, ConTEXT, and (on a few occasions) jEdit. Mainly on the Windows side it has been ConTEXT, since it didn’t have the restrictive feel many IDE’s have. On the downside, it didn’t do much.

At work, I’ve been using Macromedia HomeSite. Overall it’s not too bad, but it does have it’s quirks. There’s definitely a bug in FTP that causes it to occasionally not show all the subdirectories when you nest to deep. Then it sometimes randomly throws errors at you. Most of the time they can be safely ignored, making me wonder if they are still errors when nothing bad happens.

So I downloaded Komodo Edit (free) on my Windows Laptop, and my Mac Mini, installed it on both, and played around. First order of business was playing with some local files. End result was that it handled things very well. I didn’t have to fuss around to much to get things accomplished… I like that. Next step was to try the FTP functionality (SFTP and SCP as I don’t like FTP). It worked, I was able to edit a few files and it performed rather well. Also nice was that it let you know it read the directory from cache. Most will hide this info from you.

In the end I decided a few things were missing. The most notable thing missing and preventing it from replacing HomeSite was the sidebar file manager. I work on many files, so I need a quick way to get to them. File –> Open wasn’t going to work. I decided to file a bug and request a similar feature as an enhancement. Other editors do a similar sidebar file tree, this wasn’t something revolutionary.

Ends up there is an Remote Drive Tree Extension that provides the functionality I desire. Sweet! The usual install process, restart and I’m going. Works perfectly. Only thing missing is drag/drop support so I could easily add files or move them around, renaming would also be helpful. Perhaps those features will come in the future.

So after a little time with it, I’ve decided I’m ready to try it for a week, and see if it handles my needs for the week. It has now qualified for testing.

I think I like it so far. I do have a few pet-peeves though:

  • FTP support is still a little buggy.
  • Slow startup time.
  • Prefs feel somewhat awkward and not as intuitive as one would hope.

But it’s still beta, so there’s time for it to grow.

Categories
Programming Software Web Development

xmlHttpReq.overrideMimeType() in IE7

This is just a little note for anyone doing xmlHttp work. I just encountered this situation this morning. As most web developers know IE7 introduces support for the native scriptable XMLHttpRequest object. The big advantage here is that ActiveX is no longer necessary to use ajax applications on IE. One thing I did note is that there is a slight difference in their support for the XMLHttpRequest object. Take the following code:

// Mozilla/Safari/IE7+
if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
    xmlHttpReq.overrideMimeType(‘text/xml’);
}
// IE6-
else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open(‘POST’, strURL, true);

That seems to break for me on IE7. A little experimentation shows that it doesn’t support the overrideMimeType() method. A simple fix for this is to simply check before invoking it as follows:

// Mozilla/Safari/IE7+
if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
    if(xmlHttpReq.overrideMimeType){       
        xmlHttpReq.overrideMimeType(‘text/xml’);
    }
}
// IE6-
else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open(‘POST’, strURL, true);

This is just FYI for anyone who happens to encounter this error. It’s a simple fix. This somewhat goes without saying, but make sure your request returns from the server as text/xml or you’ll likely still encounter issues.

Categories
Programming Web Development

Asynchronous Processing With PHP

Several weeks ago I was looking on line for a way to have PHP execute a background process, so that the page didn’t hang while some functionality happened. One way to do that is to use fsockopen() to open a url to exec what you want. But I wanted to see if there was another way. This is what I came up with.

I should note that this isn’t that well tested. I ran it on a Windows and Linux system without incident to test it, but I haven’t given it a very thorough exam. Also note $call is completely unsanitized. If you’re using this and accepting anything from the web, you need to make really sure that $call is completely sane and free of evil.