Archive for January, 2009

Zwart

Just stumbled upon this post: Are Dynamic Languages the New Black? In it, the author questions the benefits of dynamic programming languages, and thinks that fashion is to blame for their recent rise in popularity.

In my experience, dynamic languages take a while to “grok”, especially if you come from mainstream statically typed languages like Java, Pascal and C++. (Which is what I will be talking about here, as opposed to static typing in languages like Haskell and OCaml.)

At first, the code you write in a dynamic language will probably look much like how you would write it in your previous language. For example, you can still write Python code that looks like Java, using getters and setters and AbstractFooHandlerFactoryFactory classes, and so on. If you do that, you are not leveraging Python’s flexibility, and you might wonder what the benefit is, other than the lack of type declarations. (Which probably feels very “unsafe”, and you might start writing unit tests or assertions to check the types, which then seems like extra work.)

In order to enjoy the full capacities of a dynamic language, you have to embrace its dynamic features and not work against it. While that may sound spacy, what it really comes down to is: if a language gives you certain features, consider using them rather than working around them! Of course it’s always possible to abuse such features and make an unmaintainable mess of your code, much like in *any* language; but that’s why there are guidelines and good coding practices, which, by the way, will be different in some areas from what is considered “good” in static typing land.

This is an example of what I consider the wrong approach:

Type safety, [proponents of dynamic languages] argue, can be achieved through unit testing. Write a test for a.b(c), and if a or c are of the wrong type in that scenario, the test will fail when the interpreter gets around to checking what’s in those slots.

Aside from what “type safety” means here, the issue is that when using a dynamic language, we’re often *not interested* in explicitly checking types. What we are interested in, and what we write tests for, is whether a.b(c) runs without errors. It should return the expected result and not raise exceptions or fail otherwise. The exact types of a and c often *don’t matter* in this context.

This is a feature rather than a bug, and can often be put to good use. Let’s say that in a.b(c), a is usually an instance of class A, but I want to replace it with a mock object M, then that can be done without much hassle. No need to worry if M implements the same interface as A, or is a subclass of it, etc… all that’s necessary is that M has a method b that can handle c, and returns a reasonable result.

This is just one example, but dynamic polymorphism can be used in many ways. It takes time to figure out everything that is possible, to learn new coding and design habits (and unlearn old ones), and to learn to trust your tests even if they don’t explicitly check for types. I think you would need tests anyway — static typing doesn’t test if your code does what it should do, just that it takes and returns the “right” types.

Anyway, about the fashion issue:

I put it to you that the current fashion for dynamic languages is excatly that – a fashion. And many people are using them because they’ve seen other people using them, and they want to be in with the in crowd.

Quite possibly. Fashion and hype tend to run strong in programming communities. However, you could argue that the proliferation of languages like Java, C# and C++ happened due to fashion and hype as well.

:: Comments (3)

Cool programmers

This sounds familiar:

“What I found shocking was that there are now “cool programmers”, who are cool not because of awesome code, but by virtue of showmanship and relentless self-promotion. It’s like the cool kids in High School. Blogging and twittering has become a way to strike a pose and market yourself.”

Indeed, there are several reasons why you might know a certain programmer by name.  Blogging has a lot to do with that, to the point where you know certainly people *exclusively* because they blog (and/or write books), not because you use their software. How many people would know who Paul Graham is if it wasn’t for his essays? Ditto for e.g. Joel Spolsky, Jeff Atwood, Steve Yegge, Eric Raymond, etc. That is not to say that these people aren’t competent programmers, or that they haven’t achieved anything; but their writings are *much* more popular than their other work.

On the other hand, there are programmers who don’t blog much (or at all), but whose names are well-known anyway. Language designers come to mind, for example (do the names Guido, Larry or Matz ring a bell?), but also developers of popular projects. They don’t even have to be the main developer; for instance, Martin von Löwis is well-known in the Python community, not because he blogs about controversial topics, but because of his relentless contributions to the development of Python. And there are many others.

There are also those in the middle; Richard Stallman perhaps, who is well known because of the Free Software movement, the GPL, and his unyielding stance on proprietary-vs-free software, among other things; but also because he is a prolific coder, and his GNU tools are all over the place. (Although I’m not sure how much of it is of his hand, nowadays.)

:: Comments (3)

Python 3.0, first impressions

For one of my new personal projects, I decided to use Python 3.0. I want to become more familiar with Py3K, and it’s not like this project will be useful to (m)any people anyway, so this is a good time to experiment.

Here are some of my findings. Most of these I had read about before, but I didn’t remember them until I tried writing code 2.x-style.

  • I intuitively write “print blah-de-blah“, without parentheses, which doesn’t fly anymore as print is a function in Python 3.0. This has been biting me surprisingly often. I figure it will keep running into this every now and then, as long as I still use both 2.x and 3.0.
  • Tuple parameter unpacking has been removed (PEP 3113), and I just happened to run into a case where this would have been useful. :-) No big deal though.
  • raw_input has been renamed to input.
  • I was somewhat surprised to find that import string still works… unfortunately, string.join is gone, so I have no choice to but to use str.join. *grumbles*
  • map and friends now return an iterator rather than a list. This one bit me when I created a list (or so I thought) at the toplevel, then tried to search it multiple times from inside a function. Although I read about it, I didn’t see this one coming, as I’ve been tinkering with functional languages lately, and using map tends to produce a list there. Fortunately it’s easily fixed with a listcomp.

So far the only third-party library I have used is Nose. There is a Py3K branch for it, get it at:

svn co http://python-nose.googlecode.com/svn/branches/py3k nose-3k

As far as I can tell, it works fine; just use python3.0 setup.py install.

More later, as I hack some more on my stupid project. I miss IPython; maybe there’s a 3.0 branch for it too? Will have to find out. [Update: Apparently not.]

:: Comments (3)

Stupid ideas part 469: line editor

Interesting quote in this article about Unix history:

By the way, ‘em’ stands for ‘editor for mortals’ – I christened it that after Ken Thompson visited our lab at QMC while I was developing it and said something like: “yeah, I’ve seen editors like that, but I don’t feel a need for them, I don’t want to see the state of the file when I’m editing”.

So I was wondering… would a line editor still be feasible today? The obvious answer is “no”, of course. Nobody uses them anymore, as far as I can tell (at least not interactively)… as a matter of fact, quite a few people seem to think that even an editor isn’t good enough to edit code anymore, they want IDEs (whatever that means — it seems to depend on the speaker).

However, I have a tendency to look for non-obvious answers. :-) Question is, why did Ken Thompson say this? Notice that this quote is from around 1975, when line editors were pretty much the default (or so I understand; I was only 2 years old at the time :-). I assume that Ken has moved on since then. Nevertheless, one can wonder, did he say that simply because he was used to line editors and didn’t see the need for anything “fancier”; or is it because line editors have certain qualities not found in other editors?

I have never really used line editors myself. In 1991 my first PC came with edlin, which seemed prehistorical even then. Heck, the C64 had a more powerful environment to code in! But I have often wondered what it would be like to use them. Because of all their restrictions, they force you to focus on only the text (or code) you are interested in. This would make for a very different editing experience than jumping through the whole documents using cursor and PgUp/PgDn/Home/End and friends.

Many questions arise:

  • Would modern-day users be able to get used to it? (I have power users and hackers in mind here, people who regularly use a command line of some sort.)
  • How would you get around the document (efficiently)?
  • How would you copy & paste?
  • How would you delete or replace stuff and be confident that it didn’t overwrite the wrong thing?
  • Would it be possible to add modern features like code completion, svn integration, etc, and what would it look like?
  • What would be the macro/automation capabilities of such an editor? (Probably quite powerful)

Maybe this would make for an interesting (although not necessarily practical) side project. I’m especially interested in finding out what it “feels” like to use such an editor, if it’s still usable nowadays, if it has benefits of any kind over regular editors, and if/how modern features can be added to it.

:: Comments (5)

Custom filters in Google App Engine

I wanted to add custom filters to my Google App Engine application… There are instructions on how to do this in several places, but some of them contradict each other, and it took me a little while to get it working. Anyway, I thought I’d share the setup that did the trick for me.

Let’s say your application is in a directory app. Create a directory app/common. Drop an empty __init__.py in it, and the file containing your filters; say, my_filters.py.

Here’s some sample code for app/common/my_filters.py:

from google.appengine.ext import webapp

register = webapp.template.create_template_register()

@register.filter
def foobar(value):
    return "(%s)" % str(value)

This creates a simple (and rather useless :-) filter named foobar, that takes an argument and returns its string values, surrounded by parentheses. register.filter can be used as a decorator. Any functions in the file that are not registered, will not be recognized as filters.

In the application’s main file, add the following (at the toplevel):

from google.appengine.ext.webapp import template

template.register_template_library('common.my_filters')

Now, in your templates, you should be able to do things like

{{ "hello"|foobar }}

That’s all. I saw some explanations online that talked about using the templatetags directory and such, but that doesn’t seem to be necessary with App Engine.

:: Comments (3)

RSS readers

I’ve been using Bloglines as my main RSS aggregator for several years now… and I’m wondering if there’s anything new/better out there. So far I’ve tried a few client-based (for OS X) and web-based solutions, like FastLadder, Vienna, NetNewsWire, Google Reader, AmphetaDesk, etc… but I don’t really see anything new that has significant benefits over Bloglines.

Am I not looking in the right places, or is this kind of software simply running out of innovation? Maybe there are only so many things you can do with RSS feeds. But if you know of an RSS reader not listed here that you particularly like, please list it in the comments.

:: Comments (5)

Haskell search engines

I didn’t know this, but there are two specialized search engines for Haskell: Hoogle and Hayoo!. Both of them allow you to search for Haskell functions by name or type signature.

Unlike its general-purpose counterpart, Hayoo seems to return the best results. For example, you can look for all packages that have a function foldl, or for the functions in the Data.Tree.AVL package.

Now if we only had such a beast for Python… :-)

:: Comments (2)

Miscellaneous links

Not a very original subject, but these links might be vaguely interesting:

  • Schola: A social network for Latin speakers.

:: Comments off

Fate

For the last few days, I’ve enjoyed playing Fate. (I’m using the OS X version, which works rather well on my MacBook.)

It’s a dungeon crawler, not unlike Nethack with pretty graphics, or Diablo. Although Nethack is probably less superficial, Fate has a lot of things going for it, that kept me going back over and over again.

Basically you look around the village for quests, then enter the dungeon to fulfill those quests (e.g. fetch an item, destroy a certain monster). While you’re at it, you pick up gold, weapons, treasure, and rack up experience points. Then you return to the village, sell the items you don’t want, and possibly upgrade your gear (get better weapons and armor, etc). Repeat.

You don’t have to go it alone; at the beginning of the game you get a pet (either a dog or a cat), which helps you fight monsters. It is also useful in other ways; for example, it can carry items and can even be sent back to the village to have these items sold. Pets cannot die, although they will temporarily flee when low on HP.

The game is quite over-the-top in some ways, like the names it uses for weapons and monsters, or cumulative abilities. For instance, at one point my weapon of choice was called “Holy Strong Battle Fork of Strength”, and there are names that are even more out there. Also, as you can see in the screenshot below, that weapon had like ten additional abilities (added by me, using gems and magic anvils).

The ability to enter cheat codes is built into the game. When I tried this, it made the game simultaneously more and less interesting. Less so, because the challenge was gone; after all, I could now level up, heal, get any item I want, etc. But also more so, because it made my character more customizable; I changed my pet into a wyvern, got the weapons I wanted and added gems to them, improved my defense, etc. (I suppose using the cheats changes the game from quest-oriented to some kind of RPG-flavored Sims variant, which is of course an entirely different playing experience… but not necessarily an unpleasant one.)

Anyway, I’m kind of done with it now, but I’ll probably return to it at some point for some more dungeon hacking. ^_^  Worth checking out if you have a Mac or PC.

Further reading:

:: Comments (1)

Minor site changes

To the right, there should now be a section in the sidebar that displays what I am currently doing. (Kind of like Zannah. :-)

Oh, and I removed the tag cloud, because it was ugly. >_<

(For what it’s worth, I have an Amazon wish list now too.)

I was actually going to design a new site for 2009, and write the blogging software, but now I think it’s better to stick with this site, at least for my blog. I have some other plans for web apps though. Nothing useful mind you :-) but I want to tinker some more with Google App Engine.

:: Comments off

« Previous entries Next Page » Next Page »