What is good code?

A long long time ago, in an office building a couple of miles away, I was asked this question in an interview. I’m not sure how much they cared about the answer, but mine was more glib than it was useful (I got the job anyway). I misquoted Einstein and said that good code was code that was as simple as possible but no simpler.

This was not a very satisfactory answer to me, though I got the job.

For, uh, reasons this question has been on my mind recently and I think I’ve come up with an answer that satisfies me:

Good code is code that I am unlikely to need to modify but easily could if I wanted to.

Of course, good code is probably only roughly correlated with good software.

This entry was posted in programming on by .

Notes on a randomized variant of majority judgement

I like randomized voting. I like majority judgement. Naturally I’ve wondered for a while whether there is a natural randomized variant of majority judgement.

One option is to do this just by resampling majority judgement, but this doesn’t seem like a good option. You have to decide how many people to sample, and if it’s all of them then the answer is just going to asymptotically approach deterministic majority judgement.

I came up with a solution recently that I think holds a certain amount of appeal as a design, but after some analysis I think it doesn’t hold much appeal as a voting system. These are some notes on it for posterity.

The idea is as follows: People vote as in majority judgement, assigning a grades to each candidate.

We then  run the following process:

  1. If there is only one remaining candidate, they win
  2. If there are multiple remaining candidates but they all have exactly the same grades, pick one at random
  3. From each candidate, independently pick a voter and assign that candidate the grade that voter gave them (so if 90% of people ranked a candidate excellent we give them the grade excellent with 90% probability). Of the grades assigned this way, pick the highest. Any candidate which got less than that grade drops out.
  4. Return to 1 with the new set of candidates.

When I came up with this I thought it was obvious and natural and clearly the randomized way to do MJ. In reality it doesn’t track the behaviour of MJ well at all.

In particular it’s very easy to show that the relation “A beats B in a two candidate race” is non-transitive. How? You just construct grades which constitute non-transitive dice.

Further, as a result of this, it lacks the property of majority judgement that adding a new candidate to the list doesn’t affect whether A beats B (because you can add a candidate which knocks A out of the race earlier than B).

It also doesn’t track random-ballot very well I think. Consider the case with three candidates, A, B and C and three grades. Suppose candidate A has 50% good and B and C have 25% each. In random ballot that would also be the probability of them winning. In this system:

The possible outcomes for the second round are {A}, {B}, {C}, {A, B}, {B, C}, {A, C}, {A, B, C}. Let WA be the event that A wins and let \(p = P(WA)\). Then:

\[\begin{align*}
P(WA|{A}) &= 1 \\
P(WA|{B}) &= P(WA|{C}) = P(WA|{B, C})= 0\\
P(WA|{A, B}) &= \frac{3}{4}\\
P(WA|{A, C}) &= \frac{3}{4}\\
P(WA|{A, B, C}) &= p \\
\end{align*}\]

So
\[\begin{align*}
p &= \frac{1}{2}*\frac{3}{4}*\frac{3}{4} \\
& + \frac{3}{4}(2 * \frac{1}{2} * \frac{1}{4} * \frac{3}{4}) \\
& + p ( \frac{1}{2} * \frac{1}{4} * \frac{1}{4} + \frac{1}{2} * \frac{3}{4} * \frac{3}{4})\\
p &= \frac{9}{16} + \fraction{5}{16}p\\
p &= \frac{9}{11} \\
\end{align*}\]

I made multiple arithmetic mistakes in doing that, so although I’ve double checked it it could still be wrong, but it’s at least in the plausible direction: In most likely scenarios, A gets several chances to win it can either win immediately by getting a high grade and the other two getting a low grade (which happens with about a 28% chance), or it can be put into a head to head which it is likely to win (which happens with about 19% chance) or it can restart and get those chances again. So this system significantly distorts random ballot in favour of the majority.

This entry was posted in voting on by .

More book recommendations

I was recommending some reading material for a friend and it occurred to me that I should just turn it into another book recommendations blog post. So, here you go. Some books I have read recently and enjoyed.

Note: If you care, I am using affiliate links in this post. Feel free to bypass them if you object. It’s mostly by way of an interesting experiment, and doesn’t influence my recommendations at all. I expect to make all of like 50p out of it.

Non-fiction

My non-fiction reading continues to be lighter than I’d like. Two recommendations from it though:

Sexuality in medieval Europe: Doing unto others

This is exactly what it sounds like: An account of sexuality in medieval Europe. It’s interesting both in and of itself, but also in terms of how it highlights the difficulties of using a modern world view / system of categorisation when talking about the behaviour of historical societies.

I confess I’ve stalled about a third of the way into it, but I do intend to pick it up again. It’s not a problem with the book so much as a thing that usually happens when I read non-fiction books outside my area.

Mathematical methods in the theory of queuing

This is a lovely short little monograph about the basics of queuing theory. It’s only about 100 pages long, and contains a nice overview of most of the elementary results in the subject. I picked this up browsing around in Foyles and decided it would be an interesting thing to learn. It’s been about half refreshing my knowledge of Markov processes and probability and half learning the applications to queuing. If that’s a thing that interests you, I recommend this.

It could unfortunately have used a bit more proof-reading. The actual prose is fine, but some of the equations are slightly confused. I haven’t found this a major problem for understanding though, it just requires staying slightly on your toes while reading it.

Fiction

The Martian

This is a remarkably enjoyable book given how much of it is iterations on the theme of “Oh god I’m going to die. Wait. MATHS. OK maybe I’ve got a shot at this”.

Basic premise: Astronaut gets accidentally left behind on Mars with equipment designed for a 31 day mission. Has to figure out how to survive to get rescued, which involves waiting a lot longer than 31 days. It’s mostly a story about engineering and ingenuity.

On utilitarian grounds I rather think NASA should have just left him there to die, mostly because I’m a cold-hearted bastard, but it’s still a really enjoyable book.

Things of Interest

Things of Interest isn’t a book, it’s just some guy’s site. He writes good fiction though. Principle amongst it:

  • The Ed Stories (also available for free on the site if you prefer). These start out as pretty light-hearted “college guy builds giant robots in his basement” stories which take a turn for the serious later. They’re mostly mind candy, but they’re tasty mind candy.
  • Fine Structure – I’ve got to be honest, I’ve read Fine Structure twice and I’m still not exactly sure what it’s about. But the basic premise is that a being sufficiently advanced that we might as well call it a god turns our universe into a prison for its enemy. In order to do this, it regularly edits the source code of physics in order to prevent advanced technology that would allow it to escape from being developed. Interesting and worth reading.
  • Ra – Magic as science. Or possibly science as magic. It’s an ongoing story, and the big reveal is in the process of happening as I write.

Brood of Bones

(also in this series: Fox’s BrideGravity’s Revenge, Dream Storm Sea).

Hiresha suffers from Narcolepsy. This is bad for obvious reasons, but in the fantasy world she happens to live in does come with at least one compensation: Enchantment can only be performed whilst you are asleep. Thus spending a lot of time asleep causes one to accrue a lot of power. Power she can hopefully use to cure herself of her disease. In the mean-time however there are all these other people who need her help.

Interesting, if not exactly ground-breaking. I thoroughly enjoyed these. If you like adventure fantasy these are worth a read.

The best of all possible worlds

I don’t really know how to describe this book other than that it’s remarkably charming for a book about the aftermath of a world being destroyed. It’s set in a galactic civilization where all the different races are more-or-less human despite coming from different worlds. They’ve clearly been watching earth and occasionally importing humans from it (there’s a lot of reference to earth culture, and people being of Terran descent), but we appear to be mostly ignorant of this fact.

Which is mostly beside the point of the story. The premise of the story is that the homeworld of one of the human races, Sadiri, has recently been destroyed, and the survivors are trying to rebuild their civilization. Some of them come to Cygnus Beta, which is a world with an incredibly mixed background, looking for people and cultures of Sadiri descent there. Grace Delarua, a civil servant there, acts as their guide in doing so.

Half exploration story, half love story. Not really the book I expected given the premise, but definitely worth a read.

Ancillary Justice

This is a story about a woman (probably. She’s a little confused about gender and refers to everyone as “she”) who used to be a spaceship seeking revenge on the people responsible for killing her.

It’s both a very good sci-fi novel and an interesting exploration of identity. You should go read it if any of that sounds remotely appealing.

The Coldest War (The Milkweed Triptych)

Alternate history and magical Nazis. Quite good if you like that sort of thing. Fairly dark, with very few unambiguously good characters.

Among Others

Somewhere between inverted Harry Potter (a witch gets sent to a boarding school for normals), a collection of Jo Walton recommending her favourite sci-fi and fantasy novels, and magical realism.

Again, charming. I totally recommend this one if you like sci-fi and fantasy at all, even if you don’t think it sounds like you would enjoy this one.

Three Parts Dead

(also Two Serpents Rise in the same sequence)

I’ve got to be honest, between reading this interview in which the author discusses why one of his characters is a gay skeleton (answer is basically “some people are gay, some people are skeletons. These two can intersect. Get over it”) and the tagline “A God has died, and it’s up to Tara, first-year associate in the international necromantic firm of Kelethres, Albrecht, and Ao, to bring Him back to life before His city falls apart” there was no way I wasn’t going to read this series. I wasn’t disappointed.

It’s basically magic as a metaphor for economics and contract law, with gods as a metaphor for corporations. Even if you ignore that they’re well written and enjoyable stories. Strong recommendation.

I think that’s it for now from recent reading. I still need to mine a whole bunch of older stuff I’ve read for recommendations. That might happen at some point.

This entry was posted in Books on by .

A class of games I wonder if exists

So I was thinking about board game design and it occurred to me that there is an interesting class of games which I hadn’t seen any examples of. That is the set of semi-cooperative or cooperative-with-hidden-missions games.

There are pure cooperative games, like Pandemic, and there are cooperative-with-hidden-traitors games like Shadows Over Camelot or Battlestar Galactica, where you’ve got the overt goal that everyone is supposed to be working towards but some of you might be traitors trying to undermine that.

What I’m thinking about though is games where everyone is working towards a common goal but people have hidden missions within that. So the possible outcomes are basically either “you all lose” or “you have all made it through the game. Now score to determine which of you actually wins”.

The “you all lose” scenario needs to be likely enough that you are basically forced to cooperate, but you should all have hidden agendas which mean that your favoured actions while generally positive might not be strictly optimal for the group because they’re designed to further your hidden goals.

Here’s a sketch of a game exhibiting these properties:

Colony

Humanity’s first colony on an alien planet has been established. Its governance consists of a president and a council. You are that council. Your goals are:

  1. Get your colony self-sustaining before your limited supplies run out and you all die horribly
  2. Gather enough political influence that when the current president’s term is up you’re elected their successor

At the beginning of the game, each player is dealt five “alliances” cards and must pick three of them. These are groups of people who are prepared to throw certain amount of influence your way if certain conditions are met (there are probably several conditions depending on how happy they are, including a penalty condition where they actively oppose you if that baseline is not met).

Play proceeds through mechanisms I don’t feel like fleshing out right now, but you have to gather resources, build infrastructure, deal with random disaster cards, etc.

The game ends under one of various possible circumstances:

  1. You run out of a critical resource (food, water, medicine, etc) and the colony dies or is thrown into anarchy. You all lose
  2. A sufficiently catastrophic disaster occurs and you do not have the capacity to deal with it. You all lose.
  3. The current president dies (you may be able to hurry this along). An election occurs. The person with the most total influence wins the election and the game.
  4. The current president’s term is up. An election occurs as above.

So while you are all working towards the survival of the colony, exactly how that survival manifests is up to you – if the miners are one of your allies, you might be really interested in promoting heavy industry, whileas if the scientists are on your side you might want to preserve some of that wilderness for research purposes rather than digging it up. On the other hand, if you don’t build those mines you might not have the resources to fix that disaster at the power plant, and it’s a real shame you hadn’t spent more time researching the native biology when that plague hit.

This entry was posted in Games on by .

A two person agreement protocol

The problem is this: You have two people, many options. Each of them have preferences amongst those options. How do you decide which is the best one?

I mean sure you could talk to each other. But that’s way too straight forward. Also it introduces some weird biases (e.g. the person who is better at persuasion is more likely to get their way). Wouldn’t it be nice if there was a simple system which let you come to a mutually acceptable agreement?

One option for this is yootling, but it would be nice if we could do this with democracy instead of economics. But how do you democracy with only two people?

I should say, I didn’t come up with this idea. It was a question posed to me by James Aylett while we were on /dev/fort, and he also suggested the solution. The idea stuck with me, I decided to think through the implications, and I realised that this was actually a very nice way of doing things.

The solution is to apply a standard voting system called Majority Judgement, which turns out to reduce to a pleasantly simple form in the two person case. It works as follows:

  1. For each of the options, each person writes down a grade. In classic majority judgement there are six grades (Reject, Poor, Acceptable, Good, Very Good, Excellent). Personally I like to drop the “Very good” option down to 5 grades. It doesn’t matter excessively though. You can even cut it down to three if you like (Bad, OK, Good).
  2. Score each option with the smallest grade either of you assigned. So if I rated something good and you rated it bad, it’s bad. Drop everything that has a strictly worse score than some other option (so if we have two options which got scored as good, drop everything that didn’t score good).
  3. If we only have one option left, that one wins. We’ve picked the most mutually acceptable option.
  4. If we have more than one option left, repeat the same thing amongst the remaining options only this time use the highest score either of assigned.
  5. If we still have more than one option, there’s nothing to choose amongst them. Either fall back to that talking thing or pick at random.

I think this sounds more complicated than it really is. Lets do a worked example. We want to go for a drink. Our options are:

  • Land o’ beer
  • Gin gin gin
  • Ye Olde Hipster
  • Whisky for you

We each score these:

Watering hole Me You
Land o’ Beer Reject Excellent
Gin Gin Gin Excellent Bad
Ye Olde Hipster Acceptable Good
Whisky For You Excellent Acceptable

This gives us the following scores:

Watering hole Round 1 Round 2
Land o’ Beer Reject Excellent
Gin Gin Gin Bad Excellent
Ye Olde Hipster Acceptable Good
Whisky For You Acceptable Excellent

So in the first round Land o’ beer drops out (because I hate it) and Gin gin gin drops out (because you hate it). This leaves “Ye Olde hipster” and “Whisky for you” remaining because we both find it at least acceptable. In the second round, we are choosing only between these two options. I rated Whisky For You excellent, whileas you consider Ye Olde Hipster merely Good, so we go for Whisky For You. Yay, Whisky.

I’ve not actually tried this in practice, but I suspect it might work quite well. And this of course generalises naturally to more people given that it’s actually a voting system designed for more people (one caveat: When generalising to > 2 people you don’t take the smallest score: You take the middlemost, rounding to the left. So e.g. if there were three of us and we rated something as Bad, Good and Excellent respectively, the rounds would go Good, Bad, Excellent)

This entry was posted in life, voting on by .