Today I got caught up reading a back and forth in twitter about a proposed assertion syntax for Ruby testing frameworks. It was interesting, but yet again it was about how to make tests 'read well.' It's hard to disagree with that, right? Well, I don't in principle, I just think about the amount of time we spend trying to warp programming syntax into English and I wonder whether it is really worth it. If it is, maybe we need more malleable languages.. more malleable than Ruby. Or, maybe we don't. Maybe it's just too much fun.. maybe there's too much 'game' in molding an existing language into what we want it to be. But, I'm getting off topic.
The thing I wanted to blog about is the clash between this natural language style of programming and the other sorts of programming we do. In the agile community, seeded by Kent Beck and Ward Cunningham, there is this meme that the code should tell a story. Luckily, our languages are nimble enough to help us with this. We can use evocative names and string together code lines so that they read like a book. Yes, it's work, but it is very doable. OO was made for that sort of thing.
Let's look at an alternative (avert your eyes if you must):
`git log #{@filename}| grep ^commit`.split(/\n/).map(&:split).map {|fields| fields[SHA1_COLUMN] }
This is a line of code that I wrote to pull the sha1s out of the output of a git log command.
Ugly? Yes, but I can make it better. Let's pull out the bash by saving the shell call's result into a variable called 'text':
text.split(/\n/).map(&:split).map {|fields| fields[SHA1_COLUMN] }
Better? No, I can still hear you saying "yuck!" I'm not going to argue with you, but I am going to say that that I think that this code is an example of an equally valid mode of expression. It's just a visual mode rather than a narrative one.
Let's bring it home. Try to explain the code out loud: "take the text and split it into lines, then split those lines into fields and map each line to the field containing the sha1."
Maybe some people do sub-vocalize that sort of thing to themselves when they see code like that, I just know that I don't. When I see code like that, I construct a picture of what it is doing inside my head. Words don't play into it much.
I don't think I'm alone in this. We have different ways of making code understandable and making it 'read like English' isn't the only way.
I remember years ago reading about something called Neuro-Linguistic Programming, and although I felt that much of it was popularized off the deep end, the idea that they had that we have different cognitive modalities is something I'll always remember. According their theory, some people are far more visual. Others prefer to get information in an auditory way, and often they subvocalize when they are thinking.
No doubt this is a ruthless simplification, but what I notice about programming is that there aren't really any fixed boundaries. We go back and forth between more structural (a.k.a functional, visual) ways of thinking about code and more narrative (auditory) ways.
When I look at really good functional code (and believe me the piece I have up there isn't a good example) there is a balance between good naming and structure that reveals computation.
It just isn't narrative.
It's a different mode and I think we have to recognize it.