The Tao of Exceptions Will Make You Error-Free
In the beginning, there was error.
The master programmer was about to write the most beautiful program ever written by mankind. He sat in front of his keyboard as the jumble of thoughts in his mind began clearing away at the smell of freshly brewed coffee. He carefully wrote:
void do_beautiful_stuff() {}
As the parentheses were closed, a question rose from the depth of the void, and it's answers needed to be enumerated; the question spoke thus: "how will they know that I, do_ beautiful_stuff(), have returned?".
The master programmer pondered the question, while many valuable clock cycles fleeted away into the void. At last, he carefully re-arranged the electrons:
int do_beautiful_stuff() {}
He looked at the screen and chuckled. I have answered the question, he thought. If do_beautiful_stuff() does indeed succeed, it returns 0, else it returns 1. Now one can merely look at the return value, and decide if it was a success or a failure.
The master programmer looked out of the window. A storm cloud was gathering in the distance. It will perhaps be a clear day when I am done with my beautiful program, the master programmer thought.
Little did he know that something exceptional was about to happen.
He dreamed of a wonderful world ruled by 0s and 1s.
The program was compiled, ready. With fingers shaking in anticipation, the master programmer typed:
./a.out
There was a pause, and then the giant wheels of the machine began to turn. Soon they were running in unison, and you could hear a melodic hum emanating from the machine. There was a trance-like quality to the hum, and the master programmer listened intently to the melody for any signs of ugly disturbances in the force. There were none.
Then all of a sudden the machine screeched to a halt. It wheezed and coughed, and finally spat out a single number. The master programmer looked carefully. The number was 1.
The error was born.
That was the beginning.
The master programmer has written 300 million lines of code. He now has a long white beard; he still wears his tie-dye shirt on Fridays. His name is spoken far and wide with fear and respect.
He has conquered exceptions.
His programs always return 0. This makes the master programmer very happy. Beautiful things happen in the world when a 0 is returned. There is harmony, peace, happiness, and the Tao is pleased.
The intern is puzzled.
The beautiful program, written by the master programmer, always returns 0.
Trembling in fear, the intern meets the master programmer. "I may be wrong", he says, "but I do not seem to find any way I can make the beautiful program return anything but 0. Can you, O Master Programmer, please show me how an error is detected and returned by the beautiful program, so I may learn from your infinite wisdom?".
The master programmer looks long and hard at the intern. The silence deepens in the void. The intern prepares to leave.
"Success is in not allowing failures to happen", the master programmer says at last, as the intern reaches the door.
The intern is enlightened.
The intern writes a new program. It never fails. It always returns 0.
The Tao rolls over in his sleep.
Comments
Hence a program without error has zero line of code.