<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Software designer &amp; deep thinker.
Follow at @MichaelDrogalis</description><title>@MichaelDrogalis</title><generator>Tumblr (3.0; @michaeldrogalis)</generator><link>http://michaeldrogalis.tumblr.com/</link><item><title>A Conjecture About Good Design</title><description>&lt;p&gt;I&amp;#8217;ve been doing some radically different design after creating &lt;a href="https://github.com/MichaelDrogalis/dire"&gt;Dire&lt;/a&gt; a couple of months ago. &lt;a href="http://michaeldrogalis.tumblr.com/post/40181639419/try-catch-complects-we-can-do-so-much-better"&gt;My stance on local exception handling&lt;/a&gt; departs from what almost anyone else is doing right now - and there&amp;#8217;s been some interesting repercussions because of it.&lt;/p&gt;
&lt;p&gt;The style of design that I&amp;#8217;ve been pushing is one of airtight separation of concerns. Clojure&amp;#8217;s runtime capabilities have allowed me to transparently alter control flow for exception handlers, assertions, and hooks. The beauty is that all the machinery to perform the aforementioned logic can be grouped into their own files - and &lt;a href="http://michaeldrogalis.tumblr.com/post/46560874730/beautiful-separation-of-concerns"&gt;loaded only when you want them&lt;/a&gt;. It&amp;#8217;s like dependency injection at a far more flexible level.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;After a few weeks of applying these concepts to a larger code base, I&amp;#8217;m noticing an interesting phenomena at the code level. Rich Hickey noted that design is about &lt;a href="http://www.youtube.com/watch?v=f84n5oFoZBc"&gt;taking the problem apart&lt;/a&gt; as much as possible. My conjecture is that when you&amp;#8217;re aggressive about dismantling the problem space, the code in each module will display similar visual characteristics.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;Here are excerpts of some files in the (closed source) project I&amp;#8217;m working on. I unimaginatively changed the functions to be about cows and chickens - but you get the point. We&amp;#8217;re concerned about structural appearance - not what the code actually does.&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;This first one is a file whose &lt;em&gt;only&lt;/em&gt; concern in the world is logging. Notice how all the forms pretty much look the same.&lt;/p&gt;
&lt;script src="https://gist.github.com/MichaelDrogalis/5326714.js" type="text/javascript"&gt;&lt;/script&gt;&lt;p&gt;&lt;br/&gt;Here&amp;#8217;s a bit of a file whose &lt;em&gt;only&lt;/em&gt; concern is reacting to thrown exceptions. They all look pretty similar!&lt;/p&gt;
&lt;script src="https://gist.github.com/MichaelDrogalis/5326709.js" type="text/javascript"&gt;&lt;/script&gt;&lt;p&gt;&lt;br/&gt;Yet another instance - a file whose &lt;em&gt;only&lt;/em&gt; job is to strip information off incoming API requests and pass them along for further destructuring:&lt;/p&gt;
&lt;script src="https://gist.github.com/MichaelDrogalis/5326705.js" type="text/javascript"&gt;&lt;/script&gt;&lt;p&gt;Last example. All of the Datomic queries get their own file:&lt;/p&gt;
&lt;script src="https://gist.github.com/MichaelDrogalis/5326700.js" type="text/javascript"&gt;&lt;/script&gt;&lt;p&gt;All of the functions in each file are &lt;em&gt;participating&lt;/em&gt; at the same level of abstraction, and hence look roughly the same. There&amp;#8217;s no surprise conditionals or loops any of them. They all flow together, and can be reasoned about with ease. This is a serious reduction in cognitive complexity.&lt;/p&gt;
&lt;p&gt;If I could make an analogy, it&amp;#8217;s a bit like a pipe organ. All of the pipes have roughly the same shape, but some of them are of different length and width. The groups of pipes correspond to the Clojure files. Each pipe represents a function. Very similar to those around it, but just a little different to make it useful.&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="http://s3.freefoto.com/images/11/14/11_14_51_web.jpg"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;br/&gt;Keep in mind, these were &lt;em&gt;excerpts &lt;/em&gt;from &lt;em&gt;a few&lt;/em&gt; files in a much larger project. I&amp;#8217;m having a blast with this code base, and I think what I&amp;#8217;ve pointed out is a green light that the design is solid.&lt;/p&gt;
&lt;p&gt;To summarize, my conjecture is this: &lt;strong&gt;Thoughtful abstractions in design will lead to code that is visually similar in each module&lt;/strong&gt;. If you notice this in your code base, you&amp;#8217;re probably doing a great job with the design.&lt;/p&gt;
&lt;p&gt;Hit me up at &lt;a href="https://twitter.com/MichaelDrogalis"&gt;@MichaelDrogalis&lt;/a&gt; with your thoughts. (It&amp;#8217;s a conjecture - I could be totally wrong!)&lt;/p&gt;
&lt;p&gt;Also, give Dire a try for logging in your system. I&amp;#8217;m a little biased since I&amp;#8217;m the author!&lt;/p&gt;</description><link>http://michaeldrogalis.tumblr.com/post/47417305519</link><guid>http://michaeldrogalis.tumblr.com/post/47417305519</guid><pubDate>Sun, 07 Apr 2013 20:58:00 -0400</pubDate><category>clojure</category></item><item><title>Beautiful Separation of Concerns</title><description>&lt;p&gt;I was going to write a nice narrative about how I wrote some beautiful code, but I’m just going to show it to you. I’d hate for my message to get drowned out in some boring text.&lt;/p&gt;
&lt;p&gt;You might recall that I wrote &lt;a href="https://github.com/MichaelDrogalis/dire"&gt;Dire&lt;/a&gt; a few months ago. I’m going to show you how to separate logging, preconditions, postconditions, and exception handling in an extremely elegant way. These concerns stay out of your application logic. And I do mean &lt;strong&gt;out&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Here’s some trivial code that gets the point across. Notice that all of these concerns get their own file!&lt;/p&gt;
&lt;script src="https://gist.github.com/MichaelDrogalis/5268385.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;p&gt;Here’s why the above design is kind of incredible. No one wants to see logging output during tests. In your test cases, simply don’t require dire-example.logging! That’s it! Don’t want preconditons? Don’t import the file! This is design elegance for Clojure on a new level. Tweet me at &lt;a href="https://twitter.com/MichaelDrogalis"&gt;@MichaelDrogalis&lt;/a&gt; with your thoughts.&lt;/p&gt;</description><link>http://michaeldrogalis.tumblr.com/post/46560874730</link><guid>http://michaeldrogalis.tumblr.com/post/46560874730</guid><pubDate>Thu, 28 Mar 2013 22:52:00 -0400</pubDate><category>clojure</category></item><item><title>try/catch complects: We can do so much better</title><description>&lt;p&gt;Picture this. You&amp;#8217;ve just finished writing a nice, clean function that clearly solves a single problem. It&amp;#8217;s beautiful. It&amp;#8217;s isolated. You&amp;#8217;re happy.&lt;/p&gt;
&lt;p&gt;You poke around at it for a while and realize some viable inputs make it explode. Part of you wants good error handling to keep the end user sane, but another part of you instinctively doesn&amp;#8217;t want to wrap that beautiful code in another form. The code is &lt;strong&gt;pure&lt;/strong&gt; as-is. With a mildly nauseous feeling in your stomach, you catch the exceptions and handle them inside your function. At least your end user won&amp;#8217;t hate you, I guess.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve had the above experience too many times. And until recently, I didn&amp;#8217;t know what to do about it. I believe this is in part why Clojure&amp;#8217;s infamous for poor error messages. It&amp;#8217;s a pain to pollute code that solves your problem with error handlers. It&amp;#8217;s worth taking the time to explain what bugs me so much about try/catch, not just in Clojure, but in any language that takes the same approach to error handling:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;It&amp;#8217;s a clear violation of the Single Responsibility Principle. By definition, anything function using a try/catch is doing &lt;strong&gt;at least &lt;/strong&gt;two things: application logic and handling an error for one exception type. It only gets worse as more exception types are propagated up the call chain. And let&amp;#8217;s not forget the finally clause! So that&amp;#8217;s 1 responsibility for application logic, n responsibilities for n catch clauses, and 1 responsibility for the finally clause to do any clean up. This does &lt;strong&gt;way&lt;/strong&gt; too much.&lt;/li&gt;
&lt;li&gt;It&amp;#8217;s a violation of the Open/Closed Principle. If you want to add another catch clause, you have to dive into the guts of a function. That bothers me a lot.&lt;/li&gt;
&lt;li&gt;It&amp;#8217;s order-complected. There&amp;#8217;s incidental complexity in the sequence that you list which exception types you&amp;#8217;re going to handle before others. Rich scorns ordering when it&amp;#8217;s not necessary, which is exemplified by his views on pattern matching and long function parameter lists (&lt;a href="http://www.infoq.com/presentations/Simple-Made-Easy"&gt;Simple Made Easy&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Now that I&amp;#8217;ve spelled out my problem with traditional error handling in Clojure, we can move onto the solution. In Rich&amp;#8217;s talk &lt;a href="http://skillsmatter.com/podcast/scala/the-language-of-the-system"&gt;The Language of the System&lt;/a&gt;, he directs the audience to read Joe Armstrong&amp;#8217;s (the creator of Erlang) &lt;a href="http://www.erlang.org/download/armstrong_thesis_2003.pdf"&gt;dissertation on error handling&lt;/a&gt;. Rich said something to the effect that it takes error handling and completely turns it on its head. I was curious, so I gave it a read. If you haven&amp;#8217;t read it before, check it out. It&amp;#8217;s a quick read filled with a lot wisdom.&lt;/p&gt;
&lt;p&gt;In his paper, he lays out 4 main points that Erlang&amp;#8217;s error handling was built around to provide incredible fault-tolerancy (page 106):&lt;/p&gt;
&lt;ol&gt;&lt;li&gt;Clean separate of issues&lt;/li&gt;
&lt;li&gt;Special processes for error handling&lt;/li&gt;
&lt;li&gt;Capability for application logic and error handling code to run on different physical machines&lt;/li&gt;
&lt;li&gt;Ease of creating general error handlers&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Points 2 and 3 are very specific to Erlang, so I won&amp;#8217;t touch on those in this post. Read the paper to get a better idea of his philosophy. Points 1 and 4, however, made great candidates for the kind of functionality I wanted to bring to Clojure.&lt;/p&gt;
&lt;p&gt;The goals for the functionality I wanted to introduce can be enumerated by a few points:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;Total separation of application logic from error handling logic. One responsibility per logical unit of code.&lt;/li&gt;
&lt;li&gt;Respect the Open/Closed Principle and make it a &amp;#8220;no-think-operation&amp;#8221; to add more error handlers. In short, make it feel like multimethods.&lt;/li&gt;
&lt;li&gt;Avoid order-complected sequences of error handling.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;These goals match up as solutions to the problems I listed about try/catch earlier. Cool, sounds like I&amp;#8217;m on the right track.&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;So I sat down and wrote the darn thing. I called it Dire (&lt;a href="https://github.com/MichaelDrogalis/dire"&gt;GitHub source&lt;/a&gt;) because I felt that we could use it that badly. Let&amp;#8217;s see some code:&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;script src="https://gist.github.com/4498488.js" type="text/javascript"&gt;&lt;/script&gt;&lt;div&gt;&lt;/div&gt;
&lt;div&gt;Compare that with Dire&amp;#8217;s approach:&lt;/div&gt;
&lt;div&gt;&lt;/div&gt;
&lt;script src="https://gist.github.com/4498503.js" type="text/javascript"&gt;&lt;/script&gt;&lt;div&gt;&lt;/div&gt;
&lt;p&gt;There&amp;#8217;s a concise API for adding catch clauses, finally clauses, and even pre and post conditions. There&amp;#8217;s also non-bang (!) variants that don&amp;#8217;t mutate your functions (they&amp;#8217;re the second bunch of examples listed on GitHub).&lt;/p&gt;
&lt;div&gt;&lt;/div&gt;
&lt;p&gt;Speaking of pre and post conditions. You know what the problem is with assertions of that nature in virtually all languages? They only offer error detection! Error handling is a two part mechanism. Part 1 is detection: check. What we&amp;#8217;re missing is part two: Reaction. When things have been detected to have gone awry, what are we going to do about it? Dire let&amp;#8217;s you react with arbitrary defined functions. I think turning off assertions when we&amp;#8217;re not in development mode is a bad idea. We&amp;#8217;re throwing away the chance to gracefully conduct our program when it&amp;#8217;s in danger.&lt;/p&gt;
&lt;p&gt;So please, let me know what you think. Was this a worthwhile effort? Tweet me at &lt;a href="https://twitter.com/MichaelDrogalis"&gt;@MichaelDrogalis&lt;/a&gt;&lt;/p&gt;</description><link>http://michaeldrogalis.tumblr.com/post/40181639419</link><guid>http://michaeldrogalis.tumblr.com/post/40181639419</guid><pubDate>Thu, 10 Jan 2013 12:03:00 -0500</pubDate><category>clojure</category></item><item><title>Designing with Topics in Clojure</title><description>&lt;p&gt;Designing systems that obey the Open/Closed principle is tough. Here&amp;#8217;s a nugget of wisdom about using topics to create a system that&amp;#8217;s more fluid in terms of extensibility. I&amp;#8217;ll demonstrate in Clojure.&lt;/p&gt;
&lt;p&gt;A topic is a type of message queue where all subscribers consume every message. An example of this in the small is Clojure&amp;#8217;s &lt;a href="http://clojuredocs.org/clojure_core/clojure.core/juxt"&gt;juxt&lt;/a&gt; function.&lt;/p&gt;
&lt;p&gt;What happens when your functions in juxt are computationally expensive? What happens if you constantly add new functions? A topic makes great sense for both of these cases. It allows you to add new functions to the computation without having to modify the innards of any existing code. Just add a new subscriber, and away you go.&lt;/p&gt;
&lt;p&gt;The following example is rather contrived, but I wanted to keep it as simple as possible to get the point across. I used &lt;a href="https://github.com/sbtourist/clamq"&gt;Clamq&lt;/a&gt; ontop of &lt;a href="http://activemq.apache.org/"&gt;ActiveMQ&lt;/a&gt;. As you read, visualize how simple it is to add another computation.&lt;/p&gt;
&lt;script src="https://gist.github.com/4481202.js" type="text/javascript"&gt;&lt;/script&gt;</description><link>http://michaeldrogalis.tumblr.com/post/39990462971</link><guid>http://michaeldrogalis.tumblr.com/post/39990462971</guid><pubDate>Mon, 07 Jan 2013 23:39:00 -0500</pubDate><category>clojure</category></item></channel></rss>
