One of the essays in the book Beautiful code is titled, "Treating Code as an Essay". The author is Yukihiro Matsumoto. Yukihiro is the creator of Ruby.
Here are some quotes from the essay:
1. "The style in which they are written is just as important as their purpose. Both essays and lines of code are meant - before all else - to be understood by human beings".
2. "Unreadable code will reduce most people's productivity significantly. On the other hand easily understandable code will increase it. And we see beauty in such code".
3. "Brevity is one element that helps make code beautiful".
4. "Simplicity ... We often see beauty in simple code. If a program is hard to understand, it cannot be considered beautiful".
As an example of brevity he gives the HelloWorld program in Java and in Ruby. In Ruby just the statement, "print "Hello World\n" constitutes the complete program!
However, I do find it hard to accept, "The real shortcut for elegant code is to chose an elegant programming language. Ruby and other lightweight languages like it support this approach". This is like saying English is more elegant than Kanji - or the other way around. What can be more elegant than substituting a couple of lines of English with a few brush strokes!
The book has an essay where the code is in Fortran, another where it is in Lisp. Obviously if the program is math intensive, Fortran is the lightweight language. If the application is AI, Lisp, I presume, will be the lightweight language.
No matter what the language, I believe the qualities mentioned by Mr. Matsumoto will characterize beautiful code.
Of course code should not become Haiku:-)
But Haiku is poetry. Code should be like good tight prose.
Consider what Churchill called the moral of his six volume "The Second World War" : "In War: Resolution, In Defeat: Defiance, In Victory: Magnanimity, In Peace: Goodwill". How's that for minimal brush-strokes?
No comments:
Post a Comment