September 17, 2004

It's not about one-liners

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.

Posted by cedric at September 17, 2004 01:36 PM

Comments

Cedric, you're right. I, too, think lambda is a bit of a line noise. And [::-1] is definitely obscure. But I somehow find it readable and clear enough, I mean, it's definitely not as obscure as some Perl idioms. Anyway, did you see my latest comment? Python has a built-in module called "glob" which allows you to retrieve a list of files using special parameters. The cleanest Python version would be something like (as I already posted before):

files = glob.glob(".gz")
files.sort()
files.reverse()

Or, in Python 2.4:

files = reversed(sorted(glob.glob(".gz")))

Posted by: Jonas Galvez at September 17, 2004 03:28 PM

Ops, that should glob.glob("*.gz").

By the way, I wrote a comment about Ruby but I keep getting a "Your comment could not be submitted due to questionable content" message. I've placed the content on a text file and uploaded it to my server:

http://jonasgalvez.com/unsorted/about-ruby-20040917.txt

Posted by: Jonas Galvez at September 17, 2004 03:34 PM

I should add that I do realize this discussion is not about that specific operation of filtering files etc, but about the way properties/methods are accessed and how nicely it flows in Ruby. The portion of my comment that I placed in the .txt file (since it kept being rejected by the spam filtering system) summarizes my opinion on the subject :)

Posted by: Jonas Galvez at September 17, 2004 03:44 PM

Yes, I agree readability is best, but I hear "life's better without braces". (remains to be seen, but I plan to see!)

Posted by: Michael Levin at September 18, 2004 04:26 PM

chqyivsw [URL=http://qjsjsgev.com]hqhyndtk[/URL] mzmoudqi http://qcgfpkyx.com brnsgmqu hsqsrsmz

Posted by: qevdxmmi at May 28, 2007 08:42 PM
Post a comment






Remember personal info?