Archive for December, 2011

On mutable state

I’m working on a talk called “Functional Programming for Ruby Programmers”. While doing so, I’ve somewhat changed my opinion about mutable state. Here’s my argument, for your commentary. Being new, it’s probably at best half-baked.

  1. Back in the early days of AIDS (the disease), I remember a blood-supply advocate saying “You’ve slept with everyone that everyone you’ve slept with ever slept with.”

    The nice thing about immutable state is that it stays virginal and knowable. It is what it was when it was created. With mutable state, it’s more like “Your code might be infected by any code that ever touched your data before (or after!) you first hooked up with it.”

    However, I don’t personally find that a huge problem in programming, debugging, or understanding code. There are other problems I’d rather see fixed first.

  2. It’s often said that “code with immutable state is easier to reason about”. I realized a long time ago that there’s some sleight-of-common-language going on there, like the way people who wanted code to be less branchy got rhetorical leverage by renaming branchiness “complexity“.

    In the claim, “reason about” is (I believe) being taken to be synonymous with “prove theorems about”. Pace a whole long trend in artificial intelligence, I don’t believe theorem proving is the basis for, nor a good analogy to, reasoning. Thus I took the “reason about” statement to be a sort of solipsistic wankery from people with roots in the theorem-proving community, not an argument that should sway me, Pragmatic Man!, who left the world of proofs-of-design-correctness around 1984.

    What I’d forgotten is that optimization is theorem proving. One can only optimize if there’s a proof that the optimized code computes the same result as the original. If you consider the desire to implement, say, lazy sequences efficiently, you see how the guarantees that immutability gives are important. Ditto for automatically spreading work to multiple cores.

  3. So:

    My previous attitude was pretty much “Yeah, I have a preference for avoiding mutable state. Being immutable isn’t a huge win, but as long as I have people providing me with data structures like zippers, it’s not really any harder than fooling with mutable state. Still, I don’t see why I need an immutable language. If I don’t want to mutate my hashmaps, I just won’t mutate my hashmaps.”

    My new attitude is: “Oh! I see why I need an immutable language.”

I still don’t make a huge deal about immutability. Its benefit is greater than its cost, sure, but it’s not the Thing That Will Save Us.

Here are three Ruby functions…

Here are three Ruby functions. Each solves this problem: “You are given a starting and ending date and an increment in days. Produce all incremental dates that don’t include the starting date but may include the ending date. More formally: produce a list of all the dates such that for some n >= 1, date = starting_date + (increment * n) && date < = ending_date.

Solution 1:

Solution 2:

Solution 3 depends on a lazily function that produces an unbounded list from a starting element and a next-element function. Here’s a use of lazily:

As a lazy sequence, integers both (1) doesn’t do the work of calculating the ith element unless a client of integers asks for it, and also (2) doesn’t waste effort calculationg any intermediate values more than once.

Solution 3:

The third solution seems “intuitively” better to me, but I’m having difficulty explaining why.

The first solution fails on three aesthetic grounds:

  • It lacks decomposability. There’s no piece that can be ripped out and used in isolation. (For example, the body of the loop both creates a new element and updates an intermediate value.)

  • It lacks flow. It’s pleasing when you can view a computation as flowing a data structure through a series of functions, each of which changes its “shape” to convert a lump of coal into a diamond.

  • It has wasted motion: it puts an element at the front of the array, then throws it away. (Note: you can eliminate that by having current start out as exclusive+increment but that code duplicates the later +=increment. Arguably, that duplicated increment-date action is wasted (programmer) motion, in the sense that the same action is done twice. (Or: don’t repeat yourself / Eliminate duplication.))

The second solution has flow of values through functions, but it wastes a lot of motion. A bunch of dates are created, only to be thrown away in the next step of the computation. Also, in some way I cannot clearly express, it seems wrong to stick the inclusive_end between the exclusive_start and the increment, given that the latter two are what was originally presented to the user and the inclusive_end is a user choice. (Therefore shouldn’t the exclusive_start and increment be more visually bound together than this solution does?)

The third solution …

  • … is decomposable: the sequence of dates is distinct from the decision about which subset to use. (You could, for example, pass in the whole lazy sequence instead of a exclusive_start/increment pair, something that couldn’t be done with the other solutions.)

  • … eliminates wasted work, in that only the dates that are required are generated. (Well, it does store away a first date — excluded_start — that is then dropped. But it doesn’t create an excess new date.)

  • … has the same feel of a data structure flowing through functions that #2 has.

So: #3 seems best to me, but the advantages over the other two seem unconvincing (especially given that programmers of my generation are likely to see closure-evaluation-requiring-heap-allocation-because-of-who-knows-what-bound-variables as scarily expensive).

Have you better arguments? Can you refute my arguments?

I’m trying to show the virtues of a lazy functional style. Perhaps this is a bad example? [It’s a real one, though, where I really do prefer the third solution.]

TDD Workflow (Sinatra / Haml / jQuery) Part 1

Introduction

This is a draft. Worth continuing the series? Let me know.

Critter4Us is a webapp used to schedule teaching animals at the University of Illinois Vet School. Its original version was a Ruby/Sinatra application with a Cappuccino front end. Cappuccino lets you write desktop-like webapps using a framework modeled after Apple’s Cocoa. I chose it for two reasons: it made it easy to test front-end code headlessly (which was harder back then than it is now), and it let me reuse my RubyCocoa experience.

Earlier this year, I decided it was time for another bout of experimentation. I decided to switch from Cappuccino to jQuery, Coffeescript, Haml because I thought they were technologies I should know and because they’d force me to develop a new TDD workflow. I’d never gotten comfortable with the testing—or for that matter, any of the design—of “traditional” front-end webapp code and the parts of the backend from the controller layer up.

I now think I’ve reached a plateau at which I’m temporarily comfortable, so this is a good time to report. Other people might find the approach and the tooling useful. And other people might explain places where my inexperience has led me astray.
(more…)

Anarchy of Evasion

“The anarchy of evasion” is a term that (I think) I read a few years ago. I occasionally search for it, but I’ve never been able to find anything about it again. As I recall, the idea is a reaction to the fact that an anarchist movement that forms an independent new society or transforms an existing society is… hard. So an alternative is to live within an existing society but try to make as many as possible of your activities and interactions run according to the principles you wished the whole society ran with. You behave as you wish, but in a way and context that increases the chance you won’t be noticed and therefore stopped.

The idea appeals to me, perhaps because I’ve always liked the idea of complex and interesting things happening unnoticed in the cracks. Probably a result of reading The Littles as a kid and books like You’re All Alone and Neverwhere later.

Parallels with things like open source, intentional communities, squatting, and freaks and geeks in US high schools are left as an exercise for the reader.

If anyone has links to the idea, let me know.


  • Buy Cheapest buy viagra meds online Now Special Prices For buy viagra meds online! Best Online.
  • Buy Cheap natural alternative to viagra Online Low Prices. Cheap Prescription Drugs.
  • Buy Cheap cheap viagra soft Online Online Prices For cheap viagra soft! Best Prices.
  • Buy Cheap natural alternative to cialis Now Free Viagra Pills! Online Prices For natural alternative to cialis!
  • Buy Cheap cialis prescriptions Now No Prescription Needed For Drugs. Best Prices.
  • Buy Cheap viagra female Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap cheap online viagra Online The Largest Internet Pharmacy. Best Online.
  • buy cialis cheap Online Without Prescription Low Prices. Pharmacy Store.
  • Buy Cheapest cialis 30mg Now Best Online. The Largest Internet Pharmacy.
  • cheep daily cialis Online Without Prescription No Prescription Online Pharmacy.
  • Buy Cheap blockers and alpha levitra Now WorldWide Shipping. Discount Online Pharmacy.
  • Buy Cheapest levitra on sale Now WorldWide Shipping. No Prescription Needed.
  • Buy Cheap free sample pack of viagra Now Cheap Pharmacy Online. Online Medical Shop.
  • cialis dosage rate Online Without Prescription Low Prices. Best Drugstore.
  • Buy Cheap levitra sales uk Now All Medications Are Certificated! Best Prices.
  • Buy Cheap soft cialis with mastercard Now Pharmacy Store. Drugs, Health And Beauty.
  • Buy Cheap cialis online Online Best Prices. Online Prices For cialis online!
  • Buy Cheapest levitra and alchol Now Pharmacy Store. Pharmacy At The Best Price!
  • Buy viagra side effect Without Prescription Doctor. Best Internet. Best Prices.
  • Buy Cheapest cialis and levitra viagra online brand Now Low Prices. Internet Prices For cialis and levitra viagra online brand!
  • Buy Cheapest cialis 10 20 mg Online Best Internet. Discount Online Pharmacy.
  • Buy Cheapest levitra for sale Now Cheap Pharmacy Online. WorldWide Shipping.
  • generica cialis Online Without Prescription Best Prices. Best Drugstore.
  • Buy Cheap sildenafil viagra Online Online Medical Shop. 24/Online Pharmacy.
  • Buy Cheap levitra without a prescription Now Cheap Online Pharmacy. Free Viagra Pills!
  • levitra cheapest Online Without Prescription Best Prices. Free Viagra Pills!
  • Buy buy sublingual viagra online Without Prescription Doctor. Low Prices. Best Online.
  • Buy Cheap viagra paypal Now No Prescription Needed. WorldWide Shipping.
  • Buy Cheap viagra propecia propecia xenical xenical celebrex Now Discount Pharmacy Online. Pharmacy Store.
  • Buy Cheap pharmacy viagra Now Online Medical Shop. Cheap Pharmacy Online.
  • Buy Cheapest cialis levitra pharmacy viagra Online Guaranteed Shipping. Best Online.
  • Buy Cheap buy viagra cialis levitra Now Best Online. Top Online Pharmacy Supplier.
  • Buy Cheap mexican generic cialis Now Buy Medications Online. Cheap Pharmacy Online.
  • Buy Cheap who should take cialis 5mg Now Safe And Secure Payment System. Low Prices.
  • Buy Cheapest how viagra works Now 24/Internet)(safe Pharmacy. Best Prices.
  • Buy Cheapest cialis and diazepam overnight delivery possible Online Best Online. Cheap Prescription Drugs.
  • Buy Cheapest viagra levitra cialis apcalis regalis zenegra Now Best Drugstore. Online Medical Shop.
  • Buy Cheapest viagra switzerland Online Best Prices. Cheap Prescription Drugs.
  • Buy Cheapest lowest cialis Now Discount Pharmacy Online. Best Online.
  • Buy Cheap mail order cialis Now Best Online. Order Cheap Meds Without Rx.
  • Buy Cheap buy cheap cialis generic levitra viagra Now Drugs, Health And Beauty. Free Viagra Pills!
  • Buy Cheapest prices cialis 120 Now Low Prices. 100% Satisfaction Guaranteed.
  • Buy Cheap cialis erectile disfunction Now Top Online Pharmacy. 24/Online Pharmacy.
  • Buy Cheap cialis online fda Online Low Prices. Pharmacy At The Best Price!
  • Buy Cheap does levitra allow for good experience Now Buy Medications Online. WorldWide Shipping.
  • Buy Cheap viagra for sex Online Low Prices. Safe And Secure Payment System.
  • Buy Cheapest discount brand viagra by pfizer Now Best Drugstore. Internet Prices For discount brand viagra by pfizer!
  • Buy Cheap buy cheap generic cialis Now Best Prices. All Medications Are Certificated!
  • Buy Cheap buy viagra in england Now Top Online Pharmacy Supplier. Best Prices.
  • Buy Cheapest viagra and cialis cheap Online Best Drugstore. Buy Medications Online.
  • Buy Cheap discount generic viagra Now Low Prices. 100% Satisfaction Guaranteed.
  • Buy Cheapest mail order for viagra tablets Online Best Online. Guaranteed Shipping.
  • Buy Cheapest original cialis Online Top Online Pharmacy. Free Viagra Pills!
  • Buy Cheapest viagra manufacturer Online Best Prices. Drugs, Health And Beauty.
  • Buy Cheapest cialis used for Now Top Online Pharmacy. Free Viagra Pills!
  • Buy Cheapest india generic cialis Now Safe And Secure Payment System. Low Prices.
  • Buy Cheapest cialis buy it online huge discount Online Best Internet. Cheap Pharmacy Online.
  • Buy Cheapest viagra from uk Now Drugs, Health And Beauty. Best Prices.
  • Buy Cheapest risperodol interactions with cialis viagra levitra Now Special Prices For risperodol interactions with cialis viagra levitra! Best Prices.
  • Buy Cheap purchase viagra in australia Now Low Prices. 24/Internet)(safe Pharmacy.
  • Buy Cheapest levitra coupon Online Best Drugstore. Top Online Pharmacy.
  • Buy Cheap cialis cheaply Online WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap what does cialis do Now Order Cheap Meds Without Rx. Best Prices.
  • Buy viagra wholesale Without Prescription Doctor. Best Internet. Low Prices.
  • Buy Cheap what is levitra Now No Prescription Online Pharmacy. Low Prices.
  • Buy Cheap buy cialis online Online Special Prices For buy cialis online! Best Internet.
  • Buy Cheapest cialis softtabs Online Buy Medications Online. Best Online.
  • Buy Cheapest canadian pharmacy levitra Online Pharmacy Store. Free Viagra Pills!
  • Buy Cheap taking half a 20mg cialis Online Guaranteed Shipping. Best Drugstore.
  • Buy Cheapest levitra experiences Online Buy Medications Online. Best Drugstore.
  • Buy Cheap drinking and viagra Online Best Online. Discount Online Pharmacy.
  • Buy Cheap viagra orgasm Online Online Prices For viagra orgasm! Best Drugstore.
  • Buy chinese viagra Online Without Prescription. Best Drugstore. Low Prices.
  • buy canada levitra Online Without Prescription Cheap Meds Without Prescription.
  • Buy Cheapest us pharmacy cialis Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap cialis buy on line Online WorldWide Shipping. Online Medical Shop.
  • Buy Cheapest women viagra Now Best Internet. Order Cheap Meds Without Rx.
  • Buy Cheapest viagra cost Now Safe And Secure Payment System. Low Prices.
  • Buy Cheap viagra for sale without a prescription Now 24/Online Pharmacy. Cheap Online Pharmacy.
  • Buy Cheapest viagra sales mexico Online No Prescription Needed. Best Online.
  • Buy Cheapest levitra faq Now Low Prices. Pharmacy At The Best Price!
  • Buy Cheapest cialis us Online Drugs, Health And Beauty. Low Prices.
  • Buy Cheap cialis compare viagra Now Low Prices. Top Online Pharmacy Supplier.
  • Buy Cheapest blood pressure and viagra Now Pharmacy At The Best Price! Low Prices.
  • Buy Cheapest bayer levitra cheapest price online pharmacy Now Best Online. No Prescription Needed.
  • Buy Cheapest pfizer viagra patent Now Best Prices. No Prescription Needed.
  • Buy Cheapest uk cialis supplier Now Low Prices. Pharmacy At The Best Price!
  • Buy Cheap viagra pay by e-check Now Best Prices. 24/Internet)(safe Pharmacy.
  • legal viagra Online Without Prescription Best Prices. Pharmacy Store.
  • Buy Cheap viagra purchase uk Now Guaranteed Shipping. No Prescription Needed.
  • Buy Cheap viagra mail order Online Pharmacy Store. Guaranteed Shipping.
  • Buy Cheap buying viagra online uk Now No Prescription Needed. Cheap Online Pharmacy.
  • Buy Cheap free levitra sample Now Pharmacy At The Best Price! Free Viagra Pills!
  • Buy viagra cheap prescription Online Without Prescription. Best Prices. Best Online.
  • Buy Cheap levitra or viagra Now Cheap Online Pharmacy. Guaranteed Shipping.
  • Buy Cheapest levitra prescription drugs Now Pharmacy Store. Online Prices For levitra prescription drugs!
  • Buy Cheap cialis dosage and uses Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheapest viagra half price pharmacy Online Low Prices. Drugs, Health And Beauty.
  • Buy Cheap viagra levitra Now The Largest Internet Pharmacy. Best Prices.
  • Buy substitute for viagra Without Prescription Doctor. Low Prices. Best Drugstore.