I have received quite a few comments on the previous entry so I thought I would clarify something. Jonas Galvez says:
The Python version is pretty much a one-liner too:files = filter(lambda s: re.match("gz$", s), sorted(os.listdir(dir))[::-1])
I don’t care about the size of the code (I find that ten lines of Java are usually more readable that one line of Perl).
I do care about readability, which usually boils down to:
- Ease of reading. This is usually a factor of how many non-alphabetical characters are used for a statement, but it also depends on keywords and on the names of the methods, which must carry the intent of the code very clearly. The code above doesn’t score very well on this scale.
- Consistency of the API. In the Python code above, we have a mix of lambda, method invocation, string parameters, cryptic parameters (::-1) and arbitrary ordering of such parameters. I am sorry, I don’t find this readable but worse, I wonder if I would remember this magic invocation in six months.
- Flow of the statements. What I like about the Ruby example is that it’s basically a piped sequence of method invocations: pipe the output of the previous invocation into the input of the next one. All methods, one object, no fuss (with the exception of the closure). Again, the Python code above seems to require object method invocation, imperative method invocations and parameters that alter the meaning of these methods.
Ruby scores pretty high on each of these points: the line can almost be read as natural language and I find it “very” object-oriented.