Archive for August, 2006

perceptions of programming

August 15th, 2006

I remember when I was much younger and I first started programming, it was such an exciting thing to do. I mean nowadays I think of it as normal, but back then it was so unique, so unlike anything else I ever did. I wasn't too good at it, I'm not terribly gifted at coding to begin with. And I didn't do *that* much of it either, there were always distractions in the form of games, later internet forums and whatnot.

But it was fun to *create* something and I certainly tried a bunch of times back in high school, we even had sort of a club for it. I remember that I always wanted to create something brilliant, something really awesome. And I didn't know the first thing about it, I was clueless. So I would just sit down and start coding. Whatever came to mind that had to be done as part of my project, I started out with that. A program that encrypts files (encryption, that was a favorite theme back then)? Ok, I need to read the file first. So I would code that to begin with, reading the contents of a file, I didn't think any further at all. Then once I did that, I would start thinking what do I need next. And so on. So working in that fashion, it's no big surprise that my code wasn't of the highest standard. I didn't read any books either, I just searched for code examples and I used the help files for the IDE. (Borland Pascal had a really great integrated api doc, very handy.)

So when I started computer science in college, that was all I knew about coding. Basically just trying to put an idea into code, however it was done, just as long as it worked. And didn't take too long to do it. And didn't crash. Mission accomplished. And we did a couple of those missions, most never got completed, cause we didn't know how, some worked just fine, which was a great satisfaction, but ultimately we didn't solve too many problems doing it.

I recall that the concept of "something awesome" intuitively was that great piece of inspiration. I knew intuitively that I wanted my code to be as good as it could be, as organized and structured and all that. But I didn't know _anything_ about how it *should* be done. I didn't know any theory, any principles, guidelines, practices. I sorta started from scratch. So college really changed that point of view, I learnt what developing systems means, what it entails, what people actually do, and how. But then I had assignments and again I would try to write that great piece of code, which I had no idea how it would even be determined to be good or bad.

My concept of great programming was that good idea. It's a bit like a painter would rig up a new canvas and just sit there waiting for inspiration. How do I paint a masterpiece? I just wait for that amazing, great idea to come along. I will not settle for mediocre ones, I will ignore them, I won't compromise on this. And when that briliant idea comes to me, I'll use it and I'll paint a masterpiece. That's how I thought I could write something awesome. This, of course, is the Big Design Upfront mentality, with pretty much the worst possible starting point (ie. complete ignorance and zero experience) for actually designing something good. But I really didn't understand anything about modularity. I would think to myself that "this encryption bit should be exported to a separate file", but I had no clue about how it should be interfaced, what logic it should and shouldn't have, only that it should be separate because that "seems like a good idea". I would start a lot and end up nowhere, just fumbling in the dark.

When I did have some "great idea" about how to solve some problem, it was always rather complicated. I had such faith in my scheme. I was confident that beside solving the basic problem, I also solved other issues that other people "probably wouldn't have thought about". My first assignments in college were all like that, based on some idea of how to improve upon the "bare minimum" by making things more complicated than they had to be. And it would obscure the issue, because there was lots of unnecessary additional logic in it. I was disheartened, time after time, to learn that the simplest way of doing something was much simpler than what I had devised. But because I had this attitude, I felt it was a great achievement to write all that code.

Gradually, partly in college and partly on my own - reading articles, watching interviews and talks - I began to see how it actually works. And of course it's nothing like what I just described. For a long time, my biggest problem was not knowing where to start, what bits I would have to create. It would just seem that I had to follow that one thread of thinking, something I needed, and then take it from there. Years on, after I've been through quite a few projects, the reality of development is starting to approximate the theory that I learnt about how it's supposed to be.

I've done enough of this now to design a system, to see what the components should be, to consider different variants and see what the tradeoffs are. Nowadays I don't even start coding until I have a good mental picture of how it's going to work. And what wonderful things can be achieved with modular coding, now I actually see how separate pieces should be divided. They can be classes (in object oriented world), they can be just modules exporting functions (python, c etc.), they can be higher order functions (functional programming) and so on. But I understand how an API works, how it's meant to work.

Nowadays, I'm much more likely to find the simplest solution in the beginning. And if I don't I try to iterate my code to reach it. One basic theory about how to manage programmers is that they have great pride in their code and everyone wants their code to be in the final product. So if you remove their code because it's not necessary, they get mad. And I'm sure that's true. But, on the way to finding the optimal solution, the more code I can remove the happier I will be. Often the simplest solution is 2-3x less code than what I started out with. It's really just a matter of expressing ideas in a very clear way. When the mind is terribly foggy, that's a huge problem.

It is such a huge shift in thinking. Coding isn't some impulsive, ingenious spur of the moment scheme, it's a well defined, highly organized, methodical way of building solutions. So when I look back at how I used to think about the subject, I'm almost amazed that I had so little natural intuition about the subject, I really had no idea what it's like.

Peter Norvig said "Teach Yourself Programming in Ten Years". It's been about ten years since I started out. And I feel I'm well grounded in the basics now. Like a formula 1 driver knowing that he can start his car without choking the engine 9 times out of 10.

my wordpress theme

August 14th, 2006

So it turns out that adding custom fields in WordPress is really easy. Just use the Custom Fields box beneath the input box and add a new field. This new field will be presented as an option for every new post as well (this I would not be brave enough to assume, but it's so). From there on, bringing back Now Playing is really simple, so I thought why not just make it a plugin while I'm at it.

Using the plugin requires this as part of the post in the template: <?php get_now_playing($post); ?>. Yes, it requires passing on the $post object and no I don't know why. Why do *you* care? :P

So since I made it a plugin anyway, I thought I might as well package it up on the very slight off chance that someone would want it for themselves.

And while we're at it, I packed up the theme I'm using as well, get both of them below. The reason I bundle this is that the theme illustrates how to use the plugin. Not only where to call the function, but also how to style it, you'll find that at the bottom of style.css.

One caveat, the plugin *expects* a custom field called Now Playing, nothing more, nothing less. So if you neglect to include the whitespace or you call it something else, you'll have to hack the plugin to make it work (which is trivial, just edit the call to get_post_meta and you're done).

"How about a demo", you say. Look at the bottom of this post. :)

blog facelift

August 13th, 2006

As you can see, the blog just got a bit of a facelift. "But", you interject, "if it ain't broketh, why fixeth", as it were. I've been meaning to do this for some time, I was never 100% happy with the blog software I was using. Granted, Radek Hulán's extensions to Nucleus in the shape of BLOG:CMS result in a very powerful and customizable package, but it is a bit rough around the edges. It would frustrate me in certain ways, like the rich content editor being buggy and not working well with images. Like the stylesheets being as good as unintelligible - even though I modified them to my liking, I knew it would be hell to try and change at any point. Like the xml feeds not always working well. It feels quite hackish. The admin control panel, for instance, is quite rough looking and frustrating. The templates are very difficult to make out, they're all stored in the database and it took me quite a bit of trial and error to figure out what comes out where. Basically, I felt locked into a system that worked, but I would rather use something that is more friendly to change.

Back in 2003, Nucleus was a good choice, but the landscape has changed somewhat. WordPress has made great strides and over the years I've seen quite a few great looking WordPress blogs. What makes them stand out is the attention to typography that WordPress has, which is a nice quality to have for publishing software. WordPress blogs are always good to read, because the fonts are a certain type and size etc. They also tend to have a very clean look. And since it's now widely used, I thought I would probably find enough plugins to satisfy my modest needs.

To get a general idea of what my blog could look like, I started browsing the hundreds of themes there are out there for WordPress. Surprisingly, I couldn't find one that was actually good enough to use. To be fair, it's not that there aren't any usable themes, just that I couldn't find one that would suit me. Since I don't want my blog pink or black or the post section very narrow with two sidebars, I soon realized I would have to make my own theme. Given how many attractive WordPress sites I've seen in the past, it's astounding that there isn't one theme of the kind I was looking for (predominantly light colors) in about 200 I looked through that would actually be good looking. A lot of them look good in some ways, like good choice of fonts, but there's always some problem, like the menu looks 1995. Or the header image along with the color scheme looks great, but the fonts are Times New Roman italic or something like that. It's strange that someone would be competent enough to produce a professional looking design with balanced colors and images, but completely botch the choice of fonts that go on the page. I'm no designer myself, I can pick fonts that look good and with some effort find a decent color scheme. But my skills are limited, I see websites all the time that I couldn't design, because I don't have the eye for it.

So I would have to roll my own. When hacking existing code, it's essential to start with a good base, ideally the fewer changes the better. The standard WordPress theme is a good place to start. It does look quite good, but it has a few problems. For one thing, the items on the sidebar are not ordered in a particularly inviting way. There is a page full of archives, so just about noone is going to see what's further down. Another problems is the way the posts are presented. The space allocated to them is too narrow. In some of my posts, I have images up to 500px in width, this post area is only 450px wide. It's also so narrow that the page looks funny, as if the post wasn't supposed to take up any more space, even though there is nothing else to look at. And strangely, on the single page, where the sidebar doesn't appear, it's still just as narrow.

kubrick-theme.png

Unlike BLOG:CMS, WordPress does not come with a rich admin control panel for changing the styles. In fact, they seem to encourage editing the actual files. At first, this felt unnatural, as I'm used to vBulletin also installing hundreds of templates that go in the database. But this is actually a better way of doing it, it's easier [quicker] to modify files on disk than rows in a database. It also means I can use nano or vim instead of having to enter the code in a textarea.

WordPress, seemingly, is documented to death. It has so much documentation that I wouldn't even consider starting reading it. Instead I made a copy of the default theme and started changing things. Themes are separated very cleanly and there is no need to modify anything that shouldn't be touched (outside the theme). Basically, all you need is in there. The stylesheet makes for a nice change of pace, it's actually logically divided and documented. But debugging css is always a pain anyway, so I found a couple of Firefox extensions that make it hurt considerably less:

These things are a blessing when trying to make sense of things! So after a while I came up with the theme you're looking at. It aims to preserve all the strengths of the theme I used in BLOG:CMS and I'm quite happy with it. One thing that isn't part of a theme are the smilies. WordPress comes with its set of smilies and I wanted to use xtorg, which I use for everything, because it's basically the best smilies set known to man. ;) This should technically be very simple, such is the hypothesis. Smilies are declared in wp-includes/vars.php, in a simple associative array. Little did I know how much effort it would take to get that array "right". It kept breaking in strange ways (parsing totally out of whack) and finally I realized that for smilies that do *not* begin _and_ end with a colon, I need to add a space at the beginning, so for instance in the case of word :D , WordPress will not recognize :D at the end, it must be prepended with a whitespace. This little quirk isn't documented in the source, and as far as I could see, isn't documented on wordpress.org either. This particular example doesn't seem to square with "code is poetry" - WordPress's slogan - but is there not an exception to every rule? In any case, I've added support for WordPress to xtorg, so anyone can now easily use those smilies in WordPress. For those just posting comments here, use either colon-text-colon or just the msn codes, those will work too.

One thing I thoroughly enjoyed in BLOG:CMS was the random quotes that would appear on the single post pages. I've ripped them off and added them on the index page. Another thing I wanted to preserve was the "Now Playing" feature, but there is no plugin for that in WordPress. Meanwhile, there is a host of plugins for pulling in data from last.fm. Well, I could use that to display the last song I listened to, but I don't quite know where. The sidebar is quite full as it is, so for the time being, it's off.

Finally (and this post isn't quite in chronological order), with WordPress set up and configured, it was time to import my posts from BLOG:CMS. As it turns out, WordPress *still* hasn't developed a script for that, but a few people have tried it. I tried every script I could find, but only one of them worked, and it was Leonid Mamchenkov's nucleus2wordpress.perl. The script ran without a hitch, actually. I realized I would have to hack it a bit, because the images weren't showing up (and this is something WordPress is very stupid with - hardcoding image paths in posts). There was also a problem with the charset - WordPress uses utf-8, BLOG:CMS uses iso-latin-1. I had to research how to encode text in perl and surprisingly, I pulled it off with two lines of code that worked (w00t! w00t!). I say that, because I don't actualy know perl. So now the rare post in Norwegian shows up in all its utf-8 splendor.

So, there it is. Without doubt, the most documented software installation in my life. :D

And here is one last look at the past:

blogcms.png

"Die Hard" riddles for kids

August 10th, 2006

Do you remember the awesome action classic from the 90s, "Die Hard: With A Vengeance"? That's the best action movie I've seen, it really had everything - the cast, the special effects, the scenery and a solid, unpredictable plot. But it had one more thing as well. Do you remember they cast Simon as this guy who tells lots of riddles to confuse people? This is the portrayal of a criminal mastermind, flaunting his intellect to embarass the street smart, but otherwise "regular Joe", cop. Those riddles weren't university level problems, they seemed better than they were because of all the explosions and gunfire that accompanied those scenes (or indeed the imminent explosions and gunfire). But they were really just the kind of riddles you tell to kids.

null

So I was thinking, since the whole category of "educational" games in free software is pretty lame, ie. very few projects that actually look serious at all, wouldn't it be a good idea to make one game that features riddles from "Die Hard: With A Vengeance"? And to make it more interesting, make it illustrated. For instance, you could divide the window into two parts, one side featuring stills from the movie (McClane with a gun on the boat etc), the other side displaying this riddle with a multiple choice option:

As I was going to St. Ives? / I met a man with seven wives. / Every wife had seven sacks, / Every sack had seven cats, / Every cat had seven kittens. / Kittens, cats, sacks, wives, / How many were going to St. Ives?

Kids would love it! :cool:

a confession

August 5th, 2006

I have a confession to make. I did something unspeakable today. No, I didn't eat meat, I'm afraid it's much worse than that. I killed a plant. Actually I didn't kill it, that would at least be the humane plantane thing to do under the Geneva Convention. Instead, I... tortured it. Not because I needed information, just to... make it look better. You see, there is this rosebush in the garden that has branched out far beyond its designated area, it's been attempting to annex part of the terrace. So in a mad rage, I grabbed the first weapon I could find, a pair of garden clippers, and went to work on it in a way that would make a serial killer proud. Once I was done, the magnitude of it all overwhelmed me. I broke down on the terrace and wept like a child at all the plant cells that had lost their lives.

I'm expecting SWAT at any moment and I'm sure PETA will hold a nude protest outside my house as well. Meanwhile I'll be in my room playing Counter Strike while I think about what I did.