Beautiful Code [75]
[]Dr. Dobb's Journal, March 1996.
I agree from the bottom of my heart. Ruby is meant to be the personification of this ideal, and while it is far from simple, it supports simple solutions for programming. Because Ruby is not simple, the programs that use it can be. This is true of other lightweight languages as well; they are not lightweight in the sense of ease of implementation, but they are called lightweight because of their intention to lighten the workload of the programmer.
To see what this means in practice, consider Rake, a build tool like make that is widely used by Ruby programmers. Unlike Makefiles, which are written in a single-purpose file format, Rakefiles are written in Ruby, as sort of Domain Specific Language (DSL) with full-featured programmability. Example 29-2 shows a Rakefile that runs a set of tests.
Example 29-2. Sample Rakefile
task :default => [:test]
task :test do
ruby "test/unittest.rb"
end
The Rakefile takes advantage of the following shortcuts in Ruby syntax:
Parentheses for method arguments can be eliminated.
Unbraced hash key/value pairs can appear at the end of methods.
Code blocks can be attached at the tails of method calls.
You can program in Ruby without these syntax elements, so in theory, they are redundant. They are often criticized for making language more complex. However, Example 29-3 shows how Example 29-2 would be written without using these features.
Example 29-3. Rakefile without abbreviated syntax
task({:default => [:test]})
task(:test, &lambda(){
ruby "test/unittest.rb"
})
As you can see, if Ruby's syntax were stripped of redundancies, Ruby the language might become more elegant, but programmers would have to do more work, and their programs would be harder to read. So, when simpler tools are used to solve a complex problem, complexity is merely shifted to the programmer, which is really putting the cart before the horse.
The next important element in the concept of "beautiful code" is flexibility. I define flexibility here as freedom from enforcement from tools. When programmers are forced to do something against their intentions, for the tools' sake, the result is stress. This stress negatively affects the programmer. The end result is far from happiness, and far from beauty as well, according to our definitions of beauty in code. Humans are more valuable than any tools or languages. Computers should serve programmers to maximize their productivity and happiness, but in reality, they often increase the burden instead of lightening it.
Balance is the final element of beautiful code. So far I have talked about brevity, conservatism, simplicity, and flexibility. No element by itself will ensure a beautiful program. When balanced together and kept in mind from the very beginning, each element will work harmoniously with the others to create beautiful code. And if you also make sure to have fun writing and reading code, you will experience happiness as a programmer.
Happy Hacking!
When a Button Is All That Connects You to the World > Basic Design Model
30. When a Button Is All That Connects You to the World
Arun Mehta
Professor stephen hawking can only press one button," was the one-line spec we were given.
Professor Hawking, the eminent theoretical physicist, has ALS. This disease is "marked by gradual degeneration of the nerve cells in the central nervous system that control voluntary muscle movement. The disorder causes muscle weakness and atrophy throughout the body."[*] He writes and speaks using the software Equalizer, which he operates via a single button. It uses an external box for text-to-speech, which is no longer manufactured. The source code for Equalizer