Idiomatic programming
June 2nd, 2008So I spent a part of my day researching Perl as I’m setting up Request Tracker at work and it’s made in Perl. While reading loads of blog posts on why Perl is good and why Perl is bad (I get sidetracked sometimes), I starting wondering if I was using the languages I know properly. That is say, idiomatically.
Many of those blogs posts had comments on why people agreed that Perl sucked or that it rocked. They often compared it to other languages, saying so and so was better most of the time. I actually chuckled at a couple of arguments that went back and forth since most of them were about style issues.
Nobody seems to like languages for what they truly are, and that is a programming language. Just like english and french, they are a matter of personal tastes and what you’ve been raised with, in a manner of speaking. I was raised by a francophone1 family and I learned english later on in my life. I am far from being a perfect speaker / writer in this language, but I still love it and I think I have a really good grasp of it.
To get back on track, there are lots of people who seem to only swear by one language (Perl, Java, Ruby, Python, C++, etc.) and hate all others. They want to do everything with their language of choice. I don’t understand this. I always feel like people writing those “opinions” are 15 year-old trolls.
Why do they think their view on a particular language is right? There is no such thing as a universal programming language. Sure, you can do everything with most, but it doesn’t mean that it’s the best, most efficient way. And isn’t that what programming is all about? Creating good, efficient applications that solve problem in the simplest way possible?
That is where idiomatic programming comes in. Programming languages were invented with an idea in mind. Python for example was created to be simple to read, learn and code. It is massively extendable from C to .NET. Java was made to be entirely object-oriented. I’m not going to go into more details, you can probably find what idea was used as foundation for each and every language that exists.
Using a language to make every application you need is stupid and an insult to the language you are using wrong and the one you aren’t using right instead. It isn’t only a concept based on ideas for programming it’s also how you use the language. Doing things in a pythony way instead of a C# way when doing C# is bad.
Writing Python? Learn the most you can about basic type such as lists and dictionaries, they’ll save your life and loads of bloated code.
Doing
1 2 3 4 5 6 7 | a_list = [1, 2, 3, 4, 5, 6, 7, 4, 3, 2] temp_list = [] for element in a_list: if element == 2: temp_list.append(element) for element in temp_list: do_something(element) |
instead of
1 2 3 | a_list = [1, 2, 3, 4, 5, 6, 7, 4, 3, 2] for element in [x for x in a_list if x == 2]: do_something(element) |
is not a Pythony way and should be avoided.
I’m not here to tell anybody how to use their favourite languages, I’m far from a master of any language at all. But one thing I know is that I’ll never claim to be one, and I will surely never be able to say that a language is better than another just because I don’t know it enough or dislike its syntax.
The goal is to make sure that someone that knows a bit about the language you are writing will be able to skim through it and understand what it’s meant to do.
Example of things that shouldn’t be because they kill idioms on sight (will be kept updated when I remember things or discover them):
- Microsoft’s .NET WebForms. Because it goes against everything the HTTP protocol declares.
- PHP general messed-up-ness. Nothing is constant, you have to know most of it by heart because the STL it has is nothing short of messed up.
- Using tables for layout in HTML or using deprecated HTML 4.0 style tags and attributes instead of CSS.
Notes
Francophone means a person or group that speaks french, for you people who assume us Quebecers are “french”.
