Wednesday, July 09, 2008

PHP best practices

I'm currently working for a company that uses the LAMP application stack. They have only had one full time programmer since they started, and he's a cowboy. They don't use much of a database abstraction layer, they mix their display code with their business logic, they don't do any testing, and even worse than not using source control at all, they sometimes use source control.

I'm starting a new project that will be fairly large and independent from the rest of the site, so I'd like to introduce some better development practices. I don't know much about php frameworks and stuff, so if you have any suggestions for what I should use please post them in the comments. The only major requirement is that it can be used along side the existing code. So what do you suggest?

Monday, June 30, 2008

JS equivalence operators: "Good enough for government work"

I was having some strange behavior with a javascript app I wrote. It's an image thumbnailing interface that allowed the user to zoom and drag an image around. When it loads, the image is scaled to be either as tall or as wide as the thumbnail size, and the other dimension is larger. The user can zoom in and out, but they can't zoom it smaller than it starts so no whitespace can appear. When a user zoomed in and then all the way out, the image would pop out of the frame a little bit and whitespace would appear at the bottom (this was an image that was as tall as the thumbnail size, I imagine the whitespace would be on the right if the image were as wide as the thumbnail size and taller). After tracing through the javascript for a while I realized the problem; javascript considers ('' == 0) to be True.

I have a function that repositions the image so when you zoom in/out it stays centered on the same point. I wanted to be able to call it to reposition for a move that only had a horizontal vector, so I made it check to make sure there was a value for each of the x and y coordinates before it tried moving the image on that vector. I passed in an empty string when I didn't want to make a move on that vector. The problem came in to play when I zoomed out to the max and the image's position on the short dimension became 0. I want to move the image to 0 on that vector, but my test for no value was catching the 0 and calling it "nothing", just like ''.

Once I tracked this down, the solution was simple. Just use the "really equal, I mean it for reals" operator; a.k.a. "===".
if (left != '' || left === 0) { do stuff; }
A more appropriate way to do this might be to have a real value like "nochange" mark when I don't want to do anything with that vector, but I did this because I didn't want to find all the places where I used '' and change them.

Wednesday, June 25, 2008

"bug" with onclick handlers in IE

I had an issue today with Internet Explorer. An object with an onClick handler worked fine in Firefox and Safari, but in IE the handler only fired every other click. In the course of debugging I discovered that if I clicked slowly, it worked on every click. I realized that this was because IE must be registering an onDblClick event instead of two onClick events. A little testing confirmed this. I searched to see if someone else had the same problem, and found this page. User jamescover had the same issue and found a solution: use the onMouseUp event to handle clicks instead of onClick. He also directed the focus in the onMouseDown event, but I found that part to be unnecessary in my application. A demo of his solution can be found here. I'll reproduce the code in this post in case that page ever gets taken down:

<script type="text/javascript">
<!--

var x = 0;
function addX(){
document['oFrm']['num'].value = x;
x++;
}

var y = 0;
function addY(){
document['oFrm2']['num2'].value = y;
y++;
}

//-->
</script>
This one invokes the function <b>onclick</b>
<form name="oFrm">
<input type="text" name="num" size="5" />
<input type="button" value="add" onclick="addX();" />
</form>
This one focuses the text field <b>onmousedown</b>, then invokes the function <b>onmouseup</b>
<form name="oFrm2">
<input type="text" name="num2" size="5" />
<input type="button" value="add" onmousedown="this.focus();" onmouseup="addY();" />
</form>

Wednesday, May 14, 2008

A Better CAPTCHA

I don't yet have any need to implement CAPTCHA myself, but if I did, it wouldn't be your standard distorted and scribbled on text. It would be one of these:

Microsoft Asirra

With Asirra, to identify yourself as a human you have to identify a series of pictures as cats (excluding the dogs). It seems like a sound approach, but on the face it looks so nonsensical that I feel compelled to use it. Picture this internet argument: "Well you plainly have no idea what you're talking about on this issue, so I won't keep wasting my valuable time trying to fight with your stupidity! As soon as I click on these cats, you'll never hear from me again!"

reCAPTCHA

This one is more serious, and has a purpose too. Instead of displaying random obscured characters, it displays a real scanned image of two words from an old book. One of these words has been identified and the other has not. The user types both words, the computer verifies the user's humanity with the known word, and records what the human said the unknown word was. Through this process the un-digitized book becomes completely digitized. They're turning CAPTCHA tests, a "lesser evil" annoyance, into something that's actually good.

Tuesday, May 13, 2008

Why so many microsoft shops?

While looking for a job, I've been surprised by the number of places developing on a Microsoft platform. I didn't know that it was so common, at least in Chicago. If you're using a Microsoft OS, service, or language and it works for you then great, but aren't there open source alternatives that are just as good and not as expensive? Especially MS SQL server; whenever I see that I can't help but wonder why they aren't using MySQL or Postgresql. Does anyone reading this work in the Microsoft world at work? What keeps your company there?

Friday, January 11, 2008

Rot13 Utility

Rot13 is a common method for obfuscating text, often used to randomize passwords or to hide "spoilers" from online discussions. The tool I most commonly use to translate rot13'd text is http://www.rot13.com/, and that works well for translating long sections of ciphertext back in to plaintext. However, often there is just one or a few words to be translated from plaintext to ciphertext, and I find the site to be too much overhead for the task.

That's why I made a simple php script on my website to do my rot13 translations from now on. The key difference between mine and rot13.com is that the form on mine uses the GET method rather than POST. This allows me to make a firefox bookmark to translate text directly from the url bar. To do this, bookmark this url: http://timsaylor.com/tools/rot13.php?plaintext=%s. Then in the bookmark's properties add a value to the keyword field. My keyword is "rot", so now whenever I type "rot [text]" into my url bar, it sends that to my script and opens a page with the ciphertext.

It's just a simple utility, and writing this blog post about it took longer than actually making the script itself. I just had to rot13 something today, though, and I remembered wishing that I could do it more simply. A quick search turned up this rot13 php function, which meant all the hard work was done. I just wrapped that up in an html form and put it online. The source is here.

Thursday, November 01, 2007

Internet Famous

Today I was informed that a project that Dan and I collaborated on recently received a bit of attention on the internets. Our "In Case of Revolution Break Glass" box, pictured below.

Mask box


Dan had the idea, I did the woodwork, and Dan painted and lettered. One major reason that Dan wanted to make this was so he could post it on a "Show off stuff you've made" thread on Something Awful. From there it was cross posted to Digg (edit: and made the #1 spot apparently!), then on to this blog, which was Reddited, and then on to College Humor today. There were many more too, 3,580 hits on Google. The only credit we got was someone in the digg comments saying "This is from Something Awful, a goon did it" (half true), but that's to be expected. I'm still pleased. And next time it'll say "timsaylor.com" across the bottom. :-)

Tuesday, October 30, 2007

Maker Faire: Austin '07

I got back from the Maker Faire last weekend, and it was amazing. Many of the projects I had seen online like the Singing Tesla Coil, the RepRap, Cyclecide and their freak bikes, all the kits from the Maker Store and Adafruit Industries, and some projects from Instructables were there to be played with in meatspace. The highlights for me were riding a double decker bicycle, seeing the 10 foot tall trebuchet launch a watermelon, Cyclecide's bicycle powered ferris/hamster wheel, the CNC woodworking tools, learning how to spin wool into yarn, 5 minute t-shirts with Bre Pettis, and actually playing the singing tesla coils through a keyboard. Unfortunately I'm an idiot and brought my camera to the faire, but left the battery safely plugged into the wall in Chicago. Fortunately, plenty of other people documented the event for me. Enjoy the pictures, and start saving up for San Mateo in May!

Thursday, September 20, 2007

Maker Faire Videos

I've been planning to go to the Maker Faire in Austin, Texas this October, and I'm surprised none of you are interested in going too. If you're not familiar with the kind of stuff that'll be there, here's some videos showing the cool stuff at the first two faires.

http://revision3.com/systm/makerfaire
http://revision3.com/systm/maker07

Check out information about this years faire at http://www.makerfaire.com.

Wednesday, September 05, 2007

Advanced but still Primitive

This story about evolutionary hardware design was indeed, as the URL indicates, a damn interesting read. The thing that struck me the most was that the design the evolutionary algorithm came up with didn't work on other chips of the same type, just on the chip on which it evolved. The minute differences between different chips of the same type are negligible if their use is limited to things we currently understand, but if the only constraint is the laws of physics then the solutions that evolve are so complex and out of the ordinary that, were we to understand them, our advanced tools would probably seem like sticks and rocks.