<?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>Made of Bugs</title>
	<atom:link href="http://blog.nelhage.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nelhage.com</link>
	<description>It's software. It's made of bugs.</description>
	<lastBuildDate>Mon, 19 Jul 2010 15:40:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Some musings on ORMs</title>
		<link>http://blog.nelhage.com/2010/07/some-musings-on-orms/</link>
		<comments>http://blog.nelhage.com/2010/07/some-musings-on-orms/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 22:38:23 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[dynamic analysis]]></category>
		<category><![CDATA[musings]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=278</guid>
		<description><![CDATA[I&#8217;m pretty sure every developer who has ever worked with a modern database-backed application, particularly a web-app, has a love/hate relationship with their ORM, or object-relational mapper. On the one hand, ORMs are vastly more pleasant to work with than code that constructs raw SQL, even, generally, from a tool that gives you an object [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty sure every developer who has ever worked with a modern
database-backed application, particularly a web-app, has a love/hate
relationship with their <a href="http://en.wikipedia.org/wiki/Object-relational_mapping">ORM</a>, or object-relational mapper.</p>

<p>On the one hand, ORMs are vastly more pleasant to work with than code
that constructs raw SQL, even, generally, from a tool that gives you
an object model to construct SQL, instead of requiring (Cthulhu help
us all) string concatenation or interpolation.</p>

<p>On the other hand, there tends to be an enormous mismatch between the
objects the ORM is modelling on one end, and the relational database
on the back end. Basically, the object model encourages dealing with
one object at a time, whereas the relational database wants you to
push your queries, JOINs, and even your entire transactions back into
it, so that it can better optimize them for you. And so, you tend to
have a choice between writing shockingly inefficient code, and jumping
through weird hoops and strange syntax to give your ORM the tools to
generate efficient queries.</p>

<p>The most fundamental problem, perhaps, is the question of how much
data the ORM should request from the database. When I ask for a
collection of objects (using some syntax to generate the SQL <code>WHERE</code>
clause automatically), the ORM has to decide:</p>

<ul>
<li>Which fields in the object should the ORM <code>SELECT</code> from the
database, and</li>
<li>Which associated models (with foreign keys to or from the object in
question) the ORM should <code>JOIN</code> in and pull out at the same time.</li>
</ul>

<p>(You can think of the second as a more involved version of the first,
but involves more complexity, and is interesting for being potentially
recursive).</p>

<p><code>SELECT</code>ing too much data in either case makes the database do too
much work, touch too many blocks on disk, read too many indexes, and
so on, and makes your queries slow. <code>SELECT</code>ing not enough data means
your ORM needs to go back and make additional queries when you ask for
fields or models that were not pulled in initially, killing your
performance even more.</p>

<p>I&#8217;d like to muse about two possible solutions, neither of which I&#8217;m
aware of implementations of, although to be honest I haven&#8217;t looked
too hard. I&#8217;d love it if anyone could point me at work on either sort.</p>

<h2>The static solution</h2>

<p>Given a language with a sufficiently awesome type system (Haskell
comes to mind :)), you could imagine encoding information about which
fields and models are or are not <code>SELECT</code>ed into the type of a record
object, making it statically an error to write a query that
under-selects. Given type inference, we can probably even go on to
statically infer (probably with a bit of programmer help in the form
of type annotations) which fields and models we need to <code>SELECT</code>,
based precisely on which fields of the result get accessed.</p>

<p>This is the kind of solution that occurs immediately to a certain type
of person (of which I know a lot) when they think about this type of
problem enough. It&#8217;s fun to think about and there are probably some
papers in it (if no one&#8217;s beaten you to it), but I suspect it&#8217;s the
kind of thing that&#8217;s unlikely to gain much traction among mainstream
developers, if only because it would require you write in Haskell or
equivalent.</p>

<h2>The dynamic solution</h2>

<p>Another solution occurred to me recently, which I don&#8217;t think I&#8217;ve
heard people talking about. I like it because I&#8217;m pretty sure it could
be grafted onto an existing ORM with little or no API change, which
means there&#8217;s potential for incrementel adoption and improvement.</p>

<p>The key insight here is that, while the decision of what to <code>SELECT</code>
is critical for performance, it&#8217;s (mostly)<a href="#fn1"><sup>1</sup></a> irrelevant for correctness whether you
have to make additional queries later, or pull in too much data. A
good ORM, if it notices you ask for a field it doesn&#8217;t have cached,
will just go ahead and make the <code>SELECT</code> for you.</p>

<p>This opens the door for heuristic solutions. A good ORM will already
have hooks for the programmer to specify which fields or models to
include or exclude &#8212; <a href="http://www.djangoproject.com/">Django</a>, to pick a popular example, has
the <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.QuerySet.select_related"><code>select_related</code></a> and <a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.QuerySet.defer"><code>defer</code></a> methods on
its <code>QuerySet</code>s.</p>

<p>So, my proposal is this: Let&#8217;s develop tools to profile an
application, and automatically generate those annotations in a
profile-driven manner. Since all accesses to your data are through the
ORM, we can instrument the ORM to determine which fields are selected
after a query, and remember that information for next time it sees the
same query.</p>

<p>In a webapp environment, the possibilities for optimization get
potentially even more exciting. A webapp framework like Django knows
an awful lot of information at any given moment, like what user is
logged into your app, and what URL is currently being generated. We
could imagine associating that data with the profile results, so that
the ORM could automatically discover relations between the application
structure and the ORM queries. It could &#8220;learn&#8221;, for example, that
certain users are administrators, and so will access more fields on
certain models than other users. It could learn that even though
<code>/places/list/</code> and <code>/places/details/</code> pull up the exact same set of
models (maybe even through the same helper function!), the former
needs only their names and IDs, while the latter needs to pull in full
details, as well as rows from the associated <code>reviews</code> table.</p>

<p>All without a single programmer annotation. Of course, in cases where
the behavior is too complex for the tool&#8217;s heuristics to pick up, or
where the programmer absolutely needs predictable behavior or needs to
micro-optimize, you could turn off the heuristics and fall back the
old behavior (with the same old annotations available to the
programmer).</p>

<p>Is anyone aware of any work in this space? It sounds like a
potentially extremely exciting project to me, but I probably don&#8217;t
have the time to attempt to throw together an implementation.</p>

<p><small><a name="fn1">1.</a>Ignoring, for example, race conditions
where you need to select a model and related models atomically, and
don&#8217;t want or can&#8217;t afford a transaction around the entire lifetime of
the objects in the app code.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/07/some-musings-on-orms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Implementing a declarative mini-language in the C preprocessor</title>
		<link>http://blog.nelhage.com/2010/07/implementing-an-edsl-in-cpp/</link>
		<comments>http://blog.nelhage.com/2010/07/implementing-an-edsl-in-cpp/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 19:54:55 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[preprocessor]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=272</guid>
		<description><![CDATA[Last time, I announced Check Plus, a declarative language for defining Check tests in C. This time, I want to talk about the tricks I used to implement a declarative minilanguage using the C preprocessor (and some GCC extensions). The Problem We want to write some toplevel declarations that look like: #define SUITE_NAME example BEGIN_SUITE("Example [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.nelhage.com/2010/06/check-plus-an-edsl-for-writing-unit-tests-in-c/">Last time</a>, I announced Check Plus, a declarative language for
defining <a href="http://check.sourceforge.net/">Check</a> tests in C. This time, I want to talk about the tricks I
used to implement a declarative minilanguage using the C preprocessor (and some
GCC extensions).</p>

<h2>The Problem</h2>

<p>We want to write some toplevel declarations that look like:</p>

<pre><code>#define SUITE_NAME example
BEGIN_SUITE("Example test suite");

#define TEST_CASE core
BEGIN_TEST_CASE("Core tests");
…
</code></pre>

<p>and so on, and somehow translate them into code that does the equivalent of:</p>

<pre><code>Suite *s = suite_create ("Example test suite");
TCase *tc_core = tcase_create ("Core tests");
…
</code></pre>

<h2>First steps</h2>

<p>Instead of having our macros somehow lay down code directly, we&#8217;ll use them to
define some global data structures, which we will then loop across at runtime,
and make the appropriate Check library calls. We&#8217;ll define some structs that
we&#8217;re going to use for this purpose:</p>

<pre><code>typedef void (*test_func)(int);
typedef void (*test_hook)(void);

struct test_case {
    test_func  *funcs, *end_funcs;
    const char *test_name;
    test_hook *setup_hooks, *end_setup_hooks;
    test_hook *teardown_hooks, *end_teardown_hooks;
};

struct test_suite {
    struct test_case *cases, *end_cases;
    const char *suite_name;
};
</code></pre>

<p>Since tests and test fixtures are code, we&#8217;re going to need to keep function
pointers to them. We use typedefs to keep thos readable, and then we define
structs to represent a test suite, and a test case. We&#8217;re going to represent
arrays (of test cases, fixture hooks, or test functions) using a pair of
pointers, to the start and end of the array. There&#8217;s a specific reason for this
representation, which I&#8217;ll get to shortly.</p>

<h2>ELF Sections</h2>

<p>We can easily define global instances of these structs, but the problem is how
to get the pointers to the child arrays correctly. These macros are going to be
interspersed with other code, so we can&#8217;t just use a literal array, like</p>

<pre><code>struct test_suite s = {
 .suite_name = "Example test suite",
 .cases = {
   …
 }
};
</code></pre>

<p>because that &#8220;…&#8221; is going to have arbitrary code shoved in it.</p>

<p>The solution is to take advantage of a feature of the ELF object file format,
and some GCC extensions that let us take advantage of it.</p>

<p>ELF object files can contain an arbitary set of named sections, each
of which can contain code or data (or both, but that&#8217;s not
typical). GCC allows us to specify that a given variable should live
in a given section using
<code>__attribute__((section("section_name"))</code>. So, by placing all our
<code>struct test_case</code>s for a given <code>test_suite</code> into a separate section,
we can cause them to get placed contiguously, which means we just need
to get pointers to the start and end. That we can accomplish by
declaring a zero-length array in the appropriate section, which (being
of size zero) won&#8217;t generate any data, but will have the appropriate
address.</p>

<p>So, the code we&#8217;re going to generate for the above will look something
like:</p>

<pre><code>struct test_case _begin_example_cases[];
struct test_case _end_example_cases[];
struct test_suite _suite_example = {
  .cases = _begin_example_cases,
  .end_cases = _end_example_cases,
  .suite_name = "Example test suite"
};
struct test_case _begin_example_cases[0]
  __attribute__((section(".data.example.cases")));

struct test_case _test_example_core
  __attribute__((section(".data.example.cases"))) = {
  .test_name = "Core tests",
  …
};

…

struct test_case _end_example_cases[0]
  __attribute__((section(".data.example.cases")));
</code></pre>

<h2>Preprocessor tricks</h2>

<p>So far, I haven&#8217;t actually talked about the preprocessor at all. I&#8217;ve
just shown the code we want our macros to generate. So now let&#8217;s get
to the business of actually generating this code. I&#8217;ll walk through
the definition of `BEGIN_SUITE&#8217;, which expands to the code above. The
other definitions are all essentially analagous.</p>

<p>My first version of this library didn&#8217;t use the <code>SUITE_NAME</code> and
<code>TEST_CASE</code> macros, instead requiring those arguments to be passed to
every macro. I like the decreased repetition that this method gives,
but we&#8217;ll start with the explicit version, since it requires less
trickery to understand.</p>

<p>From the above example, we can see that <code>BEGIN_SUITE(example,
"Example")' is given the</code>example&#8217; token, and needs to construct both
symbols (<code>_suite_example</code>) and strings (<code>".data.example.cases"</code>)
containing that symbol. To do the first, we need to the processor
operator <code>##</code>, which concatenates two tokens into one. For example,
given</p>

<pre><code>#define PASTE(x,y) x##_##y
</code></pre>

<p>`PASTE(foo, bar)&#8217; is equivalent to just having written &#8220;foo_bar&#8221;
literally in your code.</p>

<p>Secondly, to construct strings, we need the <code>#x' preprocessor
operator. This operator, when applied to an argument to a macro,
returns a string literal containing the tokens passed to the macro. A
common usage of this is for an</code>assert&#8217; macro, that can print the
failed expression:</p>

<pre><code>#define assert(x) if (!(x)) { die("Assertion failed: %s\n", #x); }
</code></pre>

<p>And finally, we&#8217;re going to need the implicit concatenation feature of
the C language, which lets you type <code>"foo" "bar"</code> with the same
meaning as &#8220;foobar&#8221;.</p>

<p>Putting it all together, and we get:</p>

<pre><code>#define BEGIN_SUITE(sym, name)                                          \
    struct test_case _begin_ ## sym ## _cases[];                        \
    struct test_case _end_ ## sym ## _cases[];                          \
    struct test_suite _suite_ ## sym = {                                \
        .cases       = (struct test_case*)_begin_ ## sym ## _cases,     \
        .end_cases   = (struct test_case*)_end_ ## sym ## _cases,       \
        .suite_name  = name,                                            \
    };                                                                  \
    struct test_case _begin_ ## sym ## _cases[0]                        \
    __attribute__((section(".data." #sym ".cases")))
</code></pre>

<p>Two last things to note: we deliberately leave off a trailing
semicolon, requiring the user to provide one, and we need to end every
line with a <code>\</code>, to include the entire body in the macro definition.</p>

<h2>Another layer of indirection</h2>

<p>The one final bit is to get rid of that explicit <code>sym</code> argument
everywhere, in favor of the <code>SUITE_NAME</code> define. Our first try might
look something like:</p>

<pre><code>#define BEGIN_SUITE(name)                                               \
    struct test_case _begin_ ## SUITE_NAME ## _cases[];                 \
    …
</code></pre>

<p>You&#8217;ll quickly realize, however, that since <code>##</code> is applied at the
same time as function-like macro expansion, this will always give you
the symbol <code>_begin_SUITE_NAME_cases</code>. The solution is to force the
preprocessor to perform multiple phases of macro expansion, to cause
the <code>##</code> to be applied only after <code>SUITE_NAME</code> is expanded. In this
case, we require three layers of macros to get the order right:</p>

<pre><code>#define BEGIN_SUITE(name) _BEGIN_SUITE(SUITE_NAME, name)
#define _BEGIN_SUITE(sym, name) __BEGIN_SUITE(sym, name)
#define __BEGIN_SUITE(sym, name)                                        \
    struct test_case _begin_ ## sym ## _cases[];                        \
</code></pre>

<p>This causes the following sequence of expansions for
<code>BEGIN_SUITE("Example");</code>:</p>

<pre><code>BEGIN_SUITE("Example");                     // (a)
_BEGIN_SUITE(SUITE_NAME, "Example");        // (b)
__BEGIN_SUITE(sym, "Example");              // (c)
</code></pre>

<p>The key here is that when expanding a function-like macro, the
preprocessor performs the following steps, in order:</p>

<ol>
<li>Apply any <code>#</code> or <code>##</code> operators.</li>
<li>Macro-expand any arguments to the call</li>
<li>Perform the substitution</li>
<li>Macro-expand the result of the substitution</li>
</ol>

<p>In going from (a) to (b), only step (3) applies. When going from (b)
to (c), rule (2) means that <code>SUITE_NAME</code> gets expanded first, leaving
(c). Rule (1) means that the <code>_BEGIN_SUITE</code> macro is necessary, since
a call to <code>__BEGIN_SUITE(SUITE_NAME, "Example")</code> will get the <code>#</code> and
<code>##</code> applied to <code>SUITE_NAME</code> before it can be expanded.</p>

<h2>Conclusions</h2>

<p>So, that&#8217;s basically all there is to the Check-Plus implementation. If
you&#8217;re comfortable with that, you can try your hand at unravelling the
implementation of the Linux kernel&#8217;s <a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=include/trace/define_trace.h;h=1dfab54015113b83bce9f3302470c3a5ed95b5e7;hb=HEAD">tracing</a>
<a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=include/trace/ftrace.h;h=5a64905d7278a47fb683a0aceb63cef029dd467b;hb=HEAD">macros</a>, next (<a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=include/trace/events/kmem.h;h=3adca0ca9dbee10479d34d5a3e3562609ef89e86;hb=HEAD">sample usage</a>)!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/07/implementing-an-edsl-in-cpp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Check Plus: An EDSL for writing unit tests in C</title>
		<link>http://blog.nelhage.com/2010/06/check-plus-an-edsl-for-writing-unit-tests-in-c/</link>
		<comments>http://blog.nelhage.com/2010/06/check-plus-an-edsl-for-writing-unit-tests-in-c/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 19:54:53 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=263</guid>
		<description><![CDATA[Check is an excellent unit-testing framework for C code, used by a number of relatively well-known projects. It includes features such as running all tests in separate address spaces (using fork(2)), which means that the test suite can properly report segfaults or similar crashes without the test runner crashes. My main complaint about Check is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://check.sourceforge.net/">Check</a> is an excellent unit-testing framework for C code, used
by a number of relatively well-known projects. It includes features
such as running all tests in separate address spaces (using
<code>fork(2)</code>), which means that the test suite can properly report
segfaults or similar crashes without the test runner crashes.</p>

<p>My main complaint about Check is that (unsurprisingly for a framework
written in C), it&#8217;s not very declarative. After you define all your
tests as separate functions, you need to write code to manually
collect them into &#8220;test cases&#8221;, which you then collect into &#8220;test
suites&#8221;, which you can then run. While this is an understandable
interface for a C library to provide, as a user I found that it
rapidly annoyed me, and so I wrote Check Plus.</p>

<p><a href="http://github.com/nelhage/check-plus">Check Plus</a> is a simple library I wrote that uses some preprocessor
tricks and GNU C extensions to provide a declarative EDSL within C for
declaring and collecting your test cases. Check-Plus is a single
self-contained header file distributed under the LGPL (like Check), so
it should be easy to pull into your project.</p>

<h2>Example</h2>

<p>Check-Plus lets you declare your Check suites and test cases in a
declarative manner. A simple example, shipped with Check-Plus, would
look like:</p>

<pre><code>#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

#include "check-plus.h"

#define SUITE_NAME example
BEGIN_SUITE("Example test suite");

#define TEST_CASE core
BEGIN_TEST_CASE("Core tests");

TEST(strcmp)
{
    fail_unless(strcmp("hello", "HELLO") != 0);
    fail_unless(strcasecmp("hello", "HELLO") == 0);
}
END_TEST

TEST(strcat)
{
    char buf[1024] = {0};
    strcat(buf, "Hello, ");
    strcat(buf, "World.");
    fail_unless(strcmp(buf, "Hello, World.") == 0);
}
END_TEST

END_TEST_CASE;
#undef TEST_CASE
END_SUITE;
#undef SUITE_NAME
</code></pre>

<p>Having done that, you can extract the declared test suite using
<code>construct_test_suite(example)</code>, and run it using the standard Check
test runner tools.</p>

<p>You can, of course, define multiple test cases within a single suite,
and you can even define test fixtures using</p>

<pre><code>DEFINE_FIXTURE(setup, teardown);
</code></pre>

<p>within the <code>BEGIN_TEST_CASE</code> &#8230; <code>END_TEST_CASE</code> block.</p>

<p>The equivalent code, without Check-Plus, would look similar, but
instead of the <code>BEGIN_</code> and <code>END_</code> blocks to define the suite and test case, you&#8217;d have to write
something like:</p>

<pre><code>Suite *
example_suite (void)
{
  Suite *s = suite_create ("Example test suite");

  /* Core test case */
  TCase *tc_core = tcase_create ("Core tests");
  tcase_add_test (tc_core, test_strcmp);
  tcase_add_test (tc_core, test_strcat);
  suite_add_tcase (s, tc_core);

  return s;
}
</code></pre>

<p>It&#8217;s not a huge difference, but it&#8217;s big enough that I really
appreciate it once the test suite gets large. For more documentation on Check, check out <a href="http://check.sourceforge.net/doc/check_html/index.html">its manual</a>.</p>

<p>Let me know if you find this useful for anything. Next week, I&#8217;ll do a
writeup on the tricks behind the implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/06/check-plus-an-edsl-for-writing-unit-tests-in-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Lab Notebooking for the Software Engineer</title>
		<link>http://blog.nelhage.com/2010/06/lab-notebooking-for-the-software-engineer/</link>
		<comments>http://blog.nelhage.com/2010/06/lab-notebooking-for-the-software-engineer/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 02:53:07 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[lab notebooks]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[writing]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=254</guid>
		<description><![CDATA[A few weeks ago, I wrote that software engineers should keep lab notebooks as they work, in addition to just documenting things after the fact. Today, I&#8217;m going to share the techniques that I&#8217;ve found useful to try to get in the habit of lab-notebooking my work, even though I still feel like I could [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, I <a href="http://blog.nelhage.com/2010/05/software-and-lab-notebooks/">wrote</a> that software engineers should keep
lab notebooks as they work, in addition to just documenting things
after the fact. Today, I&#8217;m going to share the techniques that I&#8217;ve
found useful to try to get in the habit of lab-notebooking my work,
even though I still feel like I could be better at writing things
down.</p>

<p>Here&#8217;s my advice for keeping a lab notebook as a computer scientist:</p>

<dl>
<dt>Use a text file</dt>

<dd> <p>There&#8217;s no need to use anything fancier. And if you&#8217;re writing
code all day, you can just keep your log file open in your editor of
choice, so that you don&#8217;t need to leave your hacking environment to
make a note of something. I&#8217;m an emacs junkie and use
<a href="http://orgmode.org/">org-mode</a> to keep all my notes, but use whatever you&#8217;re
comfortable with.</p>


<p>You could use a physical notebook, but unless you expect to want
documentation of what you were working on for some legal reason, I
don&#8217;t see the point. And most programmers I know hate writing things
out by hand.</p>
</dd>

<dt>Treat your file as append-only</dt>

<dd>One of the downsides of just using a text file is that you need to
enforce some discipline on yourself. It&#8217;s tempting to go back and fix
up things that have changed, or to keep editing a desgin description
as it evolved. But creating a perfect finished piece of documentation
is a different task. The point here is to have a log of everything you
do, not a single finished document. You can work on one side-by-side
with the (dated or even timestampped) append-only log.</dd>

<dt>Keep automatic backups.</dt>

<dd>Do this any way you want. I keep my log files in git, and have a
cron job that commits and pushes to a server every five minutes. This
provides at least two benefits. First, it applies an automatic timestamp (to
within five minutes) on anything I type, and second, it serves to make
sure that a recent version is available on my server, in case I switch
machines and want to keep working on a project.
</dd>

<dt>Train yourself to make regular entries.</dt>

<dd>

<p>Keeping this nice text file, regularly backed up, is useless if you
don&#8217;t record relevant things into it. Getting yourself into the habit
of writing enough down is tricky. One strategy I&#8217;ve found helpful is
to write down a list of &#8220;triggers&#8221; for situations that you promise to
always record. With an explict list of these that you&#8217;ve decided to
always record, you can get good at making a note whenever you perform
one of these tasks, which gets you into the habit of writing things
down. Here&#8217;s a partial list of some of the sorts of things I make a
point of writing down:</p>

<ul>

<li>Whenever I take a measurement. That is, any time I run
<tt>time</tt>, or look at <tt>top</tt> to figure out how much memory a
program under development is using, or similar, I&#8217;ll write down the
number and the circumstances under which it was recorded. I&#8217;ve found
that I almost always would like to have a better record of such things
later.</li>

<li>Whenever I do a non-trivial search and find a result. This can be
anything from googling for a solution to some problem where it takes
me several tries to find the right keywords, or grepping a large
codebase searching for the function that performs some task, or
tracking down some API function in documentation. If I wanted it once,
I&#8217;ll often want it later, and if it was hard to find, I&#8217;ll feel stupid
if I didn&#8217;t write it down.</li>

<li>Whenever I make an explicit design decision. If I&#8217;m thinking about
how to design some function, system, process, or whatever, I&#8217;ll stop
and take a moment to write down the options I was considering, and why
I made the choice I did. This ensures I can go back and remember why I
built a system the way it is.
</li>


<li>Whenever I wish I had written something down last time. This
should be obvious, but if you find yourself thinking &#8220;I know I did
this before; Why didn&#8217;t I take notes?&#8221;, you should
<strong>always</strong> make a point of writing it down this time, no
matter what excuse you come up with for why you really won&#8217;t need that
information a third time.
</li>

</ul>

<p>These categories are deliberately broad, but at the same time
well-defined. It&#8217;s fairly easy to train yourself to notice whenever
any of these points triggers, and remember to stop and write something
down. And that&#8217;s the first step to getting in the habit of writing
down everything that you&#8217;ll later wish you had written down.</p>

</dd>

</dl>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/06/lab-notebooking-for-the-software-engineer/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>WordPress tricks: Disabling editing shortcuts</title>
		<link>http://blog.nelhage.com/2010/06/disable-wordpress-edit-shortcuts/</link>
		<comments>http://blog.nelhage.com/2010/06/disable-wordpress-edit-shortcuts/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 00:07:00 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=250</guid>
		<description><![CDATA[One of the major reasons I can&#8217;t stand webapps is because I&#8217;m a serious emacs junkie, and I can&#8217;t edit text in anything that doesn&#8217;t have decent emacs keybindings. Fortunately, on Linux, at least, GTK provides basic emacs keybindings if you add gtk-key-theme-name = "Emacs" to your .gtkrc-2.0. However, some webapps think that they deserve [...]]]></description>
			<content:encoded><![CDATA[<p>One of the major reasons I can&#8217;t stand webapps is because I&#8217;m a
serious emacs junkie, and I can&#8217;t edit text in anything that doesn&#8217;t
have decent emacs keybindings.</p>

<p>Fortunately, on Linux, at least, GTK provides basic emacs keybindings
if you add</p>

<pre><code>gtk-key-theme-name = "Emacs"
</code></pre>

<p>to your <code>.gtkrc-2.0</code>. However, some webapps think that they deserve
total control over your keys, and grab key combinations for a WYSIWYG
editor of some sort. And so whenever I try to edit a post in WordPress
(most of them are written in emacs and then copied over), I find
myself trying to go backwards a word, and inserting random
<code>&lt;strong&gt;</code> tags all over my post (Because <code>M-b</code> is
bound to make text bold, by WordPress&#8217;s editor). I finally got annoyed
enough to do some source-diving, and discovered that WordPress&#8217;s
editor constructs keyboard shortcuts using the HTML <a href="http://www.w3.org/TR/html5/editing.html#dfnReturnLink-0">accesskey</a>
attribute. This is easy enough to manipulate from Javascript, so I
went and wrote up a quick Greasemonkey user script. The bulk of it is
a simple XPath:</p>

<pre><code>    var buttons = document.evaluate('//input[@type="button"][@accesskey]', poststuff);
    var button;
</code></pre>

<p>You can <a href="http://nelhage.com/files/wp-keys.user.js">install the
script</a> off of nelhage.com.</p>

<p>Let me know if you find this useful, or if anyone figures out a
general way to disable (sets of) keyboard shortcuts for websites,
without relying on knowing the specific tricks that a website uses.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/06/disable-wordpress-edit-shortcuts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Confessions of a programmer: I hate code review</title>
		<link>http://blog.nelhage.com/2010/06/i-hate-code-review/</link>
		<comments>http://blog.nelhage.com/2010/06/i-hate-code-review/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 00:21:11 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[code review]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ksplice]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=241</guid>
		<description><![CDATA[Most of the projects I&#8217;ve been working on today have fairly strict code review policies. My work requires code review on most of our code, and as we bring on an army of interns for the summer, I&#8217;ve been responsible for reviewing lots of code. Additionally, about five months ago BarnOwl, the console-based IM client [...]]]></description>
			<content:encoded><![CDATA[<div id="text-1">

<p>
Most of the projects I&#8217;ve been working on today have fairly strict
code review policies. My <a href="http://www.ksplice.com">work</a> requires code review on most of our
code, and as we bring on <a href="http://blog.ksplice.com/2010/03/quadruple-productivity-with-an-intern-army/">an army of interns</a> for the summer, I&#8217;ve been
responsible for reviewing lots of code. Additionally, about five
months ago <a href="http://barnowl.mit.edu/">BarnOwl</a>, the console-based IM client I develop, adopted an
official pre-commit review policy. And I have a confession to make: I
hate mandatory code review.
</p>
<p>

I should be clear that I think I still <b>believe</b> in code review
as a useful tool for developers working together on a project. A
reviewer will flag as bad style or inefficient or ugly things that you
let slide working for yourself. A reviewer comes at code without the
assumptions of how it&#8217;s supposed to work that you made, and can often
spot errors you made because mixed up a mental model of your intent
with the code you actually wrote.
</p>

<p> In addition, code review is a great way to ensure that at least
two people are familiar with each piece of your infrastructure. There
is often a natural tendency for different developers to take ownership
of specific pieces of code or infrastructure, and be the only ones who
touch or maintain it. But then it breaks while they&#8217;re on vacation,
and everyone else is left trying to figure out how this code was ever
supposed to work. A mandatory code review policy is often a great way
to mitigate that class of problem.
</p>

<p> But, theoretical and observed benefits of code review aside,
speaking personally, as both a developer and as a reviewer, I&#8217;ve been
finding it a really frustrating process.  </p>

</div>

<div id="outline-container-1.1" class="outline-3">
<h3 id="sec-1.1">As an author </h3>
<div id="text-1.1">


<p>
As a developer, I hate that code review adds unpredictably long
latencies into my development workflow. Once I&#8217;ve finished and tested
a feature to my satisfaction, and sent it off for code review, I have
to wait for a potentially long time before I can actually mark it as
done. This means that, when deciding what to do next, I have
essentially three choices:
</p>
<ol>
<li>
Busy-wait. Get coffee, read reddit, and check my mail until the
review request comes back.

</li>
<li>
Continue development on top of the code I just sent for code
review.

</li>
<li>
Work on something completely different.

</li>
</ol>

<p>All three of these options suck. (1) is convenient if I can expect the
review to come back shortly, since doing something idle like reading
reddit or checking mail lets me keep the code in the back of my mind,
so I don&#8217;t have to page it back in when the review response comes
back. But obviously it&#8217;s inefficient, wasted time, and unacceptable if
I don&#8217;t expect a reply within an hour at most.
</p>
<p>
(2) is often what I want to be doing. I&#8217;ll often be working on a
project with several logically distinct but related stages. It often
makes sense to send out a review request for each, since they can be
deployed and reviewed separately.
</p>
<p>
However, if a reviewer comes back with significant comments on the
code I sent out, I now not only need to update that code, but I also
need to rebase the work I&#8217;ve done since then on top of the result,
which may be a real pain if I&#8217;m working on something closely related
(e.g. if my work used APIs I built previously, and the reviewer asks
for changes in those APIs in some way).
</p>
<p>
Option (3) avoids both problems, but means that I&#8217;m continually
swapping different projects in and out of focus. This slows me down,
since I have to constantly re-remember where I was in each project,
which APIs I was using, and so on. Any developer can tell you that
they hate switching between different projects too frequently.
</p>
<p>
Option (3) might be more tolerable with large projects, where
development/review cycles are on the order of weeks. But those aren&#8217;t
the projects I&#8217;m working on.
</p>
</div>

</div>

<div id="outline-container-1.2" class="outline-3">
<h3 id="sec-1.2">As a reviewer </h3>
<div id="text-1.2">


<p>
Reviewing code is one of those things that I would enjoy if I had
infinite time, but that I find a nuisance when I don&#8217;t. I do enjoy
reading other peoples&#8217; code and figuring out how it could be better,
and giving feedback to help get it there.
</p>
<p>
But doing so well takes a lot of time, and a lot of time is something
I rarely have these days. In addition, because of my above complaints
about dealing with code review as a developer, I&#8217;m always acutely
aware that someone is probably waiting on my reply in order to get
work done. So I always feel rushed to reply to code review requests,
as well.
</p>
<p>
So, even though I always feel like code review should be something I&#8217;m
enjoying, I tend to find it a frustrating experience where it just
feels like a task I have to do before I can get back to real work.
</p>
<p>

This is, of course, in part just a symptom of being too busy, but code
review makes it a task that bugs me more than other time drains. If a
customer reports a critical bug that I have to drop everything to
investigate, I&#8217;ll be annoyed, but I at least feel like I&#8217;m doing
something important that fixes a real problem and hopefully ends with
a happy customer. If I spend a day doing nothing but reading code
reviews, I&#8217;ll end up feeling unsatisfied and unproductive. Because
code review feels fundamentally optional &#8212; even though I believe it&#8217;s
beneficial, it&#8217;s something we&#8217;ve chosen to do, not something that we
absolutely have to do in order for the project or business to keep
operating &#8212; it&#8217;s more frustrating to find myself spending a large
amount of time on.

</p>
</div>

</div>

<div id="outline-container-1.3" class="outline-3">
<h3 id="sec-1.3">In conclusion </h3>
<div id="text-1.3">


<p>
I believe in code review as a powerful tool. But in practice, I&#8217;ve
found it frustrating to work with. I&#8217;d like to hear your thoughts:
Does code review work for you? Am I doing it wrong, in some ways? Is
it just a question of changing my attitude in some way?
</p>
<p>
I know that a lot has been written about doing code review
effectively. I&#8217;d appreciate pointers to anything you&#8217;ve found
particularly compelling.
</p></div>
</div>

<p></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/06/i-hate-code-review/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Using X forwarding with screen by proxying $DISPLAY</title>
		<link>http://blog.nelhage.com/2010/05/using-x-forwarding-with-screen/</link>
		<comments>http://blog.nelhage.com/2010/05/using-x-forwarding-with-screen/#comments</comments>
		<pubDate>Mon, 31 May 2010 00:25:52 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=232</guid>
		<description><![CDATA[If you&#8217;re reading this blog, I probably don&#8217;t have to explain why I love GNU screen. I can keep a long-running session going on a server somewhere, and log in and resume my session without losing any state. I also love X-forwarding. I love being able to log into a remote server and work in [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re reading this blog, I probably don&#8217;t have to explain why I love
<a href="http://www.gnu.org/software/screen/">GNU screen</a>. I can keep a long-running session going on a
server somewhere, and log in and resume my session without losing any
state.</p>

<p>I also love X-forwarding. I love being able to log into a remote
server and work in a shell there, but still pop up graphical windows
(for instance, gitk&#8217;s) on my local machine when I need to.</p>

<p>Unfortunately, X-forwarding and screen don&#8217;t totally play nice
together. When I start a screen session, I fix a value of <code>DISPLAY</code> in
that session, while I can change it in individual shells, having to
remember to do so whenever I open a new ssh session is
irritating. Ideally, of course, we&#8217;d have something like <code>screen</code>, but
for X11, so that X sessions could live in a virtual X server on your
machine, which gets forward to a real X server on demand. I hear that
<a href="http://www.nomachine.com/">NX</a> does something like this, or even just a VNC window.</p>

<p>But in practice, I find I tend to care less about having my X windows
long running. If I&#8217;m popping up a <code>gitk</code> to look at some commits, I
will probably just close it, and don&#8217;t care about it being there
tomorrow. Really, I just want a way for <code>DISPLAY</code> to magically track
the latest X-forwared <code>DISPLAY</code>, so that in any window in my screen
session, I can run <code>gitk</code> or <code>display</code> or such, and it will magically
pop up in the appropriate X server.</p>

<p>So, last week, I finally wrote a <a href="http://github.com/nelhage/x11-proxy">script</a> that lets you do just
that. Instead of futzing with <code>DISPLAY</code>, it works by proxying between
two values of <code>DISPLAY</code>. You initially open a screen with some dummy
&#8220;virtual&#8221; DISPLAY that nothing is connected to &#8212; I tend to use <code>:15</code>:</p>

<pre><code>env DISPLAY=:15 screen
</code></pre>

<p>Then, whenever you log in to the machine with X forwarding (or log in
locally), you simply from:</p>

<pre><code>proxy-display :15
</code></pre>

<p><code>proxy-display</code> starts listening for connections on display <code>:15</code>, and
proxying traffic between there and whatever <code>$DISPLAY</code> was when it was
launched. In addition, it makes the appropriate <code>xauth</code> incants so
that everything just works. It also looks for any old instances of
itself listening on <code>:15</code>, and kills them off, to prevent leaking
processes. Any <em>connections</em> to old instances of itself, however, that
had accepted connections and were now proxying data, are left alone &#8211;
so existing X windows stay open on whatever display they&#8217;re on.</p>

<p>So, now you can just add to your dotfiles something along the lines of</p>

<pre><code>if [ "$DISPLAY" ]; then
    proxy-display :15
fi
</code></pre>

<p>And whenever you <code>ssh</code> into your remote machine and resume your screen
sessions, you can run X programs and they&#8217;ll magically pop up in your
most recent X-forwarded connection.</p>

<p>(The script, available on <a href="http://github.com/nelhage/x11-proxy">github</a>, is currently a disgusting mess of shell that uses <code>socat</code> to do the actual proxying, and mucks around in <code>/proc/net</code> to find old instances of itself to kill. But it works wonderfully, so I haven&#8217;t bothered to clean it up)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/05/using-x-forwarding-with-screen/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting carried away with hack value</title>
		<link>http://blog.nelhage.com/2010/05/hack-value/</link>
		<comments>http://blog.nelhage.com/2010/05/hack-value/#comments</comments>
		<pubDate>Sun, 23 May 2010 23:53:30 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=224</guid>
		<description><![CDATA[Recently, I&#8217;ve been working on some BarnOwl branches that move more of the core functionality of BarnOwl into perl code, instead of C (BarnOwl is written in an unholy mix of C and perl code that call each other back and forth obsessively). Moving code into perl has many advantages, but one problem is speed [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been working on some <a href="http://barnowl.mit.edu/">BarnOwl</a> branches that move more of
the core functionality of BarnOwl into perl code, instead of C
(BarnOwl is written in an unholy mix of C and perl code that call each
other back and forth obsessively).</p>

<p>Moving code into perl has many advantages, but one problem is speed &#8211;
perl code is obvious a lot slower than C, and BarnOwl has a lot of hot
spots related to its tendency to keep tens or hundreds of thousands of
messages in memory and loop over all of them in response to various
commands.</p>

<p>Unfortunately, one downside of the C/perl mix is that it makes
profiling quite difficult. I can run the perl side under NYTProf and
get a good picture from the perl side of things, but I&#8217;ve been
unsatisfied about my visibility into the C side of things. The main
problem is that oprofile and gprof, my usual tools are statistical
profilers, which means that if they sample while the code is executing
inside Perl, they don&#8217;t know which C code called it, and so while I
can tell if a lot of time is being spent in perl, I can&#8217;t tell which C
functions made the calls that are being slow. What I really want is a
deterministic profiler that tracks every function entry and exit, so
that time spent in a perl call can be included in the total time of
the C functions earlier in the call chain.</p>

<p>After doing some quick googling and finding nothing compelling, I
decided to hack up something of my own &#8212; this can&#8217;t be that hard,
right?</p>

<p>The tricky part here is getting a way to intercept function calls and
returns, so that you can timestamp them and then figure out where the
time was being spent. My brilliantly hackish plan was:</p>

<ul>
<li>If you compile your code with <code>-pg</code>, for profiling with gprof, GCC
generates a call to the <code>mcount</code> function early in the prologue of
every function. Normally that calls out into a function in glibc
that keeps stats for gprof, but I can override that with
<code>LD_PRELOAD</code>.</li>
<li><p>The next step is trapping function returns. I wrote a quick program
that loads an ELF using <a href="http://en.wikipedia.org/wiki/Binary_File_Descriptor_library">libbfd</a>, disassembles any text sections
using <a href="http://udis86.sourceforge.net/">udis86</a>, and replaces any RET instructions with the
one-byte <code>INT3</code> instruction, which generates a debug trap.</p>

<p>I could then make my preloaded library install a <code>SIGTRAP</code> handler,
which could inspect the trapped instruction pointer, emulate the
RET, and then return.</p></li>
</ul>

<p>The two parts work independently, but when I glued them together, I
hit trouble. It turns out that <code>gcc -pg</code> dumps some code into the
<code>.text</code> section of your binary, that ends up getting called into very
early in the shared-library load process &#8212; in particular, before my
<code>LD_PRELOAD</code>ed library could install its <code>SIGTRAP</code> handler. Since I
had patched those functions to <code>INT3</code> instead of <code>RET</code>, this crashed
the binary with an unhandled <code>SIGTRAP</code> almost immediately.</p>

<p>After some debugging, I was able to resolve that issue by modifying
the patch program to overwrite the first byte of the offending
programs with a RET instruction (after patching out RETs, of course),
so that they effectively didn&#8217;t get called. Since I&#8217;m using <code>gcc -pg</code>
only for the <code>mcount</code> calls, I don&#8217;t want them, anyways.</p>

<p>After about four hours of hacking, I had a working system which could
take an executable and run it, generating a trace of function calls
and returns as the program executed. As I was settling down to write
some code to do something useful with the output, my friend <a href="http://oremanj.scripts.mit.edu/blog/">Josh
Oreman</a> asked a question: &#8220;Couldn&#8217;t you do this more easily with
-finstrument-functions?&#8221;</p>

<p>Having never heard of this, I pulled up the GCC <a href="http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Code-Gen-Options.html">info page</a>, and
found:</p>

<pre><code> Generate instrumentation calls for entry and exit to functions.
 Just after function entry and just before function exit, the
 following profiling functions will be called with the address of
 the current function and its call site.

      void __cyg_profile_func_enter (void *this_fn, void *call_site);
      void __cyg_profile_func_exit  (void *this_fn, void *call_site);
</code></pre>

<p>Oh. Sometimes it pays to do a little bit of documentation searching
before you get carried away with the hack value of your Awesome
Solution &#8212; someone may have anticipated your problem and written a
real solution.</p>

<p>Oh well. It was a fun experiment, and I refreshed my memory about doing evil things with bfd, udis86, signals, and the dynamic linker.</p>

<p>I still haven&#8217;t yet written the code to turn traces into userful profiling
information, but if I do produce a useful profiler tool, I&#8217;ll post something on this blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/05/hack-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Window Manager I Want</title>
		<link>http://blog.nelhage.com/2010/05/the-window-manager-i-want/</link>
		<comments>http://blog.nelhage.com/2010/05/the-window-manager-i-want/#comments</comments>
		<pubDate>Sun, 09 May 2010 21:08:47 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[ratpoison]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[window manager]]></category>
		<category><![CDATA[xmonad]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=220</guid>
		<description><![CDATA[Since I first discovered ratpoison in 2005 or so, I&#8217;ve basically exclusively used tiling window managers, going through, over the years, StumpWM, Ion 3, and finally XMonad. They&#8217;ve all had various strengths and weaknesses, but I&#8217;ve never been totally happy with any of them. This blog entry is a writeup of what I want to [...]]]></description>
			<content:encoded><![CDATA[<div id="outline-container-1" class="outline-2">
<div id="text-1">

<p>Since I first discovered <a href="http://www.nongnu.org/ratpoison/">ratpoison</a> in 2005 or so, I&#8217;ve basically
exclusively used tiling window managers, going through, over the
years, <a href="http://www.nongnu.org/stumpwm/">StumpWM</a>, <a href="http://en.wikipedia.org/wiki/Ion_(window_manager)">Ion 3</a>, and finally <a href="http://xmonad.org/">XMonad</a>. They&#8217;ve all had various
strengths and weaknesses, but I&#8217;ve never been totally happy with any
of them. This blog entry is a writeup of what I want to see as a
window manager. It&#8217;s possible that some day I&#8217;ll get annoyed enough to
write it, but maybe this post will inspire someone else to (Not
likely, but I can hope).
</p>

</div>

<div id="outline-container-1.1" class="outline-3">
<h3 id="sec-1.1">Layout </h3>
<div id="text-1.1">


<p>
At any given moment, the screen<sup><a class="footref" name="fnr.1" href="#fn.1">1</a></sup>
is divided into one or more <b>panes</b>. These panes always tile the
screen. Each pane may or may not currently be displaying a window. If
it is not, then the desktop will be displayed in that pane.
</p>
<p>
The primitive operations you can perform on the pane include, but are
not necessarily limited to:
</p>
<dl>
<dt>Split</dt><dd>
Splits the focused pane into two equally-sized panes,
either horizontally or vertically. One of the child panes
will display whichever window the previous pane displayed,
the other is empty.
</dd>
<dt>Resize</dt><dd>
Change the relative size of two child windows that were
split from the same parent.
</dd>
<dt>Kill Pane</dt><dd>
Destroy the current pane. If it is displaying a window,
that window is not sent a close message. The pane&#8217;s
sibling window expands to consume its space.
</dd>
<dt>Next/Previous pane</dt><dd>
Focus the next or previous pane. Panes are
ordered based on position on the screen, regardless of how they
were split to arrive at the current layout.
</dd>
<dt>Goto Pane</dt><dd>
Panes are numbered, based on their location on
screen. <code>Mod-N</code> focuses the Nth pane.

</dd>
</dl>
</div>

</div>

<div id="outline-container-1.2" class="outline-3">
<h3 id="sec-1.2">Selecting windows </h3>
<div id="text-1.2">


<p>
There is a global list of all windows. Windows are ordered arbitrarily
in this list (maybe based on the order they were opened?). Every
window has a name, which is initially set to the <code>WM_NAME</code> property
set by the window&#8217;s client.
</p>
<p>
The following operations are available to manipulate windows:
</p>
<dl>
<dt>Next/Previous Window</dt><dd>
Replace the window in the current panel with
the next or previous window in the list.
</dd>
<dt>Rename</dt><dd>
Prompts for a new name for the current window. If the user
enters an empty string, the window reverts to the default
behavior of using the <code>WM_NAME</code>.
</dd>
<dt>Goto Window</dt><dd>
Prompts for the name of a window to switch to. This
prompt matches on substrings or even sub-sequences of
the window name, displaying the result of the
selection as you type. After typing some characters,
you can scroll through the list to select an entry,
instead of completing typing.
</dd>
<dt>Kill</dt><dd>
Sends a close message to the window in the focused panel.

</dd>
</dl>
</div>

</div>

<div id="outline-container-1.3" class="outline-3">
<h3 id="sec-1.3">Desktops </h3>
<div id="text-1.3">


<p>
It seems likely I will want multiple desktops. Each desktop will have
its own pane layout. However, the window list is still global, not
per-desktop. <code>Goto Window</code> will always operate on the global window
list. Selecting a window that is visible through a pane somewhere else
causes that pane to become empty.
</p>
<p>
Alternately, there is no concept of multiple desktops. However, there
is the ability to save and restore layouts, meaning both the layout of
the panels on the screen, and the list of which window is in each
pane. The primary difference here is that a window can be associated
with multiple panes in different saved layouts, and gets resized/moved
as necessary as you switch panes. I suspect I like this model better.
</p>
</div>

</div>

<div id="outline-container-1.4" class="outline-3">
<h3 id="sec-1.4">Postscript </h3>
<div id="text-1.4">


<p>
If this sounds familiar to you, it probably should. What I&#8217;ve
described is essentially identical to how emacs manages buffers and
windows (analogous to X windows and panes in the above, respectively),
with <a href="http://www.emacswiki.org/emacs/window-number.el">window-number.el</a> and either <a href="http://www.emacswiki.org/emacs/IswitchBuffers">iswitchb</a> or <a href="http://www.emacswiki.org/emacs/InteractivelyDoThings">ido</a>. I manage hundreds
of buffers in emacs this way, and complicated screen layouts, whenever
I&#8217;m doing any hacking, and I love it.
</p>
<p>
I would in fact be tempted to write my window manager into emacs
itself, except for the annoying fact that emacs is very much
single-threaded. It&#8217;s already annoying enough when network drops and a
hung network filesystem takes down my emacs waiting for a timeout; It
would be utterly unacceptable if that took down my entire window
manager, too.
</p>

<p>
I&#8217;d alternately be tempted to try to make this an XMonad plugin, but I
think that it&#8217;s sufficiently different from XMonad&#8217;s data model that
the impedance mismatch would suck.
</p>

</div>
</div>
</div>

<p><div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<p class="footnote"><sup><a class="footnum" name="fn.1" href="#fnr.1">1</a></sup> I&#8217;m only going to consider the
single-monitor case for now, but the generalization should be easy.
</p>
</div>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/05/the-window-manager-i-want/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Software Engineers should keep lab notebooks</title>
		<link>http://blog.nelhage.com/2010/05/software-and-lab-notebooks/</link>
		<comments>http://blog.nelhage.com/2010/05/software-and-lab-notebooks/#comments</comments>
		<pubDate>Mon, 03 May 2010 03:14:14 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[lab notebooks]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=217</guid>
		<description><![CDATA[Software engineers, as a rule, suck at writing things down. Part of this is training &#8211; unlike chemists and biologists who are trailed to obsessively document everything they do in their lab notebooks, computer scientists are taught to document the end results of their work, but aren&#8217;t, in general, taught to take notes as they [...]]]></description>
			<content:encoded><![CDATA[<div id="text-1">

<p>
Software engineers, as a rule, suck at writing
things down. Part of this is training &ndash; unlike chemists and
biologists who are trailed to obsessively document everything they do
in their lab notebooks, computer scientists are taught to document the
end results of their work, but aren&#8217;t, in general, taught to take
notes as they go, and document the steps they take in building a
system. 6.005, MIT&#8217;s new introductory software engineering class,
attempted to require its students to keep lab notebooks for a few
semesters, and was met with near-universal complaints and ridicule
from the students (“Lab notebooks? For a software engineering class?
What the hell?”). (To be fair, I suspect they did a horrible job of
it, but I&#8217;m not sure that students would have been any less confused
at the idea).
</p>
<p>
Part of the reason is probably also the nature of software that makes
it very easy to record certain things as part of our tools, and that
makes experiments cheaply reproducible. Version control lets us
document the process of developing a piece of code, so it feels
superfluous to be taking additional notes on the side. Computers are
mostly deterministic, and cycles are cheap, so why bother meticuously
recording the results of a test run somewhere when you can just run it
again later, any time you want? Computers feel much neater and simpler
than messy bio or chem labs, and software is much simpler than
complicated biology experiments or chemical syntheses, and so no one
feels the need to be nearly as careful.
</p>
<p>
However, I am increasingly of the opinion that most software
engineers&#8217; total inability to work in a lab-notebook style, where you
meticulously document your work, is unfortunate and often seriously
detrimental to their work. While it&#8217;s true that things like commit
logs do a good job of documenting certain processes, here are some
types of situations where I&#8217;ve found working with meticulous notes at
every step can be invaluable:
</p>
<dl>
<dt>Debugging subtle problems</dt><dd>
Debugging is very much a problem of
gathering data and making and testing hypotheses. For subtle bugs
in large programs, the amount of state you need to keep track of
can rapidly get out of hand. And good luck when a bug is tricky
enough that your debugging gets spread across multiple days, or
even across a lunch break.

<p>
If you&#8217;ve ever found yourself wondering &#8220;Wait &ndash; did I see the
bug after I made $CHANGE to my code or test environment?&#8221;, you
should have been writing more things down.
</p>
<p>
This is especially important for non-deterministic bugs, such as
rare race conditions. If it takes you half a hour on average to
reproduce a bug, and you are experimenting with a dozen different
variables in your test environment that might affect the bug, you
can&#8217;t afford to forget the results of a single test, or to forget
a single detail of what you did to test. This is the point at
which you should be writing down every single command you type in
any relevant prompts, and every single code change (or, since we
have technology, obsessively saving the output of `history`,
making commits to test branches, and recording the correlation
between them).
</p></dd>
<dt>Profiling and optimization</dt><dd>
This is a process similar in many ways
to debugging, but even more data-driven. When you&#8217;re done with a
session of optimizing a piece of code or a system, if you can&#8217;t
show documented evidence of exactly how much faster you&#8217;ve made
it, where that speedup came from, and all the things you tried
and how much they helped, perhaps you should be writing more
things down. And if you (or ideally, even someone else) can&#8217;t go
back and reproduce the experiments you did, with approximately
the same results, you probably haven&#8217;t been documenting your work
well enough.

<p>
Even if you&#8217;re happy with the performance improvements you&#8217;ve
made, you may need to come back and wring even more performance
out of the system, and it&#8217;d be nice not to start from scratch. Or
maybe future testing will reveal that or more of your
optimizations was invalid, and you need to go back and consider
alternate options.
</p>
<p>
This is critically important when you&#8217;re optimizing not just a
piece of code, but some kind of system with lots of configuration
and setup, that you&#8217;ll later have to duplicate somewhere else,
instead of just checking the result into source control.
</p></dd>
<dt>Understanding a new project&#8217;s code or documentation</dt><dd>
Whenever I&#8217;m
first diving into a large code-base or first playing with a large
new API, I find it invaluable to take notes as I go about what I
look for and where I find it. I&#8217;ll often need to look up half a
dozen different API calls or pieces of code to understand
something, often too many to keep in my head as I go and dive
through more and more pieces of code or docs.

<p>
And when the documentation is ambiguous, I&#8217;ll often drop into a
REPL or build test programs to make various calls and understand
what happens. Again, after more than two or three of these, it&#8217;s
vital that I&#8217;ve been writing down my findings.
</p>
<p>
This is one example where a chronological style documenting
exactly in what order I found things is less critical, but that
detailed notes as I go are still vital.
</p></dd>
<dt>Designing things</dt><dd>
Whenever you&#8217;re designing something &ndash; be it an
API, a protocol, an interface, some kind of system, or something
else &ndash; it&#8217;s worth taking notes on the process you took to get to
your final decisions, and the choices you considered and
rejected, and why.

<p>
You&#8217;ll presumably end up a producing a piece of code or a design
document that indicates what you ended up deciding, but
understanding why you made the decisions you made is often
important to understanding how your system is supposed to work,
and how to best use or extend it in the future. Hopefully, when
you&#8217;re done, you&#8217;d do this writeup in brief somewhere anyways,
but the best way to make sure you don&#8217;t forget is to take good
notes as your thought process happens.
</p>
<p>
And nothing in software is ever complete. If you have to revise
the design for some reason, because someone points out problems
or new requirements come up, you&#8217;ll probably want to remember the
other possibilities you came up with &ndash; maybe one of them is now
more right.
</p>
</dd>
</dl>

<p>So, if you&#8217;re a software engineer, I strongly encourage you to try to
get better at writing things down. In a future post, I&#8217;ll hopefully
write up the techniques I&#8217;ve started using to take notes as I code,
debug, and design, but in the meanwhile, I encourage you to just grab
a text editor or a physical lab notebook, whichever is more
comfortable for you, and start taking more notes on what you&#8217;re doing.
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2010/05/software-and-lab-notebooks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
