<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martin Wolf&#039;s weblog &#187; algorithms</title>
	<atom:link href="http://mwolf.net/archive/category/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://mwolf.net</link>
	<description>Software development and assorted geekery</description>
	<lastBuildDate>Sun, 08 Aug 2010 18:15:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>About chess and nuclear reactors: the case for exception handling</title>
		<link>http://mwolf.net/archive/exception-handling/</link>
		<comments>http://mwolf.net/archive/exception-handling/#comments</comments>
		<pubDate>Sun, 01 Aug 2010 10:02:48 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception handling]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://mwolf.net/archive/exception-handling/</guid>
		<description><![CDATA[The world of software development has more than its fair share of topics where people tend to have long religious discussions about the &#8220;correct&#8221; way to do something. I think this is partly because the field for some reason attracts the kind of person who enjoys a nice bout of verbal fisticuffs, and partly because [...]]]></description>
			<content:encoded><![CDATA[<p>The world of software development has more than its fair share of topics where people tend to have long religious discussions about the &#8220;correct&#8221; way to do something. I think this is partly because the field for some reason attracts the kind of person who enjoys a nice bout of verbal fisticuffs, and partly because we spend a lot of time dealing with very abstract topics where the pros and cons of a given choice have more to do with differing philosophies than with objective facts.</p>
<p>One classic topic for this kind of discussion, which came up recently at work, is the use of exceptions for error handling. Every modern programming language offers an exception mechanism for this purpose, and presumably it is there to be used. However, ever since they were first introduced, there has been a large and vocal subset of the community arguing that exceptions do more harm than good and you&#8217;ll be writing better code if you just use good old return values to report whether a method succeeded.</p>
<p><a target="_blank" href="http://www.joelonsoftware.com/items/2003/10/13.html">One representative example</a> comes from Joel Spolsky, one of my favorite authors. Another oft-quoted article making the same arguments is found in the <a target="_blank" href="http://yosefk.com/c++fqa/exceptions.html#fqa-17.1">&#8220;Frequently Questioned Answers&#8221;</a> by Yossi Kreinin. They both make the same basic points: exceptions do not reduce complexity but merely hide it, and when complexity is hidden people tend to forget about it.</p>
<p>These arguments have merit, but I still feel that (when properly used) exception handling delivers enough value to be worth the cost. So I am going to be arguing for the status quo here, for a change. Executive summary: the dangers of exceptions are real, but code readability trumps almost everything.<br /><span id="more-99"></span><br />Let&#8217;s say that we have a method of, say, 15 lines of code. This method is straightforward and easy to read and to modify. I can understand at a glance what it does; it forms a clear picture in my mind which I can easily reason about. There is just one problem with it: We haven&#8217;t added any error handling yet. For which we have basically two options: use return values, or use exceptions.</p>
<p>If we use return values, our nice clean 15-line method will suddenly blow up to easily five times as large, with lots of conditional statements and lots of ugly, repetitive boilerplate code. Such code is annoying to write and to read, easy to make mistakes in, and <i>very</i> tempting to get sloppy with. Suddenly, I can no longer hold a mental picture in my mind of what the method actually does. I&#8217;d have to go through the code line-by-line and trace all the possible code paths. Even then, I am quite likely to miss some subtlety. And since the method is now too large to fit on a screen, I should refactor it into multiple smaller methods, which requires adding even more errorhandling boilerplate.</p>
<p>On the other hand, if I use exceptions instead, most of my methods will become only a little larger, and all of the errorhandling code will be nicely contained in one place where I can reason about it separately. The rest of the code stays nice and clean and easy to understand at a single glance.</p>
<p>&#8220;Aha!&#8221; say the exception skeptics (exceptics?). &#8220;But that reduced complexity is only illusionary! If every line of code in your program might thrown an exception, you still have an enormous amount of possible code paths to worry about &#8212; they&#8217;re just not explicitly visible in your source code anymore, which is even worse!</p>
<p>Well yeah, that&#8217;s true. I still have to worry about those exceptions even if I can&#8217;t see them. But the nice thing is, I <i>can</i> think about them, because we are now back in the scenario where I have just 15 lines of code which I can easily turn into a mental picture which I can reason about. I can look at that code and ask myself: &#8220;What happens if an exception is thrown on this line?&#8221; &#8220;Will the resource I allocate on line 4 be properly cleaned up in all possible scenarios?&#8221; &#8220;If this function fails because of an invalid input file, how will we recover from that?&#8221; And the big difference is that I can reason about these questions <i>at a much higher level of abstraction</i>.</p>
<p>It&#8217;s like the difference between an amateur chess player and an expert. When a novice player thinks about his next move, he works his way through the possible options as a computer would: &#8220;I could do this, but then my opponent will do either that or that. Or I could do this, but then she will do &#8230;&#8221; This is not a good way to play chess, since there are a lot of different combinations and the human brain is really bad at systematically working its way through such a huge decision tree.</p>
<p>An expert chess player, on the other hand, thinks at the level of strategy and tactics, rather than individual moves. &#8220;I am on the offensive, but my opponent has stronger control over the center field. It looks like I could attack over the left flank and win a few pawns, but I should probably do something about that exposed bishop first..&#8221; Only after analyzing the board at this high level, will the expert identify a small number of specific opportunities and threats to be investigated at the level of individual moves and counter-moves.</p>
<p>From a given starting position, the number of moves and counter-moves available to the expert is the same as to the novice. But by thinking about the game at a high level of abstraction first, the expert can dismiss the vast majority of possibilities at a glance, without conscious thought, and focus on the half-dozen interesting cases which need to be investigated in detail. It <i>could</i> happen that the expert overlooks some subtle trick which the plodding novice would have discovered, but the smart money will bet against it.</p>
<p>(As an aside, research has shown that experts are much better than  novices at memorizing the position of the pieces of a chess board, but  only when dealing with a position from a real game. When the pieces are  placed on the board randomly, the experts are not much better at  memorizing them than people with no knowledge of chess at all.)</p>
<p>I am hardly a chess master, but I&#8217;m fairly good at reading code. When I try to read a large block of ugly code where every &#8220;real&#8221; function call is followed by a dozen lines of boring error handling code, I feel like the novice chess player in the story above, missing the forest for the trees. When reading a nice clean piece of code where the error handling is provided through exceptions, I feel like the expert player, spending most of my time thinking at a higher level of abstraction. The complexity of the situation is the same either way, but now I have a mental picture of the situation which allows me to visualize it from different angles, dismiss all of the boring straightforward cases at a glance, and quickly zoom in on the ones which require my detailed attention.</p>
<p>At this point, assuming you&#8217;re still reading, you are probably either nodding along with me or you&#8217;re getting red in the face and starting to sputter in protest. If you&#8217;re nodding along, that likely means you belong to the camp which views programming as an essentially artistic activity, where intuition and the undefinable concept of &#8216;elegance&#8217; play a big role. You believe that there is an unprovable, but very real, connection between the subjective beauty of a piece of code and the chance that it is correct.</p>
<p>On the other hand, if you&#8217;re sputtering and getting angry, you probably belong to the camp which feels that software development is an engineering discipline in which feelings and emotions have no place. Your standpoint is that if a piece of code has 200 different possible execution paths, then somebody should bloody well go over all of those 200 paths and verify that each of them is correct &#8212; anything else is sloppy and amateurish and would never be allowed in your team.</p>
<p>And to be fair, I have a lot of sympathy for that standpoint, as I like to think of myself as a left-brain type of person as well. In fact, if I were in charge of developing the control software for a nuclear reactor, I would change sides: I would disallow exceptions, make sure that all conditionals are written out explicitly, and then use a variety of formal proof techniques to guarantee the correctness of every single path through the code. My team&#8217;s productivity may be less than one line of code per day, but there will be no mushroom clouds on my watch!</p>
<p>At this point, the people in the second group puff up their chest and say, well maybe you think that <i>your</i> project is not important enough to be worth using proper software engineering techniques on, but <i>I</i> take pride in my work and all my code is written to the same level of quality as a nuclear reactor! Well, maybe. Good for you. In the meantime, I have a deadline to meet.</p>
<p>So, dear reader, to determine which of these two camps you believe yourself to be in: when was the last time your used mathematically rigorous formal proof techniques to validate your program&#8217;s correctness? Yeah, me neither. Well, since we have just found out which camp you are <i>not</i> in, you had better make sure that your code is readable and elegant enough to be able to reason about it intuitively. It&#8217;s the only chance you have of delivering reasonably bug-free code at an acceptable level of productivity.</p>
<p>(Oh, and adding some automated testing won&#8217;t hurt either way. As Donald Knuth once famously wrote: &#8220;Beware of errors in the above code; I have only proved it correct, not tested it.&#8221;)</p>
]]></content:encoded>
			<wfw:commentRss>http://mwolf.net/archive/exception-handling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Paying with bits</title>
		<link>http://mwolf.net/archive/paying-with-bits/</link>
		<comments>http://mwolf.net/archive/paying-with-bits/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 21:52:40 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://mwolf.net/archive/paying-with-bits/</guid>
		<description><![CDATA[In the course of a friendly discussion with Dirk-Jan, I&#8217;ve been reading up on the miracles of digital cash.
And by digital cash, I do not mean lame stuff like Paypal, which is basically just an ordinary bank account to which you can send transfer orders. No, what I&#8217;m interested in is the real heavy stuff, [...]]]></description>
			<content:encoded><![CDATA[<p>In the course of a friendly discussion with <a href="http://www.djcbsoftware.nl/ChangeLog/" target="_blank">Dirk-Jan</a>, I&#8217;ve been reading up on the miracles of digital cash.</p>
<p>And by digital cash, I do not mean lame stuff like <a href="http://paypal.com/" target="_blank">Paypal</a>, which is basically just an ordinary bank account to which you can send transfer orders. No, what I&#8217;m interested in is the real heavy stuff, whereby you have a digital wallet full of cryptographic &#8220;coins&#8221; which can be transfered from one party to another, without a single central entity keeping track of the contents of your wallet. Ideally, you want to be able to transfer such coins even off-line, without the central entity needing to be involved with every individual transfer.</p>
<p>There are a couple of basic problems with the idea of using bits as money, which any &#8220;crypto cash&#8221; system will need to find solutions for.<br />
<!-- more --><span id="more-78"></span></p>
<p>Probably the biggest one is non-technical: the fact that governments really hate it when you propose something like an untraceable, anonymous and decentralized currency which is not under their control. Ask Douglas Jackson, founder of E-Gold, who in <a href="http://www.wired.com/threatlevel/2009/06/e-gold/" target="_blank">this Wired article</a> is painted as a naive but basically well-intentioned and idealistic guy who tried to create a currency for the twenty-first century but who ran afoul of a whole lot of government objections. But let&#8217;s ignore the political angle for now, and look into the technical obstacles.</p>
<p>The first technical problem is obvious: if my coins are just strings of bits, what is preventing me from spending a single coin multiple times, or minting my own coins?</p>
<p>And then there is an issue which is somewhere mid-way between technical and legal/practical: where does the money get its value from? Here in the real world, it used to be that money was backed by gold or silver: in theory, you could take your bank note to the bank and they would exchange it for a little sliver of precious metal. Nowadays, pretty much all money is fiat currency: it has value because the government says it has value, which works as long as we all believe that it does. The latter is a bit less intellectually satisfying than the former, but it does raise the interesting question of whether you could create a working fiat currency from scratch, without being backed by any government or central trusted organization.</p>
<p>When it comes to sophisticated cryptographic techniques for digital money, the work of <a href="http://en.wikipedia.org/wiki/David_Chaum" target="_blank">David Chaum</a> is the acknowledged gold standard (sorry). His digital money is a brilliant combination of pretty much all of the most advanced techniques in cryptography: blind signatures, commitment schemes, zero-knowledge proofs and lots more. Particularly brilliant is his solution to the &#8220;double spending problem&#8221; mentioned above. In his approach, people cryptographically sign digital money orders with their name when they receive them from the bank, but they sign them in such a way that the information can only be retrieved when a money order is spent twice. The spending (which can be done off-line) will succeed, but when the bank receives the same &#8220;coin&#8221; back through two different channels, it will not only know that cheating has occurred, but it can retrieve the name of the cheater! However, as long as you spend each coin only once, your anonymity is safe from the bank as well as from the people you exchange money with, even if they all work together!</p>
<p>Chaum&#8217;s work requires some seriously advanced crypto, though. In the book <a href="http://www.daviddfriedman.com/Future_Imperfect.html" target="_blank">Future Imperfect</a> from my favourite author David Friedman, there is an <a href="http://www.daviddfriedman.com/Future_Imperfect/Chapter6.html" target="_blank">alternative scheme</a> which is a lot less sophisticated, but just as elegant in its impressive simplicity. Here is the entire system:</p>
<ol>
<li>The bank gives out coins, which are simply large randomly-generated numbers.</li>
<li>When you receive a coin, you immediately send an (anonymous, encrypted) message to the bank, requesting that the coin&#8217;s number be changed to a different random value generated by you. You include a transaction code which is another random number.</li>
<li>If the bank can indeed find the original number in its database, it exchanges it for your new number, and publishes the transaction code in a public place so that you can verify that the coin you were given did indeed exist, and has been assigned the new number which only you know.</li>
<li>When you walk into the bank and present them with the new number, they will exchange it for, say, an amount of gold matching the value of the coin.</li>
</ol>
<p>That&#8217;s all!</p>
<p>It&#8217;s not quite as secure as Chaum&#8217;s system in all directions. The system can be used off-line, but only if the payee trusts the payer to give him a real coin. The bank cannot breach your anonymity, but it can cheat by refusing to honour a valid coin. Even in the case of an on-line transaction, if the payee claims that the payer did not give him a valid coin, there is no way for an outsider to determine which of the two is telling the truth. But within these limitations, the basic functionality works and it&#8217;s a rather impressive achievement for a system which is so trivially simple that you could explain it to your ten-year-old nephew.</p>
<p>If you&#8217;re looking for a nice little intellectual challenge, think about Friedman&#8217;s proposal a little further. Identify the various practical problems with it, and see if you can come up with a solution for each of them, using basic crypto techniques such as symmetric and asymmetric encryption, digital signatures and secure hash codes.</p>
<p>Now, in the above approaches, the assumption is that there is still a central bank, trusted by all parties, which gives out the money and which gives that money its value by pledging to exchange it for some real-world valuable material such as gold, or perhaps simply by linking it to an ordinary bank account containing ordinary government-backed euros or dollars.</p>
<p><a href="http://www.bitcoin.org/sites/default/files/bitcoin.pdf" target="_blank">Bitcoin</a> goes even further: it proposes (there exists a technical implementation, but they&#8217;re not really pretending that it&#8217;s ready for real-world use) a form of digital money which is completely decentralized: there is no central entity, anybody can create money and then spend it. As the FAQ explains it:</p>
<blockquote><p>What is Bitcoinâ€™s value backed by?</p>
<p>Bitcoin is valued for the things it can be exchanged to, just like all the traditional paper currencies are.</p>
<p>When the first user publicly announces that he will make a pizza for anyone who gives him enough Bitcoins, then he can use Bitcoins as payment to some extent &#8211; as much as people want pizza and trust his announcement. A pizza-eating hairdresser who trusts him as a friend might then announce that she starts accepting Bitcoins as payment for fancy haircuts, and the value of the Bitcoin would be higher &#8211; now it would be backed by pizzas &#8221;and&#8221; haircuts. When Bitcoins have become accepted widely enough, he could retire from his pizza business and still be able to use his Bitcoin-savings.</p></blockquote>
<p>That seems to make sense &#8212; money is basically a system of transferable IOUs whereby the participants agree to cover each other&#8217;s debts. However, if anybody can create Bitcoins and spend them anonymously, how can they ever be made to make good on their debts? And even without anonymity, what prevents me from creating more Bitcoins than I will ever be able to honour and then living like a king until the day of my death? Somewhere, it seems, there has to be a mechanism to create some artificial scarcity in order for the scheme to work.</p>
<p>Bitcoins proposes to solve this problem by making each coin represent a <a href="http://en.wikipedia.org/wiki/Proof_of_work" target="_blank">proof of work</a>: creating a Bitcoin requires a large amount of CPU power; anybody who receives a coin from you can verify that you have performed a very complex and time-consuming calculation. Coins are linked together and there is a peer-to-peer system to verify that coins are not being double-spent. As with Friedman&#8217;s approach, you can accept coins off-line if you want but then you will need to trust the payer to not deliberately cheat on you.</p>
<p>This way, the amount of Bitcoins in the economy is limited and grows predictably, which are two very important requirements for a usable currency. Using CPU power to back the money&#8217;s value is of course completely arbitrary, but in principle that is not an impediment for a working currency. As with a conventional fiat currency, once the pool of people willing to accept the money is large enough, it is resistant to occasional cheaters; anybody who refuses to play along is hurting themselves more than they are hurting the people who partake in the system. Obviously, getting to that point is the difficult part.</p>
<p>By the way, if you think that the above schemes are not secure enough to work in practice, what would you think of the following system?</p>
<ul>
<li>Your bank account is represented by a single sixteen-digit number, which remains the same for several years.</li>
<li>Anybody who knows this number can use it to withdraw money from your bank account.</li>
<li>Making a payment consists of giving the number to the person you want to pay. So if you want to purchase something at a gas station, for example, you show a card with the number to the gas station attendant, after which he knows your number and you will have to trust him not to abuse it.</li>
</ul>
<p>That must be the most horrible system anybody could possibly come up with, right? If you proposed this in an introductory computer security class for high-schoolers, your classmates would laugh you out of the room. Nobody with an ounce of common sense would every use such a horribly botched system for <em>anything</em>, let alone for making payments over the Internet, right?</p>
<p>And yet what I am describing here is, of course, the humble credit card.</p>
<p>I do not expect any of the cryptographic systems described above to become popular any time soon. But the most likely reason for their failure will be a combination of chicken-and-egg effect (nobody wants to use a currency which nobody else uses) and the first, non-technical problem mentioned at the start of this article. Nonetheless, it is awfully cool to consider what is possible in principle, and a good mental exercise to consider the various practical objections and try to come up with solutions to them.<br />
<a class="performancingtags" rel="tag" href="http://technorati.com/tag/david%20friedman"><br />
</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=0bfda2a3-9717-8e43-97d5-80b05be56cac" alt="" /></div>
]]></content:encoded>
			<wfw:commentRss>http://mwolf.net/archive/paying-with-bits/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
