Friday, May 19, 2017

Slytherin Software Engineering: Checked Exceptions, Java 8 Streams, and you!

There's nothing quite so Slytherin in software as functional programming. A functional program is not a heroic quest to impose order on the chaos of memory, but the evaluation of elegant mathematical and logical expressions, over data structures that can never change, with no side-effects. Is all as it ever was? Yes. Our machines are not as perfect as our ideas, but we use compilers to keep the (vaguely dirty) world of processors, instructions, and memory separate from our timeless forms. Additionally there's a bit of the esoteric: pure functional programming disallows the most basic elements of the imperative style that most people learn first, data structures like arrays and control-flow ideas like iteration. This lends itself to uniform syntax with meaning differentiated by position and hierarchy, not ugly flat structures with wide varieties of (plucky?) characters. This is serious Slytherin stuff, right here.

Alas, sometimes even a Slytherin must code in Java. Java's endemic flaw is that it never has enough features; in this it is infinitely preferable to, say, C++, which always has too many! The consequence of this flaw is that we must write lots of redundancies. A redundancy is a statement that offers no possibility for expression, but that we must write anyway. This is drudgery, mere toil, not befitting our high nature! Java has long supported some functional idioms and even anonymous classes, but using them was an exercise in managing redundant boilerplate. Java 7 helped clear some Generics-related boilerplate, but Java 8's compact lambda expressions and Streams API finally made functional expressions look good on the page.

There's just one problem: what if your lambda expression calls some function that throws a checked exception? Streams methods won't accept such a lambda! This sort of problem predates Java's lambdas and Streams, but now that functional idioms are good-looking enough to use it's a problem worth re-examining. So let's consider the possibilities.

You could handle the exception. Yeah, right. In your nice, pithy lambda? What are you, a Hufflepuff? You didn't throw that exception, it's not your responsibility. Let someone else handle it.

You could catch it, wrap it in RuntimeException, and throw that. You'd do that with a higher-order function, obviously. The downside to this is that by wrapping the checked exception you obscure its true nature to callers. That's a red flag right there (we obviously prefer green ones). Another core Slytherin value this violates is reciprocity. You had to deal with the annoyance of a checked exception and it's your right to propagate this annoyance up the call stack. But to force the caller to check arbitrary levels of getCause() and try to guess the intentions of the various wrapping layers? I'm not going to lecture you about fairness or the social contract, but think of the possible consequences: your caller could denounce your method for being imprecise. You don't want that reputation. It's just this fear that makes us civilized; do heed it.

You could wrap the exception to get it through the Streams expression, then catch it outside and unwrap it for the caller. For this you need a more specific type than RuntimeException, one that is only used for this purpose — if you wouldn't make your caller guess at the meaning of a caught RuntimeException you certainly shouldn't have to do it. This is the Golden Rule of Entitlement shared by all Slytherins, in case you were already developing a habit of skipping class by Kindergarten. So you declare such a type along with your function-wrapper and take care to use it consistently. But now your aesthetic is starting to slip. I once had such code reviewed by a Ravenclaw that commented, “That's an awful lot of control-flow just to avoid a for loop.” I could only sulk, “And an awful lot of type-names just to avoid a builder,” and take my laptop and cognac into the stairwell to rewrite the whole module, newly enlightened to the futility of striving for beauty, for unity of expression and purpose. Never have your code reviewed by a Ravenclaw.

It turns out that the best way, for a Slytherin, was invented in 2009. Do the sneakyThrow. Like it just by the sound of the name? I thought you might. You still have to wrap your lambda expression, but you don't need an extra try-catch block around the Streams expression, and the exception that comes out is exactly the type that was thrown. All you did was abuse Generics so you could make an unchecked cast and deliberately make that cast incorrectly to an unchecked type1 to fool the complier's static exception-type checks — the ends justify the means. Now you can put a (correct) throws declaration on your method, tie it up in a nice little bow, and laugh as the compiler warns you about it: how na├»ve it is, how little it knows of the dark arts.

You could also leave the throws declaration off and lift the burden of checked-exception handling from your caller. This could have real consequences: there are exception-handling schemes out there that rely on the methods they invoke to honor their declarations. Some Slytherins would say that the weakness of those that trust in the honor of others is their own fault, that we should not be blamed for doing what we must. I mean, it's just code, so do whatever, but don't be a douchebag about stuff that really matters, OK?

1 Two different meanings of “unchecked” in one sentence... oh, dear...

Saturday, May 6, 2017

Should a word have two meanings? What the fuck for?

When first I sat at the table I took all the cards at face value. It was bad enough when they gave me directions according to where the old post office used to be. Worse when I arrived there with a package to mail. “And where should I take this, the hospital?”
“Only if it's for a patient, dear.” She took a drag, stamped out her cigarette on the curb, and walked back into the bookstore.

In the town square stood a statue of a man in a military coat and cap, striding forward with anger in his face. “A civic or national hero?”
“Why, no. He led a revolution for a country that no longer exists in a land that, though it's a lot like ours, still seems a world away.”
“Then, what, he was an exponent of our ideals?”
“Well, I can't speak for everyone, but not mine. His professed ideology ridiculed political and personal freedoms from the first. When he held power he indeed suppressed these and others besides, and used racist oppression as a tool to consolidate political support. This came to be a hallmark of his country and its successors, though of course this is hardly unique in the world.”
“Then why should we keep his statue here?” The plaque below mentioned something about its artistic significance. It had been pulled from a scrap heap after the war.

A bar off the square had its own statue outside. A person holding a glass, with a motorized arm lifting the glass to drink and lowering it back down, over and over again, forever. A sticker on the door read, “Register to vote here.” Inside a drinker slapped his arm around me. He was, it turned out, in this strange city, from my home town, and that's one of our local pastimes: getting black-out drunk and hugging eachother. That's one of the reasons I left. He raised his glass and toasted a candidate for local office whose speeches were false fiction (fiction is no different from fact or prophecy, it can be false but it isn't always). I wasn't going to change his mind.

He had a landscaping practice. His best customer was always traveling for business but kept a house in a post-bohemian neighborhood up the hill from the square. He was taking classes for an Associate's Degree but was struggling with math requirements. I met an accountant once that said she never liked math, and was never good at it. Then she wondered aloud why she'd gone into accountancy! Anyway, eventually he asked me what I did for work and I didn't have a quick deflecting lie ready and it was like he knew. I have lots of quick lies ready for when my colleagues ask me about my personal life, but not for that, because they all know what I do for work! He said he'd never been so close to someone he wanted to punch in the face.

He lived a couple dozen miles south of town. Past the shipping port and the huge railyard that spreads out from a massive freight beltway, hidden to highway maps but imposing on the ground, in a glacial valley among factories and warehouses. He'd parked blocks away on a side street to avoid the specter of crime on the main drag. He was frighteningly drunk. Before my work came up and before punching-in-the-face came up he'd been laughing at my expense, or at the expense of the act I put on. Many of my lies and embellishments are self-deprecating, and I was dressed ridiculously; punch up or punch in, that's comedy. Now his sarcastic disgust was getting angrier. “I bet you take public transportation!” he sputtered. I didn't mention my bike locked up outside. I guessed at the sort of route he might take out to the freeway and made a mental note of streets to avoid on my way home.

Saturday, April 29, 2017

Current mood: 2001

It is 2001 again (or maybe 2003):

  • Mass protests have returned to mainstream consciousness
  • I am extremely “single”
  • I've messed up my body by running too fast
  • Irony's dead now, for real this time

Because it is 2001 again, I am learning to play every song on (Radiohead's classic electronic album) Kid A as an acoustic-guitar strum-along. Back then I worked out several of them on piano, and some of them really are a lot easier on piano, but I'm being stubborn and sticking to guitar. Also I didn't really know how to play piano back then and I really don't know how to play piano now.

Sunday, April 23, 2017

Why am I writing about Juicero?

There are just so many angles to this perfect exemplar of human silliness ca. 2017 (this is where I'd find a way to insert the phrase “late capitalism” if I was1 a Marxist but I'm not a Marxist; adjust per your politics).
  • Juicero is an Internet-connected “juicer” that squeezes bags of pre-chopped ingredients (sold separately) until juice comes out. You might ask what you need the Internet connectivity for, and the answer is, “Nothing.” Yet Juicero needs Internet connectivity. Not the juicer itself but the concept of such a juicer. Juicing is a dumb, overblown fad. The health benefits are dubious, and juice you buy in bottles or frozen cans is perfectly adequate — good enough to tide you over between monthly trips to that swanky juice bar in your town, if you're into that sort of thing. Only by the power of rapid, global, peer-to-peer human connection, only by the Internet, can a bunch of sparsely-distributed juice fanatics congeal into a “community”, with fresh-pressed juice becoming such a large part of their identity that they can be convinced they need this.

    Juicero the venture-capital pitch also needs Internet connectivity for a couple reasons, which only matter to a user in that they allow Juicero to exist:

    • DRM2: ensuring that only authorized Juicero juice may be juiced by a Juicero juicer3. In order to get/stay rich Juicero's founders need to be able to profit on initial sales of the machine and on continued sales of bags of chopped fruit.
      • The DRM angle includes a thin pretense of user benefit: that the machine could warn users of expired or recalled packets. I don't know, maybe you could have an online profile where you collected badges for your impressive juicing accomplishments. Whether or not you'd actually use any of that, none of it is a feature for the user — it's all there to make the company's life better, not yours.
    • Internet connectivity makes it a “tech” product, suitable to be pitched to “tech” investors. “Tech”, economically, means that you're using technological novelty to stay ahead of competition, thereby keeping profit margins high4. So there's more silly money in “tech” than in the food and beverage sector, and it's silly-arrogant money that believes it can understand all the other sectors better than the established money already in them.

  • As much as Juicero is riffing on a dumb, overblown fad to try to become a dumb, overblown fad in its own right, hating on Juicero is also a dumb, overblown fad. There's actually a product here. It's similar to the whole Keurig coffee maker thing, which has certainly taken off. Coffee from a Keurig tastes better than instant coffee from powder or crystals; the prep and clean-up is much easier than other home-brewing methods where you have to deal with grounds. On a per-serving basis it's more expensive than other home-brewing methods but cheaper than going to a cafe. On the Juicero side there's something about some frazzled dad, you get the point.

    • So about Keurig, I guess they're having trouble dealing with competitors making “cups” compatible with their machines, undercutting them on price, and killing their profit margins. Hence Juicero's need for DRM from the outset, hence their Internet-connected concept.
    • The “easy” clean-up associated with Keurig machines has an associated environmental cost. A discarded, used Keurig cup contains plastic, aluminum, and organic matter all in a tight package, making it hard to recycle or compost, even though the materials in isolation are pretty well suited to recycling or compost. A Juicero bag must be quite similar. There's the outer material of the bag, strong enough to hold under the pressure of the machine. There's some kind of filter at the bottom of the bag, where the juice comes out — if that was built into the machine it would require regular cleaning, so it must be built into the bag. And there's the organic matter, the fibrous remains of the squeezed-out fruit.
      • When I make coffee at home I use a French press, and I can attest that coffee grounds are annoying, like any other damp organic kitchen waste. When I was renting the first floor of a house with a yard I could just walk out in my PJs and sandals and dump the grounds under some plants that the Internet said would appreciate having coffee grounds dumped on them. Now I toss them in a moldy compost bin for a couple days until the stink becomes unbearable, then take that bin down three flights of stairs to the bigger, moldier compost bin on the parking level of my building. Then I have to take a shower to wash the fruit flies out of my hair.
        • Point is, the inability to compost the coffee grounds and juice waste is, perversely, a feature.
        • “Why don't you just shave your head, Al?” I did that, once, because all my cross-country teammates did it for the state meet my senior year. My head is not shaped right. It looked ridiculous, even more ridiculous than shaved heads usually look.
    • So you can squeeze a Juicero bag with your hands. So, what? I'm sure you can strategically poke holes in a K-cup and pour hot water over it just the right way and end up with coffee. Who would actually do that? Not the “frazzled dad” on his way out to dad it up at some dad thing. Not the customers in the waiting room at the vet's office5. Not the waiter, hurrying-up-and-waiting like 10 tables, and this guy is going to regret that 3:00 PM coffee order at 3:00 AM, but the customer is always right...
      • The workout you get from squeezing the Juicero bag with your hands probably has more health benefits than the juice. The problem is that as you develop strength you can only do more reps by drinking more and more juice, confusing the question of what's really responsible for your newly buff forearms...

1 This is where I'd use the subjunctive voice if I was the sort of person that used the subjunctive voice in English...

2 DRM stands for Digital Rights Management. Like many software dorks of my age, when I was younger I ranted a lot about DRM in software because it represented extra complexity toward the end of making products less useful for users. Like many software dorks of my age I've mellowed on this subject. Anyway, computing models have shifted and more important work is done server-side (“in the cloud”), where big companies naturally have control of the data and the rights. Instead of seeing further erosion in users' rights to use data they possess, we've seen the erosion of users' possession of that data, often even what they'd call “their own” data. However the legal arrangements have or haven't changed, “Possession is nine-tenths of the law,” and it's also nine-tenths of practical power, power that the big companies in charge of the cloud wield mostly for their own benefit. Oh, right, the point that I should be getting to. Now there's DRM in a juicer that I'd never own, and I'm distinctly un-mellowed about it. I guess the mellowing wasn't about age, but just about what I was accustomed to. Humans are pretty good at adjusting themselves to their conditions.

3 It's really hard for me to keep typing, “Juice,” instead of, “Guice.” Just thought you should know.

4 Remember when I said I wasn't a Marxist? Well I have a very thin understanding of competition and profit margins that mostly comes from my observations of the tech industry and what I remember from the chapters of Das Kapital I managed to get through.

5 My vet's office does have a Keurig machine in the waiting area.

Saturday, April 22, 2017

Humility / Audacity

I thought about making a poster for the Seattle March for Science today, then didn't, because I figured there would be lots of posters, and I'm not that good at slogans or visual art, better at long-winded explanations of stuff, so probably other posters would be more worthy of people's attention. Indeed, there were lots of clever posters there and I didn't take any pictures of them because I'm not good at remembering to take pictures. So instead I'm writing a blog post about the poster I maybe should have made. It would have had just two words: “HUMILITY” across the top and “AUDACITY” across the bottom.

I'm probably missing a lot of the words for this, having not studied history nor philosophy of science in any depth, being just some guy that does software and reads stuff... so this is going to be a pretty square and incomplete account of things... so maybe you think about science as an institution, or as a practice. It's an institution made of people, a practice performed by people. We often fail to live up to our best ideals, but they're still our ideals (cf. the USA). Humility in particular would be good to highlight at this sort of event, both to and from the crowd.

I didn't see either of my words anywhere. That's fine. I did see a “Make America Care Again” sign, which was the first MAGA riff I've ever liked.

Tuesday, April 18, 2017

Cold Take: Serial

I finally listened to Serial, a few years late now. One of the big things I got out of it is that I don't want to talk about “reasonable doubt” in a case where I'm not on the jury.

To talk about reasonable doubt is to implicitly question the jury's decision. It seems unfair to do that after listening to a podcast about a trial. I didn't see the whole trial! And I saw a lot of things that weren't in the trial, which were presented because they might indicate the wrong verdict was reached.

But even if I had watched a video of the whole trial and nothing else... both sides in any jury trial prepare arguments to convince the particular jury in the courtroom. In this case that meant a majority-black jury from Baltimore; the podcast audience is a global audience of NPR listeners. There's a whole episode of the podcast called The Deal with Jay, Jay being the prosecution's star witness, where host Sarah Koenig asks, “What's the deal with Jay?” She was never quite sure what to make of him, the inconsistencies in his story, or the lies and evasions he gave before admitting his role in the crime and testifying for the prosecution. So in this episode she talked with a juror, Stella Armstrong, about how she understood Jay. Stella explained, “We all have somebody in our life like that, you know, that you may know, a cousin or a relative, who, if something goes wrong, you think you can call to help you.“ The implication was that “something” was something outside the law. In this case Jay testified that Adnan, the defendant, asked him to help him cover up a murder he committed. Sarah replied that she didn't know anyone like that.

OK, I don't think I know someone like that either. Forget covering up a murder — if I commit a murder (I will try very hard not to commit a murder!) and ask you to help cover it up, don't mess around with that, slap me in the face and drag me out to face justice! But for stuff that isn't so grave... I don't really know who I'd call off the top of my head. Most of the people I'd call for advice would probably say stuff like, “Don't do anything stupid.” Some of that is a mark of privilege, that the law generally works for people like me (I've jaywalked as much as anyone...). But this trial wasn't put on for myself, Sarah Koenig, and a jury of NPR listeners. If it had been, both the prosecution and defense would have had to present somewhat different cases!

So for me, in no way am I in the position of someone asked to do what the jury is doing. I'm listening to a podcast. I just speculate about stuff.

Tuesday, March 14, 2017

Just because I'm tired of arguing with people about dumb things...

... I guess I have to state a moral philosophy of bad form and annoyance.

1. There are individual actions and there are collective actions. They are different.

2. Some individual actions have terrible results when performed often by many people. Often, here, these actions are the result of many individual decisions made in self-interest, and the result does not resemble what we'd choose collectively. Often the harm can be significantly reduced if those individual actions are simply done less often, by fewer people. Sometimes, here, we luck out of disaster because of physical or economic constraints. Other times we need to act collectively to avert disaster. Acting individually in self-interest is human. Acting collectively toward our collective best interest is a really good idea. Acting individually toward a collective best interest is usually futile. Acting individually in self-interest while hoping that others' individual actions toward a collective best interest will solve our collective problems is stupid and annoying.

3. Sometimes the demands of our world are such that we have to do things that wouldn't be good for everyone to do all the time (cf. Kant). Sometimes when we do we run up against constraints imposed on us by necessary collective actions taken to limit the harms caused by such behaviors. It is bad form, in this scenario, to complain about it.

4. We are all responsible for the consequences of our actions to some degree, in proportion to how much of a choice we really have (cf, I dunno, Camus?). Sometimes people with lots of reasonable choices choose to do things that cause a lot of harm relative to the alternatives, simply because these things benefit them. It is bad form, in this scenario, to disclaim responsibility. It is particularly annoying to disclaim responsibility on the basis that these actions or choices are popular.

OK. That's not comprehensive or absolute, but I think it's reasonable. Good enough to stand around as proof of my logical and moral consistency, in order to annoy the bejesus out of people. BE IT PROCLAIMED.