August 2011

22 Aug

In-depth resource on how browsers work

in css, html, javascript, programming

How Browsers Work is an in-depth resource -- about 50 printed pages long -- explaining how a browser begins with HTML, CSS, and JavaScript files, and ends up with a display.

The clear explanation, based on FireFox and Chrome/webkit, explains many of the nuances that frequently cause edge-case bugs in CSS rendering or JavaScript code. It also provides a good introduction to parsing techniques (browsers use various kinds of parsers and parser generators), along with other lower-level implementation details.

19 Aug

Good Code: A Useful Definition

in drupal, programming

What is "good" code? We toss around all kinds of high-level definitions, but these are usually either hopelessly vague or hopelessly complex. This month's Pragmatic Programmer magazine offers a fantastic definition, summarized on this card:
Seven Virtues of CodeSeven Virtues of Code
(Apparently originally from Agile in a Flash)

Tim Ottinger and Jeff Langr's PragProg article is fantastic -- an absolute must read for anyone who regularly works on software. I tell you. I beg you. I entreat you. (I'd bribe you if I could afford it.) Please read it.

Seven Virtues of Code

Seven Virtues of Code
18 Aug

Microbenchmarking PHP: Switch Statements are Slow

in benchmarks, drupal, php, programming

There are very good uses for Switch statements. They can be great for nesting logic. But sometimes they are used as a way to (essentially) map a name to a value. In such a scenario, the body of the case is just a simple assignment.

After noticing this pattern a lot recently, I thought I'd benchmark it against the obvious replacement candidate: a hash table lookup. PHP arrays are more like ordered hash tables. For that reason, they provide very fast random access. Are they faster than a switch statement?

I ran two tests. In the first case, I assumed no default. In the second, I assumed a default. The conclusion: Arrays are faster. Read on for the details.

17 Aug

Your Code is Not Your Baby

in programming

In the introduction to The Gay Science, Friedrich Nietzsche famously compared writing to giving birth:

"[W]e have to give birth to our thoughts out of our pain and, like mothers, endow them with all we have of blood, heart, fire, pleasure, passion, agony, conscience, fate, and catastrophe."

Ever feel that way about your code? You shouldn't.

Your code is not your baby. It's not living, it's not breathing, and frankly it doesn't deserve much fawning over. And worse, getting attached to your code at that level will make you a worse developer.

  • When your code is your baby, emotional reactions trump rational actions.
  • When your code is your baby, you don't want to change it.
  • When your code is your baby, you resist other people's attempts to make it better.
  • When your code is your baby, you fail to see its shortcomings.
  • When your code is your baby, what it does takes a back seat to what it is (to you).

Your code is just your code. Nietzsche is wrong -- or at least guilty of gross hyperbole. Transforming your thoughts to words may cause a certain amount of psychological pain, and you might indeed pour your creativity into every line. But at the end of the day, you have an object that performs a function. Treat it that way, and you will have the freedom to make it better.

10 Aug

Switching from Mollom to Disqus: The aftermath

A while back, I stated here that I was switching from Drupal comments with Mollom to Disqus comments. Here are my preliminary results. First, AFAIK, I currently have no spam on my blog comments. Second, on an average day, I get about 4-5 spammish comments that I need to delete. Third, overall my legitimate comments are down, too. I get <1 a day. That may just be because I don't write anything inflammatory anymore. More likely, it's due to the higher barrier of entry. The net result: Disqus is far more manageable for me.