Don’t write “smart,” “fast,” or “safe” code

This is really about naming. Never name anything “smartXXX,” “fastYYY,” or “safeZZZ.” I see it happen all the time.

The crux of the biscuit is that once you’ve called a function, method, class, or what-have-you “smartXXX,” “fastYYY,” or “safeZZZ,” why the hell would I ever use ordinary XXX, YYY, or ZZZ. Am I supposed to prefer “dumb,” “slow,” or “dangerous” code? If the version you’ve produced is purely superior and functionally equivalent to the existing code otherwise, get rid of the existing stuff. If it differs in some meaningful way, it can’t ONLY be that it differs in being smarter, faster, or safer. Otherwise, you’d purge the other one. Tell us something useful in its name about why the old one continues to exist. If your new one is “safe” because it catches exceptions and deals with them somehow where the old one allowed them to pass upward, don’t call it “safeFoo,” call it “FooNoThrow.”
Continue reading

Mouse Button Drag Tracking in Javascript

HTML5 Canvas:

Event log: (code adapted from Jan Wolter)
Clear event log
One of the first obstacles you’ll encounter when trying to use HTML5 Canvas for interactive graphics experiments is that mouse button drag tracking is badly broken. First, there isn’t uniformity across browsers even for the numbering of the mouse buttons. Second, once the mouse leaves the canvas element itself, you get no more mouse events, so you won’t even hear about a mouseup event that ends a drag if it happens outside the canvas.
Continue reading

A Fluid Simulation in Javascript for the HTML5 Canvas

Show FPS

I wrote a small iPhone app a little while ago called FireWater, for which I developed a simple pressure-based fluid simulation.

I thought it would be fun to port it to the HTML5 Canvas in Javascript to learn the Canvas myself and teach a little about writing simulations.

If your browser supports the HTML5 Canvas element (Chrome, Safari, Firefox, Opera, IE9) you should see the canvas above filled with black. Once you have a black canvas, click and drag on it to see it in action.

The GitHub repository for the source can be found here.

Hello, again

UpFork!
I have decided to begin again to write here. I will be writing about software development. I hope there may be some things of interest. That’s my son Henry there in the banner in a ball pit. I won’t be writing about him, though.

(I’ll start by reposting a few articles I wrote a long time ago…)

Picking a platform

I want to experiment in making an online diagramming tool.

So step one is picking a platform in which to implement it. Normally this is the kind of decision that would paralyze me in weeks of delicious research and tinkering. Not anymore. As Jason Fried reminds us, decisions are temporary. Just make a choice. Change it later if it needs changing. You’re just as likely to make a bad choice after wasting two weeks fretting about it. Keep moving.

Continue reading