Formula versus text
For all flame and discussion on advantages and disadvantages of programming languages there is one thing which is usually omitted.
It’s the difference in approaches to programming languages. When you like some language, why do you like it? Or, what do you want from a language?
There are two big schools. The first school prefers mathematical approach. Not in term of math per se, but with certain inclination toward formulae. Each stanza is a formula. The more meaning and the less characters formula take, the better it is. Look at this beauty:
(define (mpower M p)
(cond [(= p 1) M]
[(even? p) (mpower (matrix* M M) (/ p 2))]
[else (matrix* M (mpower M (sub1 p)))]))
(for ([i (in-range 1 11)])
(printf "a^~a = ~s\n" i (matrix-expt a i)))(source)
The second school insists on readability for unsophisticated reader. If you have too much meaning in the code, code itself becomes a puzzle. There are a few reasons why unsophisticated reader may want to open a code: to add something humble, to fix a bug, to find out what happens, to become a part of the developers team.
Out of all those reasons, only ‘become part of the team’ may justify enforcement of ‘beauty’ upon reader. For all other cases it would leave reader utterly devastated. Most hilarious case for ‘beautiful formula’ is when unsophisticated reader want to fix a bug, and they need not only to debug actual misbehavior of application, but also to solve unsolvable and broken puzzle of such beautiful formula which is not only splendid, but also doesn’t work.
Code should hide unimportant irrelevant details outside of the scope and leave important in a plain sight.
Here example of ‘plain boring code’ which is not beautiful by any means, but accepted with gratitude by reader:
def save_settings(self):
"""Saves the current settings to persistent storage."""
self.brushmanager.save_brushes_for_devices()
self.brushmanager.save_brush_history()
self.filehandler.save_scratchpad(self.scratchpad_filename)
settingspath = join(self.user_confpath, 'settings.json')
jsonstr = helpers.json_dumps(self.preferences)
with open(settingspath, 'w') as f:
f.write(jsonstr)(from Mypaint editor).
Yes, every CS guru would cry on incomparability of beauty of matrix exponentiation with mere boring gloom of ‘save settings’ code.
But hell, the last one is readable! You can glimpse through code without meditating on each operator, dot and column, and still be able to understand what’s going on. In a beautiful formula if you skipped half of formula, the rest is useless. You need to work through the formula. No shortcuts allowed.
I work most of the time with code as operator. Occasionally I write my own. But I’m more a reader than a writer. And as a reader I detest code which takes time to work through. Elegant abstract fabric to generate classes? No, but thank you for the offer. Brilliant DSL implemented on top of applicative functors? I appreciate your efforts but I need to decline deeper involvement. Interceptor class with polymorphic type routing? Never again.
I understand that use of ‘unsophisticated reader’ as evaluation criteria may lead to a pure luddism, as any fancy construction will be denounced as ‘too complex’.
Still, I don’t want program to be viewed as list of formulae. I want it to be viewed as a text. Not as ‘literate programming’ (which is impossible to maintain), but as important text which I can read without stopping on each syllable.
