I’ve been working with Erlang as my primary programming language for about three years now. (Don’t quote me on that, it might only be two years) In any case I learn new techniques almost every day and with everything that I learn the language itself seems to get simpler and the number of lines of code I need to accomplish something goes down.
I was really noticing this on my implementation of the Flash Media server, which uses an odd proprietary protocol that was create by Macromedia (now adobe). I was implementing a section of code that was parsing the header for the packet. My first rendition was nearly 100 lines of code. When I was showing this to a colleague of mine he expressed that the way I had coded it was exactly the way he would have as well, which I consider standard practices for most programmers.
This same section of code is now down to 7 lines of code, it is easier to read and I suspect it is much faster if for no other reason that is has fewer conditional statements in my code. The simplicity of the newer version of this function came from Erlang’s pattern matching in general and the amazing way that Erlang performs binary pattern matching. I was able to write patterns instead of conditional statements and set variables within the patterns. I understand that python has some features like this as well, but I have never used python or any other language that create code that is so succinct.
After realizing what had happened with this one function I started looking at some of my other code from the past few months to try and find similar patterns that I might be able to reduce. I found plenty of the in the SMTP and IMAP clients from ErlMail and I plan on releasing the new version of ErlMail-0.0.5 Friday or Saturday of this week. (I want to add some more documentation into the code so I remember what was going on and other people can use it as well, other then that it is done)
After I write the first version of the IMAP server I am working on next I plan on revisiting the DNS server code I had been working on. Since the DNS protocol is a binary protocol the pattern matching that I learned while working on the Flash Media Server will almost directly translate into the DNS server. I looked at the code in one of the modules before writing this post and I saw three different functions that I could reduce with just 1 or 2 minutes of scanning the code. I’m planning on paying more attention to how many lines of code I started with and how many I am left with after re-factoring and I’ll publish that some time in the future. I hope to be able to get to that at some point in November or December, unless I get stuck on the IMAP server and need a break from it ![]()