Non-Text-Based Programming and Picking Examples

Very interesting video about the non-text-based programming environment subtext. You can go watch the video, I’ll still be here.

I had to have a quick go at a better version of the Java program that he is improving.

He uses:

    enum Attack { Magic, Melee }
    int damage (Attack attack, bool surprise, int defense) {
        int power;
        switch (attack) {
            case Magic:
                power = 5;
                break;
            case Melee:
                power = 4;
                break;
        }
        int effectiveness = power * (surprise ? 3 : 2);
        switch (attack) {
            case Magic:
                if (effectiveness >= defense)
                    return effectiveness - defense;
                return 0;
            case Melee:
                return (effectiveness / defense) * 2;
        }
    }

Which isn’t actually legal java but it’s close enough.

But how about:

    enum Attack { MAGIC, MELEE }
    int damage (Attack attack, boolean surprise, int defense) {
        int m = (surprise ? 3 : 2);
        if (attack == Attack.MAGIC) {
            return Math.max(5 * m - defense, 0);
        } else {
            return ((4 * m) / defense) * 2;
        }
    }

Which reduces 21 lines to 9. It also gets rid of the main complaint – the “traffic jam” in the middle (but see Update, below).

Or even:

    enum Attack { MAGIC, MELEE }
    int damage (Attack attack, boolean surprise, int defense) {
       int m = (surprise ? 3 : 2);
       return (attack == Attack.MAGIC ? Math.max(5 * m - defense, 0) : ((4 * m) / defense) * 2);
   }

Which is pretty cryptic but only 5 lines.

I think it was a very interesting video and I’m being a bit unfair harping on about this which is kind of irrelevant. But perhaps there’s a lesson in there about picking examples. Conclude what thou wilt.

Would love to have a go with the subtext application.

Update I sent my annoying little examples to Jonathan Edwards who is the creator of subtext. He made some good points in reply (and helped me remove a redundant assertion in my code):

Thanks for your comments. Your revision avoids duplicating the switch by duplicating the multiplication operation. Imagine that single multiplication being instead 10 lines of code. One could of course then push it into a separate method which is called twice, at the cost of separating that code from its uses, and passing as arguments all the dependencies of that code. These are all reasonable trade offs. I am trying to reduce the need to make such trade offs, and keep the code simpler and more compact.

Steven Pinker at Congress Centre

Great talk taken from his new book The Stuff of Thought. I won’t cover the sophisticated and interesting arguments here (read the book!) but here are some nuggets on use of language …

Box of crayons that looks like an audience because it is in rows and tiered

Child to her father:

I don’t want the flat crayons I want the ones that look like an audience.

Groucho Marx classics:

I’ve had a great evening. But this wasn’t it.

If I held you any closer I’d be on the other side of you.

Why does a man with hair on his head have more hair than a man with hairs on his head?

Was the World Trade Centre attack on September 11th 2001 one incident or two? There was a 3.5 billion dollar lawsuit based on the interpretation of a maximum payout of $3.5 billion dollars “per incident”.

One of the reasons detectives suspected Scott Peterson of the murder of his wife was that he referred to her in the past tense before her body was found.

James Garfield‘s assassin’s remark following the death of the President of the United States from complications arising from poor medical care after being shot three months earlier:

The doctors killed him. I just shot him.

The Federal Communications Comission chose not to censure Bono over his remark that the award he was receiving was “really, really fucking brilliant” as it did not refer to a sexual act but was rather used to emphasize. Further legislation was passed as a result of this. Although the legislation goes into graphic detail over what words are not allowed it fails to make illegal the use of fucking as an emphasizing adverb.

That “damn tabernacle” and “damn chalice” are swearing in Quebecois French.

The description of a certain mode of speech as “fuck patois”. And an illustrative tirade from a soldier returning from war:

You fucking mean I’ve fucking come all the way fucking home from the fucking war to my fucking house to find my fucking wife having illicit sexual relations with my fucking neighbour?

The editor of Gourmet magazine challenged one of his journalists to bribe the maître d’s of New York’s finest restaurants $50 to seat him and his partner without reservations. It worked each and every time.