I think I’ve changed.

There was a time, oh, maybe 20 years ago or so, when I really wanted to write the next great thing. A new drafting tool, maybe, or a new software tool for managing software versions, or something… Partially I wanted to come up with something because it was intellectually challenging, and partially so I could make enough money to be comfortable.

Today, I still would love to sit down and come up with some software product to build; something which would challenge my programming skills. Maybe a spreadsheet in Java, or a drafting tool or something–something which would intellectually stimulate me. But I find myself wishing I had more and more time to build cabinets or boxes from scratch.

I think it has something to do with the fact that I find that I love the process of creating–and perhaps because there are very few problems that I see which challenge me that wouldn’t require me going back to school and getting a Ph.D.

And money wise, I’m comfortable. Not rich, which I would define as having enough money to allow me to maintain my standard of living without ever having to work again. But I’m definitely comfortable: I have most of what I want, and the things I don’t have–well, it’s either something that would require a major lifestyle change (such as a cabin in the woods), or it’s something that honestly would be more trouble than its worth.

So I find myself not really wanting the time to go off and start the next great Internet “thing”–because I’m comfortable. (Which, on a side note, explains why the next great thing generally is built by the young: they have less to lose and much more to win than someone who already has a wife, a nice car, and a nice house in Los Angeles.) Perhaps I will figure out the next cool thing and go off and start a new company–but because it’s an extension of my career path, not because I have this burning urge to ‘make my mark.’

Really, though, I want to just have a week or two where I can build a set of home-made cabinets and book cases. Something with flowers carved into the stiles, maybe.

Yesterday’s Design Patterns

I blog for a variety of reasons. To practice my writing and communications skills. To vent frustration in a safe place. For public intellectual exhibitionism.

Today I blog to remember something I had forgotten.

A design pattern, generally speaking, is a common pattern that we use to solve a problem. Unfortunately that observation has been reduced by most programmers I know into an exercise in name calling–and like the practice in high school of teaching history as a dry list of dates and event names, has a way of turning a wonderful observation as to how problems can be solved using common themes into mindless recital. (The next time someone tells me “why didn’t you use a fubar pattern here?” I’m going to smack them.)

Design patterns, however, are not just a dry list of names and UML class diagrams, just as history is not a dry list of dates and event names. And some design patterns from yesterday are nearly forgotten–at least I had nearly forgotten them–which as far as I’m concerned is the same thing.

All this came about because I was reading the paper Regular Expression Matching Can Be Simple And Fast. Because I’ve spent the past couple of months locked in meetings and other brain-scrambling exercises, I figured it would be good to exercise that most important muscle, the brain, on an interesting problem: writing my own regular expression matcher engine in Java, and do it without looking at the code in the paper. (Yes, I know: I can just use java.util.regex, or compile the code in the paper–but the point here was not to mindlessly go “oook! regular expression!” like a brainless monkey. The idea here was to engage in a mental exercise. Do you think Sudoku puzzles are pulled out of thin air without first computing a solution then working backwards to create the puzzle?)


The first design pattern comes about in the discussion of the Regular Expression paper; this is the State Machine. While I’ve linked the Wikipedia paper, the key thing to remember is that a state machine consists of a set of states S and a set of actions A, such that each state {s1} triggers action a1 and transitions to a new state s2 depending on the action’s outcome.

Typically states are represented as an integer, and your state machine code often looks like:

int state = 1;
boolean done = false;

while (!done) {
    switch (state) {
        case 1:
            state = doAction1();
            break;
        case 2:
            state = doAction2();
            break;
        ...
    }
}

What’s great about state machines is twofold: first, it helps break a computational problem into small chunks of code. This can be useful if you need to write code that fits into a small footprint (like an embedded processor you’d find in a remote control or a watch or calculator), and is especially useful in this context if you are emulating a multi-threaded process in an environment where you don’t have a multi-tasking operating system to rely upon. (I once built a print spool server that ran on a 6809 embedded processor without any operating system by building the entire network stack as a sequence of states in a state machine. I could then monitor up to 8 separate incoming network connections at the same time simply by having my program walk through 8 separate chunks of memory which held the state for each incoming connection and running the state machine in each: update state in slot 1, then update state in lot 2, then…, then update state in slot 8, then repeat with slot 1, etc.)

The second thing that is great about state machines is that it allows you to represent a dynamic program which is built from some input string (such as a language specification in BNF form or a regular expression parser); in this case, each state is not an integer in a switch statement, but an object in a linked list of objects. It’s great; rather than hard-code how your program works, you load the string, build the state machine, and your execution code simply works the state machine until it’s done.


The second design pattern that came about when creating the regular expression parser–and I’m kicking myself because it took me four hours to notice this last night (because of the brain-scrambling effects of managerial meetings) is the shunting yard algorithm, which is used to convert a statement in infix notation into a parse tree which can then be turned into a state machine or solved or turned into assembly language code.

Typically the shunting yard algorithm is used to solve algebraic equations in infix notation, so we can convert the string “34 + 4*5 – (-3 + 4) * 8” into a parse tree (shown here in LISP style notation) (- (+ 34 (* 4 5)) (* (+ (- 3) 4) 8)), which can eventually be solved:

(- (+ 34 (* 4 5)) (* (+ (- 3) 4) 8))
=> (- (+ 34 20) (* (+ -3 4) 8))
=> (- 54 (* 1 8))
=> (- 54 8 )
=> 46

The nice thing about the shunting yard algorithm is that it works for any string which uses a series of prefix, postfix and infix operators: we could use it to solve, for example, -(1+2) + 3!, where the first minus sign is a negation operator, and the “!” is a factorial operator.

The algorithm itself is rather simple: you need an operator stack (a push/pop stack which stores the operators you’re using, such as ‘+’, ‘*’, etc), and an ‘eval’ stack, which can be as simple as a push/pop stack of numbers or a stack of parse trees or whatever intermediate representation you’re using to store the equation. Then it’s just a matter of reading the string as a series of tokens: each value is pushed onto the eval stack (popping prefix operators as needed after the push, updating the eval stack as needed), and each operator is pushed onto the stack, popping and evaluating operators that are of lower precedence. The details are in the Wikipedia article linked above.

What took me four hours to remember–and why I’m upset at myself today–is that this algorithm is not just good for solving math equations, but is useful for any sort of notation which is expressed as a series of prefix, infix and postfix operators.

Such as regular expressions.

When you get right down to it, a regular expression is a series of values (letters or characters you’re matching against), with a series of implicit and explicit operators.

‘Concatenation’ is an implicit operator here. Using # to represent the concatenation operator, it’s easy to see that /ABC/ (which matches the substring “ABC” in DABCDEF) can be represented as ‘A’ (i.e. “match letter ‘A'”) # ‘B’ # ‘C’.

The ‘|’ (or ‘or’ operator), which for the expression /ABC|DEF/ matches either “ABC” or “DEF”, is an explicit infix operator: the regular expression /ABC|DEF/ can be represented by the parse tree (in LISP notation): (| (# A B C) (# D E F)).

And the ‘*’ (match 0 or more), ‘+’ (match 1 or more) and ‘?’ (match 0 or 1) are all postfix operators with higher precedence than the concatenation operator: ABC* matches the string ABCCCCCCCC or AB and all in between.

The point is that all of this (including the ‘[…]’ character group, which is simply a single value object matching multiple letters, and the ‘(…)’, which operates in the same way as the parenthesis in an algebraic statement) can all be parsed using the shunting yard algorithm.


As an aside, what’s really spifilicious about the regular expression algorithm in the paper is the observation that a state doesn’t have to be a single integer value, but can actually be an array of values, with each value representing a specific action to carry out–and a single action can spawn two or more values representing two or more actions to carry out at the same time.

For example if you have a regular expression ‘a*a’, essentially meaning “match 0 or more ‘a’ characters, followed by a single ‘a’ character, and you’re matching against “aaa”, then is the second ‘a’ in that string part of the ‘a*’ (match 0 or more ‘a’ characters), or part of the terminal ‘a’ (match a single ‘a’) character? In the case of the algorithm outlined in the regular expression paper at the top of this article, the answer can be ‘both!’ In other words, you can maintain a state which is an array of vectors, and if we represent our regular expression ‘a*a’ with ‘a*’ as 1, ‘a’ as 2, and ‘done’ as 3, we could have our state go from {1} (read first ‘a’) to {1,2} (read second ‘a’) to {1,3,2} (read third ‘a’) to {1,3,3,2} (read end of string) to {3,3,3}: that is, we can simultaneously be doing multiple things at once.

This, by the way, is the point of the paper: by maintaining multiple “states” at once, or rather, by representing our current match state as a vector rather than backtracking (that is, rewinding the matcher back so we can try an alternate matching strategy), we can solve degenerate matching cases without having the program grind to a halt.


I’m blogging this so I can remember. And what I want to remember is this: that the shunting yard algorithm and the state machine are cornerstone design patterns nearly a half century old. For a regular expression, they both can be brought to bare on the problem: the shunting yard algorithm can be used to build a parse tree from a regular expression, and the parse tree can then be used to build an optimal state machine for matching strings against the regular expression.

And more importantly, they are two forgotten tools in a very old toolbox which are a hell of a lot more powerful than the ‘singleton design pattern’, which, to be honest, barely deserves to be named outside of the halls where computer scientists engage in taxonomy for the purpose of obtaining tenure.

Long Term Brain Scrambling

Here at Yahoo! (and you can tell I work for Yahoo! by adding the damned exclamation point–the company name is “Yahoo!”, not just “Yahoo”) I’m finding myself traveling about once a week to the mothership where I meet with many people to help coordinate and manage a large scale integration project.

And every evening I go home, sit in front of my computer thinking about hacking–but between taking care of an elder friend of mine and the events of the day, I find myself looking at code that should be trivially obvious–and seeing greek instead.

One thing that struck me about Paul Graham’s essay Holding a Program in One’s Head, he talks about the things that can cause a programmer to become inefficient. Basically, as a programmer you hold a program in your head–and there are events (meetings, destractions, etc) which scramble your brains, causing you to spend a half hour (at best) reloading your mind with the problem you’re trying to visualize.

I think it’s worse than that: I think all management functions are designed to scramble your (programming) brain.

And long term exposure may cause permanent brain damage.

Management is essentially incompatible with software development: it’s why the best ideas usually come from either small companies or small business units in large companies–business units which are essentially run like small startups.

So for the past few months I’ve been dealing with what are essentially management functions: communicating to other teams, coordinating actions, creating delivery dates and filing expense reports.

Hopefully I can get out of this loop before permanent damage sets in and I can no longer code my way out of a paper bag.

Update: A friend pointed me to the following site which can help exercise your programming brain: Project Euler, which provides a number of mathematical problems which can really only be solved by writing a short snippet of code. Some of the problems they have wind up being o(N!) or o(2N) if you take the trivial solution–causing the amount of time to solve the problem to explode, unless you also figure out a more efficient algorithm.

If I can’t clear my head this weekend, I may just spend a few hours solving problems from the Euler site.

Twenty Years (at least!) of integrated development environments

Back in 1987 or so, a small company put together an interesting development tool on the Macintosh called “LightspeedC”, which was later renamed “Think C” for legal reasons. What made this tool exciting is that rather than using a series of ‘makefiles’ and command-line tools for building your application, the development environment was integrated into two tools: a development environment which contained a text editor and a compiler, and a separate debugging tool which was automatically launched when you started debugging your code.

What made all this really exciting was that it was an “all-in-one” environment: while it was not as flexible as Unix ‘make’, if you played within the restrictions imposed by the IDE, you got one-button compilation, one keystroke compile and debug, and the ability to double-click on an error message and have the program automatically put the cursor at the point where the error occurred.

This was revolutionary and influential–though there were earlier IDEs, including an old tool called “Hippo C” on the Macintosh which boasted a similar (though less powerful) integrated environment for developing code.

The key to developing code is the “edit/compile/debug” cycle: the faster and more transparently you can go from ‘edit’ to ‘debug’, the faster you can make changes to your code, and the less code you feel like you have to write between testing your changes. If it takes less than half a second or so from changing some code to seeing if that code works, you can damned near debug code at the same time you write it.

And an integrated development environment allows this to happen: with the old method of a separate editor (vi, say), you would make your code changes, escape out of vi, go to the command line and type ‘ant’ or ‘make’ or whatever, wait for the code to compile–write down the line number (or open up a separate window) where the error occurred, open up vi, change the mistake, exit vi, run make again, start up the debugger tool (assuming you even have one!), run your program–it’s a mess.

In Eclipse, the compile error is shown as you type–so you fix your errors as you make them. And to debug, you press the ‘debug’ button.

I note this because for some strange reason I cannot figure out, even though we have the most powerful development tools on the planet today–tools which even make the goodness of ‘Think C’ look like primitive flint knives and crudely crafted clubs, for some fucking reason I cannot fathom, the code I’m working on is still built using command-line tools and editors first invented in the 1970’s!

There is nothing about our project that cannot be handled in Eclipse or NetBeans or the JetBrains IDEs–yet co-workers who are younger than ‘vi’ insist upon using build methods that are older than they.

Next think you’ll know, they’ll insist upon giving up their cars and commuting to work by horse-drawn wagons.

And now for something completely different.

When I started this blog, I wanted to have a place where I could put my technical rantings. If you knew me in person, you’d realize that I’m rather passionate (in a “meet my little friend” beat people to death with a baseball bat and bury the remains in the back yard under the flowers sort of way) about quality software development.

This is a completely different rant.

I have a friend, an elder gentleman who was in the process of having all of his savings squirreled away by an unscrupulous fellow who served as his “power of attorney” and was going to ship my friend off to a hell-hole in some God-forsaken part of the country to die.

And so I stepped up to help.

Not to go into the details, but we’re slowly unraveling the problems he had in his life, and we’re in the process of putting his life back together. My wife is going to help clean up his apartment; we’ll get him a new television set and a new bed, get everything organized, just the way he wants it–so when he goes back home, everything is just perfect.

Bottom line–and I cannot possibly feel more strongly about this: If you are unwilling to be an advocate for someone who finds themselves in trouble, then you have absolutely no business asking for an advocate when you find yourself in trouble.

You mean people don’t do this?

Paul Graham has, as usual, an excellent article on the art of software development: Holding a Program in One’s Head

I have only one question. In his article he writes:

You can magnify the effect of a powerful language by using a style called bottom-up programming, where you write programs in multiple layers, the lower ones acting as programming languages for those above. If you do this right, you only have to keep the topmost layer in your head.

You mean people don’t do this?

In my mind this is the first Design Pattern; the most important one from which all other Design Patterns derive from and pay homage to. It was the most important thing I learned in college when studying networking models. It was the key to my understanding when I started writing software for the Macintosh under System 1. (Yes, I’m that old.) It helped me crack object oriented design. This design pattern: that you develop software in layers, which each layer supporting the one on top of it–this pattern is so ingrained in my mind that I cannot think of writing software any other way.

You can see a beautiful example of this approach in the TCP/IP networking stack: each layer of that stack from the hardware to the application layer is designed to do a very simple and predictable job, and to support the layer on top. The magic of sending a packet of data reliably across the world in an error-prone environment works because of a layer of components, where each component is designed to exactly one thing and do that one thing well–and each component relies upon the services provided by the service below to do that thing well.

My first parser was built this way: I wrote a layer of software that did nothing but tokenization, on top of a layer that did nothing but identify comments and stripped them out of the input stream, on top of a layer that did nothing but read bytes and track the current line number. And those layers sit on top of an operating system call that itself is built in layers: from a layer that does nothing but read blocks from the disk and spools them out in chunks at the request of the application to a layer which does nothing but communicate with the hard disk via a simple protocol, to a layer in the hard disk responsible for stepping the read head motor and interpret the magnetic pulses on the platter.

And on top of this tokenization layer was another layer which did nothing but read in and interpret the tokens representing the top level constructs of the language, which then called subroutines which did nothing but handle the language constructs for small pieces of the language.

I wrote (and have on my web site) an ASN.1 BER parser; it does nothing but handle reading and writing BER encoded byte streams. A man (whose name escapes me now because I’m at work–me culpa) has been in contact with me about writing an ASN.1 parser which uses my BER parser to handle tokenization; I’ve advised him to write code that handles the parsing, validation and interpretation of an ASN.1 specification. And in some cases I’ve said that essentially the BER parser layer isn’t the right place to handle certain features in an ASN.1 parser, such as validation of the input data stream.

See, part of the art of programming is also understanding what belongs in a layer, and what belongs in a new layer. Just as much as the art of programming is about understanding what pieces are part of the same functionality–and should therefore be part of the same layer.

At its root, each layer should be simple, self-contained, and do exactly one thing and do that one thing well. So if the code you’re working on doesn’t do this, then get up from your computer, go for a walk, and think about how to break it up into the proper layers.

Why Observable/Observer Should DIE!

I’m not talking about the design model here, but the java.util class and interface. From a development standpoint the interfaces should die a painful death and here’s why:

(1) It violates the rule of discovery–that a newbie to the code should be able to discover easily what is going on. If you’re maintaining some code and you need to change the object you’re passing to notifyObservers, because the object being passed is a generic Object, you have no good way to discover which Observers would be affected. Two different Observers will have the exact same interface–but one may be affected while the other is not. You have no good way to figure out through simple search which observers are observing which observables.

(2) Like the dreaded ‘GOTO’, it makes it easy to write spaghetti code. One of the bad habits in Java is for people to create inline anonymous classes to handle events and state changes–and while this makes the original author’s life easy, it makes figuring out what little ‘classlet’ or snippet of code functionally belongs to another chunk of code. You may have a toolbar with five different buttons which seem functionally related–but the code which processes the contents may be scattered throughout your code.

This ability to create little anonymous inline classlets of functionality is Java’s equivalent to the GOTO: easily abused, it creates spaghetti code real quick. And observers simply encourage this practice.

(3) It potentially introduces event order dependencies. While an observer should preferably be used to update an unrelated element when a state changes–such as highlighting the ‘window dirty’ icon in a document window, in many cases I’ve encountered the observer object is used as a sort of “cross-cutting” functionality enabler–with cross-cut functionality added in a way which creates hard to understand dependencies.

I’ve got one class here where the notification creates a state change which later code in the observable depends upon. In other words, the observable depends upon the state of the observer to change before the observable can continue: fail to register the observer, and the observable fails.

It’s the dreaded GOTO all over again, except worse: it’s data driven GOTOs…

Like perpetual motion machines and anti-gravity…

After reading this article on Slashdot: Hiring Programmers and The High Cost of Low Quality, and simultaneously attending an internal meeting where they discussed an internal reorganization and the promise to “execute” on a new project using “agile development methodologies”, I just had an epiphany.

Software development methodologies, such as “Agile” and “XP” and “Scrum” and the like are all attempts to build a perpetual motion machines: they are all attempts to reverse the laws of physics discovered fourty years ago, especially the observation that the productivity difference between an excellent engineer and a poor engineer can be more than a factor of 25. Each of them promise management that they can overcome the barriers in finding good programming talent by imposing some sort of order. And each of them ultimately either fail–or at best manage to allow a company to limp along.

Anti-Patterns: Roll Your Own

Not quite the class inheritance error I had originally planned to write about, but something that is currently biting me in the ass.

The project I’m working on uses a Java Swing table. The way they use the table is, um, “unique” in that “Oh My God What Were They Thinking!” sort of a way. The fundamental problem here is that rather than using the existing Swing class and interface hierarchy in order to create a new table model to feed a JTable object, they instead rolled their own code that sort of kinda uses the JTable stuff, but in their own unique way.

They have their own ‘model’ object, for example, which doesn’t have anything to do with the TableModel interface in Swing, and which uses their own ColumnSpecification class–which again has nothing to dow with the TableColumnModel interface in Swing. Perhaps once I wade through this pile of code I’ll find that their way is more streamlines and more efficient to the task at hand. But right now, none of the stuff makes any sense. I even found one case where they appear to be using a table model object as a ‘flyweight’ table object–which makes no sense, as there are only two tables here.

At home I’ve been tinkering with a piece of code which creates a database connection to a remote SQL server and runs a whole bunch of queries in parallel. I came up with what I thought was an extremely clever way to queue requests up into a pooled connection manager which would pull my requests off and run the request by translating it into a SQL query in one of several pooled background threads.

I plan to rip it all apart.

Why? Because there is an accepted way to do J2EE cached connections and cached prepared statements–and while my design is probably fairly clever, it also doesn’t do things the way it’s normally done in J2EE world with a DataSource object obtaining connections from the database.

And that’s the anti-pattern: rather than sort your way through a whole bunch of puzzling interfaces and use the accepted pattern that the designers of a class expected you to use when writing SQL queries or creating JTables in Swing, you instead decide to roll your own. While your creation may be quite solid and impressive–because it’s not the standard way to do things the next maintenance developer to come along will bang his head against a brick wall trying to sort out what you did while you move on to create new ‘self-rolled’ anti-patterns elsewhere.

For me, what should have been a 30 minute change to their source base–adding two new columns which can be dynamically turned on and off as needed–has turned into a three day nightmare of wading through foreign (and completely unnecessary) class hierarchies and odd-ball problems, such as the apparent abuse of the fireTableStructureChanged() method to indicate a request to change the structure rather than a notification that the structure has changed.

Many years ago Apple had a technical note describing this very problem. They called the problem a failure to understand not just a particular interface, but also a problem to understand the “Gestalt” of that interface. You may be using the interface in a way which accomplishes what you want–but if you don’t use the interface in the way it was intended to be used (because you don’t understand the gestalt of that interface)–you may be screwed heavily later on down the road.

Sun provides a number of Java tutorials on the different Swing components–including tutorials on the JTable class. What these tutorials are good for is not in learning how to build a table–someone could in theory roll their own table class without ever touching the JTable class–but in learning the gestalt of the existing table implementation.

And even though you may not run into dire problems by not understanding the gestalt of an interface (Apple’s tech note was dealing more with interface abuse, such as reaching into a memory handle and fiddling the bits there in a way which was documented as private–but documented nonetheless), you will make the maintenance programmer’s (or you, six months or a year later) life a living hell.

Anti-patterns: improper inheritance.

While examining some other code I’ve encountered a bunch of stuff which makes me wonder what they’re teaching people nowadays in school. Rather than silently complain about the problem to my co-workers, of course I decided instead to use this forum to rant.

Today’s anti-pattern: improper inheritance.

The problem: A class ‘ClassA’ is defined which contains two features–call them ‘feature 1’ and ‘feature 2’. A class ‘ClassB’ needs to adopt ‘feature 1’ in ‘ClassA’, but also add new features which are somewhat orthogonal to the functionality in ‘ClassA’, ignoring ‘feature 2.’

When a software maintainer comes around to change ‘feature 2’ in ClassA, he finds that ClassB breaks–ClassB was written in such a way as to hide or never initialize ‘feature 2’ within ClassA in the first place.

Why this happened: The software developer writing ClassB saw some of the code he wanted to reuse, but rather than deal with the problem of properly abstracting ‘feature 1’ out of ClassA, he instead took advantage of subclass-level visibility of the internal fields in order to bypass ‘feature 2’.

What the developer should have done: Take ‘feature 1’ out of ClassA and create an abstract super-class ‘ClassC’, then rewrote ‘ClassA’ and created ‘ClassB’ to inherit from ‘ClassC.’ Thus it’s clear that the functionality in ‘feature 2’ isn’t used in ClassB, and ClassB doesn’t have to rely upon supernatural knowledge of ‘ClassA’ which could be broken in an upgrade or bug fix.

What this illustrates: A class in object oriented design should be a self-contained unit of functionality. If a class is well designed, it should have well-designed extension points and a well-designed feature set which acts as a single unit.

When well designed, other classes do not have to rely upon super-natural knowledge of the implementation of a class in order to operate correctly; the implementation knowledge–while relatively transparent so that users can know how the black box should operate–should be implemented as if it were a black box.

When a subclass has to rely upon supernatural knowledge or relies upon knowing which calls touch certain internal states related to a feature that the subclass doesn’t want to enable or use, then this clearly shows the need to refactor the superclass.