emacs-lisp

Static Languages: Rationalizations and Myths

February 5th, 2008  |  Published in dynamic languages, emacs-lisp, erlang, perl, python, Ruby  |  Bookmark on Pinboard.in

I see my old pal Bill is spreading dynamic language FUD again. I’m the “dynamic language evangelist colleague” to whom he sent the email he mentions.

First, I find it funny that he calls me a “dynamic language evangelist.” I’m really more of a multi-language evangelist, or as Ola Bini recently put it, a polyglot programmer (in fact much of Ola’s recent thread on this topic matches up well with what I said on the closing JAOO 2006 panel). This year marks my 20th year of using C++, for example, and I still use it every day, so that probably automatically disqualifies me from membership in any dynamic language clubs. But this year also marks my 20th year of using both Perl and emacs-lisp, so maybe there’s hope for me yet.

Bill gets irrationally upset when I tell him I write in five different languages every day. They are, in no particular order, Erlang, C++, Python, Perl, and emacs-lisp. I really like Ruby but unfortunately don’t get much chance to use it at the moment. I even have to delve into SQL every now and again. Apparently, this notion drives Bill crazy not only because I don’t use Java (though I’ve written my share of it in the past), but also because it means I can’t use an IDE, since no IDE can handle all of the above. Apparently, if you can’t use an IDE to automatically rename your methods for you every 10 minutes or so, you’re not a real programmer.

Bill’s posting, like his original email to me that sparked it, is yet another “we’ve got to put these dynamic language folks in their place” postings (read to the bottom of that link for the context). He basically says so himself. His posting is full of the usual misunderstandings, rumors, poor guesses, and misinformation.

Issue 1: Dynamic Language XXXX didn’t do well against Java in XXXX benchmark

According to Bill, glaring differences between languages in how they perform on benchmarks must be recognized. Yes, that’s true — if all languages performed equally for all tasks, then we wouldn’t need multiple languages. Unfortunately, monolingual developers like Bill don’t view it that way; instead, they view benchmarks as some sort of strange competition where you can try to build up your self-esteem by gloating over the benchmarks for which your one language looks good.

I asked Bill why anyone would choose a language not known for its number crunching capabilities and actually try to use it for real code requiring number-crunching, like calculations involving the Mandelbrot set. He didn’t answer.

In reality, we need multiple languages because there are so many trade-offs to make. One way to address the need to make trade-offs is to choose a general-purpose language like Java and hope that it’s good enough for whatever you have to work on. Another way, and the way I prefer, is to be adept at multiple languages so that you can use the right tool for the job and handle unforeseen requirements. When a monolingual developer runs into a task for which their one language is ill-suited, they typically either bend the problem to fit their language, or they just avoid solving it altogether. Hammer, meet nail. Either way, they’re out of luck, or actually, their customer is out of luck. We polyglots don’t have that problem.

Issue 2: Ruby VM cannot support kernel threads

This issue is already addressed in comments in Bill’s blog, where several people mention all the current work on Ruby internals. But I would add that someone who’s writing authoritatively about Ruby should at least do his homework and already know that this “burning” issue is actually not an issue. Bill mentioned both in his email to me and on his blog that even his beloved Java still needs work, citing the need for “typesafe closures to complement annotations,” “a standard, non-code generating way of adding behavior to annotations,” and “a structural syntax to make initialization easier.” I assume therefore that the fact that Ruby also needs work will not be held against it.

Dynamic languages like Ruby, Python, etc… are not typesafe/statically typed

This is where Bill gets into the IDE religion. When I told him I use five different languages every day and said that I don’t need an IDE, he got mad and called me a name. It wasn’t even a particularly imaginative name, so perhaps he could get his IDE to automatically rename that name for him to turn it into a better insult. I asked if he could recommend a good IDE that could adequately handle all those languages for me, but he didn’t answer.

I use emacs. I’ve used it since 1985. I used Eclipse over the past few years for my Java work, and I really had strong hopes for it for other languages, but it’s still too Java-centric, and my current work doesn’t involve Java anyway. I am highly productive with emacs, and I see no reason whatsoever to change.

Bill asks why we can’t have a dynamically statically typed language. We can; see for example Haskell, and go look at all of Erik Meijer’s work.

Lack of type safety doesn’t scale well to large teams

Prove it. This is pure conjecture, nothing more, nothing less. Scaling development has everything to do with the team members and how they work and work together. Saying that “my buddy Jason was on a team that had to put type names in their Python function names” is certainly not even close to being solid evidence of your claim, Bill. For that particular example, BTW, why not describe the typing expectations for a function in its docstring, thus making it easily accessible to actual human programmers from their interactive Python prompts? That’s way better than applying some weird brittle Hungarian notation convention.

Lack of static typing does not allow for reliable refactoring in modern IDEs

The contrived Ruby example that Bill uses to “prove” this is, well, contrived. Why would anyone write code like that or suddenly get the urge to rename init to init2? I’m no Ruby expert, but I’d probably rename the method and then stick a method_missing in there to catch any call instances I might have missed with my editor. I’m sure a stronger Ruby developer can suggest a better way. Either way, it’s really not a big deal, Bill. I’m sure I could get it done well within the timespan you waste on fantasy football every day. ;-)

In 20 years of writing some pretty large C++ systems, for example, I don’t really recall ever having to go and rename a whole bunch of functions across a codebase. I recently had to move some classes from one C++ namespace to another, but a few lines of elisp automatically fixed 3000+ files for me, all without breaking the build (and yes I’m aware of namespace aliasing, thanks).

I asked him if these magical modern IDEs that raise productivity and eliminate common errors also eliminate defects caused by missing or misunderstood requirements, missed use cases, or just plain ol’ bad programming. Again, no answer. The reason I asked this is that those are the bad bugs; syntactical errors are really the least of your worries. Even if the IDE spits out common idiom/pattern skeletons for you, it’s still quite possible to screw up the code logic, and neither the IDE nor the compiler is going to prevent that.

I asked Bill if he’s ever considered that Java IDEs are the way they are due to issues with the Java language itself that must be compensated for. Consider its verbosity, for example, which is why IDEs spit out those skeletons. I can’t remember if he called me a name for asking that, or if he just didn’t answer. Either way, I got no response.

Bill asks if we can’t just learn how to type, but isn’t it the other way around? Shouldn’t he be asking why he needs the IDE to type for him? Odd.

Considering how old Java is, it’s obvious that it’s taken quite a bit of time to get these IDEs to where they are today. I asked Bill if everyone should have to wait a long time, on the order of 10-15 years, before an IDE truly becomes valuable for a given language. No answer. Personally, I’d rather stick to my emacs and UNIX tools, with their infinite applicability and flexibility that can be used for development in pretty much any language, than wait around for someone else to give me a far less functional IDE that barely addresses only one language. But then again, if one language is all you got, then I guess you have no choice.

The rest of the myths Bill cites just circle back to the same weak IDE arguments again, so I’ll skip those, save for this one:

Software Engineers should use the best language for the job

Yes, this is true, unless you’re an elitist.

Like Bill, I’ve spent much of my career writing middleware. However, unlike Bill, I’m not happy to sit around thinking that C++ or Java is the best we can do. I wish I would have known about Erlang 10 years ago, for example, so I could have saved myself countless development hours and headaches. In hindsight, I wasted way too much time trying to build in C++ and Java what Erlang basically gives you for free. I have only my own ignorance to blame, but now I know better. Thankfully, I now work in a place that lets me apply the right language for the right job, and our productivity is significantly higher than any place else I’ve ever worked.

I told Bill that if he wanted to do benchmarking, he should go benchmark the Yaws web server, written in Erlang, against Apache or some Java web server. Needless to say, that would be far more realistic to a lot of developers than Mandelbrot calculations. His response was that he didn’t need to, because Apache would win. That kind of poor assumption is much like the mindset that led me to poorly reinvent half of Erlang in my years of middleware work. These days, though, if I saw this graph I’d be highly intrigued — and in fact, I was when I first saw it — so much so that I’d want to run my own tests to see if those claims were accurate. My own tests were smaller scale than what that graph shows, but the results were basically the same. I’ve rerun my own tests and some others as well, and I get the same results every time. So Bill, rather than guess and make assumptions, why not try it for yourself? Or are you afraid to find out that dynamic languages actually are quite useful?

The things I write about in this blog are based on significant hands-on experience. Unlike many developers, I’ve heavily used approaches and techniques from both sides of the fence, and based on all my experiences, I’d rather use dynamic languages than static ones. I have more success with them, and the systems I use them for turn out better. That’s not wishful thinking or fabrication, that’s simply the cold, hard, factual truth. I wonder, then, why Bill and others like him insist of trying to “cure” me and others like me of our successes?

If you don’t like dynamic languages, then don’t use them! Leave us alone and go back to your monolingual world, and maybe rename your methods again. You can even have my IDE, since I’m not using it, which should double your productivity yet again, right?

Elisp

January 24th, 2008  |  Published in code, emacs, emacs-lisp  |  Bookmark on Pinboard.in

Today I saw two blog postings about emacs-lisp, which is unusual given the feeds I pay attention to. I’ve been using emacs since 1985, and so over the years I’ve written more than my share of elisp. I didn’t know anything about Lisp when I started, so I learned by doing, first by just reading other code, then experimenting and adopting more and more patterns and approaches as I came to understand them. Over the years I’ve studied Lisp here and there on my own and so have improved my elisp, I think, but I’d never call myself an expert. I’ve never worked closely with anyone who enjoyed writing elisp as much as I do, so with nobody to bounce ideas off of, I’m sure there’s still much I can learn.

The first posting I saw today is about the proper handling of association lists, or alists. An alist is a list of key/value pairs, and one critical way they’re used in emacs is for the auto-mode-alist, which indicates what editing mode a given file should be set into when it’s visited. The mode is chosen by attempting to matching each key in the alist against the name of the file being visited; when one matches, it treats the associated value as a function and executes it in the file’s buffer. Such functions usually set the editing mode of the buffer. The keys are typically regular expressions that match file suffixes.

The author of that post had been using the aput function to replace elements of the auto-mode-alist with the editing modes he preferred, but recent changes to emacs resulted in aput being moved to the assoc package, so it was no longer directly available for use in the author’s ~/.emacs startup file. The author’s search for a substitute function to use instead resulted in him getting some bad advice about how alists are handled, and his posting explains how things really work.

One obvious way to fix the problem is to simply (require 'assoc), which would load the assoc package and make aput available. The author didn’t to want to do that, probably to avoid dragging in everything else the package defines. So, he instead resorted to using the push function to prepend elements to the auto-mode-alist to indicate his preferred editing modes. As he explains, file loading always searches the alist from head to tail, and so will always find his settings first, even if the same key patterns occur later in the list.

Another way to do it — a more fun way, perhaps — is to write your own version of aput. The code is interesting because it requires you to pass the alist essentially by reference to the function so it can modify it. The second elisp posting I saw today was Steve Yegge’s “Emergency Elisp” tutorial, and he mentions pass-by-reference but doesn’t really say how to do it, so let’s look at a way to do that. Here’s my version of aput:

(defun my-aput (alist key value)
  (let ((al (symbol-value alist))
        cell)
    (cond ((null al) (set alist (list (cons key value))))
          ((setq cell (assoc key al)) (setcdr cell value))
          (t (set alist (cons (cons key value) al))))))

This doesn’t do exactly what the real aput does, since it doesn’t handle the case where a key with a nil value is passed, but that’s not needed for the auto-mode-alist case. The function expects the alist, the key, and the value. As the three cases of the cond statement show:

  1. if the alist is empty, set it to a list consisting only of the new key/value pair;
  2. if we find the key, replace its associated value with the value passed in;
  3. otherwise prepend the new key/value pair to the front of the alist.

The key to making this work, though, is that we don’t really pass the alist. Instead, we pass the symbol for the alist. The symbol is kind of like a reference, in that it lets us get at the value of the alist, which we do in the first line of the function via the symbol-value function. Our first condition tests the alist to see if it’s nil; if so, we set a new alist value for the symbol. Our third condition (which always runs if the first two don’t, since it tests the value of t, which is always true) also sets a new value for the symbol by prepending a new key/value pair onto the current alist value.

To call it, we do this:

(my-aput 'auto-mode-alist "\\.erl\\'" 'erlang-mode)

Note how we quote the auto-mode-alist, so rather than evaluating it and passing its value, we pass just its name, or symbol, effectively giving us pass-by-reference.

Elisp is the primary reason I keep using emacs. It’s amazingly powerful. You can make it do all kinds of editing chores for you. A lot of people today tend to rely on their IDEs, and I even tried to move to Eclipse a few years ago, but I just don’t think there’s any IDE that can match the power and extensibility that elisp gives you. I’m sure I’ll get a few disagreeable comments for that remark, but they’ll almost certainly be from people who don’t know elisp.

Since I’m not an elisp expert, though, my explanation might be off in some way, and it’s probably possible to improve my code. All constructive criticism is welcomed!