Friday, January 11, 2008

Do What I Mean

Computers always do precisely what you tell them too. That's the problem. People don't do that. They understand what isn't said. They know about context, past experiences, accepted norms, what doesn't make sense, and can look at what they are given and figure out what it is and what it can do. When learning programming many people often get frustrated quickly when they realize computers by default do none of the above. You need to break problems down into smaller and more specific chunks than you are used to, and there can be a big up front learning curve to knowing what the building blocks are.

I got to thinking about this while working on a project to covert some code from Actionscript 2 to 3. Actionscript (AS) is part of Flash, a Macromedia, and now Adobe product that started as a design application and slowly morphed over time into a rather capable software development tool. Designers are an unusual bunch, and they think us programmers are an unusual bunch. They tend to be some of the least technical people, but their work is done in some of the most complex and expensive software out there. The special hallmark of design software, is that it is focused around "do what I mean", letting you see a result right away, and then working in a feedback loop with the user to fine tune it.

So when I first heard that AS3 is strongly typed, I was really surprised and dismayed. Actionscript 3 borrows a lot of Java's ideas of how programming in the large should work. Java is great at programming in the large, and it also is great at making everything into a large program. It works great for big corporate bean counter types, since its static typing, and almost complete lack of anonymous data structures, make it easy to count the beans. In order to do that you have to write a lot of code. There are no assumptions, nothing that tries to look at what you are doing and fill in the missing bits with reasonable defaults, or switch its behavior.

Object oriented isn't good enough. People don't need to be told what object they are looking at, they can figure it out. They don't need to be told one object can do something and an other can not, they can look at it and figure it out. People can communicate in free form associations, without having to declare all the possible things they might say beforehand.

So how do you "do what I mean?". Its all about introspection and anonymous data. Write objects that manage sets of other objects, that can look at them and see what abilities they have (available methods), and which they do not, and use them accordingly. Take arguments as hashes (associative arrays, dictionaries, anonymous objects, whatever you like to call them). They let you say what you need to without either side needing to know all the possibilities. Use roles, so new objects can be built up by joining together existing ones, and make those roles do all the things I just mentioned.

Think of design software, showing a result right away and then working in a feedback loop with the user to fine tune it. This is how I write code. I start with something small, and work in a feedback loop with my debugger making correct and larger as I go. If you API does the above, working this way will make a lot of sense. If you are building a new API, doing the above will make it expandable without breaking code that already depends on it.

"Human languages ... differ not so much in what you can say but in what you must say", Larry Wall, from "Programming is Hard, Lets go Scripting...". This statement, and the linked article, sum up where I am going with this quite nicely. I'm mostly a Perl programmer, and Larry Wall is the father of Perl, a highly dynamic language, some might consider the antithesis of Java.

AS2 was a lot like JavaScript, an other highly dynamic language. You could change definitions of objects, throw anonymous data around, use functions as objects, and do all sorts of introspection without too much trouble. In the Javascript world these things make wonderful libraries like prototype.js possible. AS3 now turns most of that off by default, preferring its Java-like static way of doing things, and making you jump through hoops to get back to the nice old dynamic world. AS2 had a horrible API to the flash runtime, but that wasn't the language's fault. What AS3 now needs is an API to make it dynamic again easily, letting the developer choose which approach is best, and bringing the magic back.


No comments: