One day a man was writing an email when he left the room to get something to drink. In his absence, his cat walked over the keyboard and among the myriad of characters entered into the body managed to send the email off. The man returned and soon realized what had transpired. Wondering what the recipient might think receiving such a random unsolicited email, he decided to pass it off as a new programming language. He named it after his cat – Perl.
Category Archives: Programming
Great Programming Book Review
On Dennis Ritchie
The past week there has been a ton of recognition for the contributions Steve Jobs had made to computing and society through his work. Steve Jobs was a household name. Dennis Ritchie however was not a household name. Despite this his work was amazingly influential on almost every aspect of our daily lives either directly or indirectly. Everything from the computer I wrote this blog post on to the server sending you this blog post involved his work.
Rob Pike best eulogized his work, copied below for posterity. An amazing legacy.
PHP’s include_once() Is Insanely Expensive
I’ve always heard the include_once() and require_once() functions were computationally expensive in PHP, but I never knew how much. I tested the following out on my i7 2010 MacBook Pro using PHP 5.3.4 as shipped by Apple.
This first test uses include_once() to keep track of how often a file is included:
Took: 10.020140171051 sec
This second example uses include() and uses in_array() to keep track of if I loaded the include:
Took: 0.27652382850647 sec
For both, the include had the following computation:
Lesson learned: Avoid using _once if you can avoid it.
Update: That means something like this will theoretically be faster:
DBSlayer + Node.js
Lately it seems the rage among developers is to take node.js and combine it with something else unusual. So here’s my contribution.
DBSlayer is a project by NYTimes a few years ago that seems to be somewhat forgotten but is pretty cool. It’s another MySQL proxy, but with a slight twist. Rather than use a binary protocol, or XML, they went with JSON. It supports things like connection pooling, round-robin distribution to slaves, automatic fallover, and it’s mutithreaded. It’s pretty fast and easy to work with. It’s almost like turning a MySQL database into a REST API. You pass a SQL query as a query argument and it gives you a JSON response.
Once you start it you can do something query using a request like:
http://localhost:9090/db?%7B%22SQL%22%3A%22SELECT%20*%20FROM%20facts%20WHERE%20id%3D1%3B%22%7D%20
That will give you a JSON object containing the result of your query.
So doing that in node.js is roughly:
Running that looks like this:
$ node test.js
STATUS: 200
HEADERS: {"date":"Fri, 27 May 2011 02:02:27 GMT","server":"dbslayer/beta-12","content-type":"text/plain; charset=utf-8","content-length":"290","connection":"close"}
BODY: {"RESULT" : {"HEADER" : ["id" , "fact" , "author" , "ip" , "timestamp"] , "ROWS" : [[7 , "1+1=2" , "raccettura" , "127.0.0.1" , 123456]] , "TYPES" : ["MYSQL_TYPE_LONG" , "MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_LONGLONG"]} , "SERVER" : "db"}
You could obviously clean that up and create a little library to hide the HTTP parts.
It’s an interesting JS centric way to abstract your database while maintaining SQL level control. JSON is becoming the new XML.
PHP 5.3.4 Changes rand(), Filled My Error Log, Spikes Load
I ran into a peculiar situation with a PHP web application that went from working for several years without incident to suddenly resulting in timeouts and spiking the load on my server. Some investigation traced it back to a seemingly benign and obscure change to PHP’s rand() implementation between 5.3.3 and 5.3.4.
To summarize several hundred lines of code: it gets a value from an array where the index is a random number between X and Y. X and Y are highly unpredictable by nature of the application. It keeps trying with different values until something is returned. Something like:
See it? If you don’t, you shouldn’t feel bad, I didn’t see it initially either.
Prior to PHP 5.3.4 mt_/rand did not check if the max is greater than the min. This has changed as a result of bug 46587. That 4 line change made an impact.
Take this example code:
In PHP 5.3.3 you’d get:
$ php test.php right: 3 wrong: 4 $ php test.php right: 2 wrong: 5
Despite the incorrect order of max/min it actually worked just fine. It had done so at least since PHP 4.3 (circa 2003) as far as I’m aware.
In PHP 5.3.4:
$ php test.php right: 2 PHP Warning: mt_rand(): max(1) is smaller than min(5) in /test/test.php on line 4 wrong:
As a result, this while(){} never terminated until the timeout was reached.
The solution is obviously trivial once you actually trace this bug back:
This resulted in several GB’s worth of warnings in my error log in a matter of hours. You can also see how it (the brown area) dropped off once the fix was deployed as measured by % of wall clock time:

It’s the little things sometimes that cause all the trouble.
Budget Calculator Dice
When I was in High School somehow dice became popular for the 2000th time since the dawn of humanity. For some strange reason I felt the desire to build a little dice game in my TI 83 calculator in TI-BASIC. I had long forgotten about this, and recently stumbled upon it. I transcribed it to my computer recently since I don’t have one of those cables and decided to just throw it out there on the web.
Looking back on it, I could have written this a lot better
. Regardless it’s a fun trip down memory lane. I believe I wrote most of this little gem while lifeguarding on a Sunday morning. Nobody drowned in the production of this code, I swear.
My favorite part may be the about text “DEPRIVING A VILLAGE SOMEWHERE OF AN IDIOT”. Someone at some point somewhere said that about me, and it stuck for years.
From Blog Post To Dictionary
A 2008 blog post of mine with one of my favorite titles ever “Object Oriented Masturbation” has led to someone creating an Urban Dictionary entry. My official stance on this is that I find it amusing, and want to accelerate the adoption of this new term.
object-oriented masturbation
ob-jikt | o·ri·ent-ed | mas⋅tur⋅ba⋅tion
- 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.
Spread the word and be sure to visit the Urban Dictionary term and give it the thumbs up. They sell merchandise with dictionary entries on it too if you want a mug.
Another Brick In The Facebook Wall
I ran across the problem recently trying to write to a users wall using the Facebook API. The Facebook documentation is hardly sane as it’s a mix of languages, not entirely up to date, and lacks good examples. The error messages are hardly ideal either. “A session key is required” at least leads me in the right direction. “Invalid parameter” is just unacceptable and makes me stabby.
So here’s some cleaned up pseudocode I pulled together that will hopefully be of use to others who bang their heads against the wall. This “works for me” in my limited testing over several days:
Getting the right permissions is key.
The thing that ends up being the most confusing is the session_key. After reading the docs, I was inclined to do:
What you really want is:
You can also use adapt this to use stream.publish if you’d like.
Debugging Sherlock Holmes and Dr. House Style
The psychology of computer programmers is interesting stuff. The classic view of a programmer is someone who sits around creating all day like an artist or writer. It’s a creative job, trying to be the next Bill Gates. The reality is that the most spend a significant amount of their time diagnosing problems and debugging. That includes analyzing bug reports, tracing bugs, finding solutions, implementing, and testing. In practice this part is much more detective than artist.
After years of observation, I’ve come to the conclusion that most good programmers bare a striking resemblance to both Dr. House, and Sherlock Holmes (whom the Dr. House the character is partly based upon).
Overlapping Traits
- Reluctance to accept cases they don’t find interesting – Both Dr. House and Sherlock Holmes hate cases that they aren’t interested in. Programmers always gravitate towards bugs that interest them, and shun bugs that don’t. Very few will even attempt to dispute this.
- Otherwise lazy – When not solving something that interests them, they are what most would call lazy.
- “Rubik’s complex” – Obsession with puzzles.
- Reliance on a related science – Dr. House and Sherlock Holmes rely heavily on Psychology. Programmers gravitate towards user experience which Wikipedia defines as incorporating “psychology, anthropology, computer science, graphic design, industrial design and cognitive science”. Coincidence?
- Substance dependence – Dr. House prefers Vicodin, Morphine and Sherlock Holmes went for Cocaine, Morphine. For programmers it’s often an extraordinary dependency on caffeine that keeps them going.
- Overconfidence to the point of arrogance – I don’t think any further explanation is necessary. Programmers are as arrogant and defensive on their work as Dr. House and Sherlock Holmes are about their diagnosis/solution.
- Introvert – Both Dr. House and Sherlock Holmes are introverts. So are many/most programmers.
- Strong deductive reasoning skills – The best programmers are the ones who can analyze a bug report and using knowledge of the application and related technologies can diagnose the problem with accuracy that surprises even those with many years more experience.
- Use of alternate names – Holmes and Dr. House call people by their last names. Programmers have this habit of using network names, usernames, IRC nicknames.
- Showmanship for their skills – Dr. House diagnosed a waiting room full of patients in about a minute with surprising accuracy without meeting with each patient. Sherlock Holmes has a love for elaborate traps to show off. Programmers love to show off. That’s why so many blog. Accomplishments are the rare things they willingly document.
Slightly higher occurrence of the following personality traits may apply: “moody”, “bitter”, “antagonistic”, “misanthropic”, “cynical” “grumpy”, “maverick” and a “curmudgeon”.
Sherlock Holmes
The most distinct Holmes trait is that he refused guessing or theorizing before having the necessary clues or data to reach a conclusion and solve the case.
Programmers who fall in the Holmes party refuse to make guesses without seeing some evidence and immediately tries to reproduce the bug and starts tracing to gather data. Once there is a mountain of data they start to deduce the problem and the solution. Once they reach a conclusion they will break it down into a very concise deductive argument.
Pipe smoking is optional.
Dr. House
The most distinct part of the House approach is the willingness to make an educated guess based on limited information.
Programmers who fall in the House party are willing to make guesses early on and immediately start debugging in very calculated parts of their code base. As they come to realizations and learn new information they are willing to adjust or completely abandon their former approach and go with a new hunch.
They often find themselves soliciting ideas from others and using good ones however they almost enjoy shooting down ideas as invalid based on their skills and knowledge.
Conclusion
I’m pretty sure there is no “better” approach. It’s just two different ways of going after a problem. It’s completely possible to be a hybrid. I think it’s more of a spectrum of personality and technique.
