Nitpicking Computers

One thing I like about Ruby is that it’s usually less nitpicky than some other languages I’ve used. Sometimes tiny little differences with computers are really important, and people programming should really have a mastery of those things. But sometimes the computer is just being petty.

Here’s one that ticks me off every time, though. Let’s say I do something really simple, like look up all the rows matching a simple criteria. I then call .each on the result, to iterate over them.

But there are actually two pet peeves in one here:

  • If you get back no results, you often end up with the variable being nil. And calling “nil.each” blows apart. Sometimes this makes sense and you really need to handle nil results specially. But very often, I couldn’t care less. I said to look something up and, for every one you found, do something with it. That works if you found 0 results, too: you do nothing. If I asked you to go through your refrigerator and, for every blue apple you found, put it on the table, and there were no blue apples in your refrigerator, I’d expect you to shrug your shoulders, put no blue apples on the counter, and briskly walk away thinking I was kind of weird. I wouldn’t expect you to keel over and die because there were no blue apples.
  • If you get one result back, it’s not always an array. Sometimes it is, and .find seems to give you an array like a good citizen. But I very often have to write dumb code that checks to see if what’s coming in is an array and, if not, puts it inside a new one-item array. This is dumb. Really dumb. (And you used to be able to blindly cast anything to an array, and Array.to_a was smart enough to not end up with an array inside an array. But that’s been deprecated.)

I’m sure the purists will argue that there’s some magical way that’s The Right Way, or explain why adding a no-op method to nil is bad and why Base.each would be unthinkably bad. But “take a group of things and iterate over each thing” is one of the most basic things you can do in any language, and it’s fantastically dumb for me to have to handle “special cases” of there being 0 or 1 things.

Leave a Reply

Your email address will not be published. Required fields are marked *