A Lesson in Humility
I started writing my first computer game when I was 16. It was a puzzle game where you had to move bricks and make them disappear. A couple of friends joined the project, and soon the game featured a detailed concept, graphics, levels, and even a penguin mascot (the Linux mascot was yet to be invented). Only the program had to be written. At that time, I was coding in the assembler language on a C64 computer. At first, programming was easy. In juvenile audacity I was convinced that programming meant throwing enough brain at writing code, until it worked. I had already written many small programs like this and everything had worked fine. But soon, programming the graphics and game mechanics turned out to be more difficult than I had thought. I was spending days trying to get small improvements to work, with little or no progress. My programming workflow was
Turn on the computer
Load the compiler
Load the program
Write a few lines of code
Run the program
Frequently, the program would crash and take down the operating system
Switch off the computer and return to step 1
Effectively, I was spending not more than a rough 10% of my time actually working on the code. It is not surprising that development slowed down and soon came to a halt completely. At the moment, the project is 23 years late. It took me very long to figure out what had happened. Any mentor (an experienced programmer or an older version of myself) would have insisted on finding a shortcut from step 5 to step 4. Such shortcuts existed at the time in the form of a cartridge you plugged into the back of the computer. Finding these shortcuts is not a trivial matter. Yet they make all the difference. We will call these shortcuts Best Practices .
The Case for Best Practices in Python
Today, 23 years later, we have Python , a language that makes many things easier. We dont have to restart the computer when the program contains an error. We have libraries like Pygame that help us to create nicer and faster graphics with very little code. Do we still need Best Practices for writing Python code? Although you will anticipate my point of view from the title of this book, I would like to start with a simple example that explains why it is still worth to think about Best Practices in Python. Suppose we want to create graphics for our own small game using Python and Pygame . As a proof of concept, we will load two images, one with a level and one with the player figure, and combine them to a single image (Figure ). The functions in the Pygame library do most of the work. To combine the images and save them to a new file, only five lines of code are needed:
Figure 1-1.
Two images to be combined with Pygame
from pygame import image, Rect
maze = image.load(maze.png)
player = image.load(player.png)
maze.blit(player, Rect((32, 32, 64, 64)), Rect((0, 0, 32, 32)))
image.save(maze, merged.png)
At first sight, the program looks dead simple. What is more, it works correctly and produces a merged image. The program is too short to fail or is it? I took the time to enumerate ways in which this five-line program might fail:
A typo in the program terminates Python with a SyntaxError
Pygame is not installed, so Python exits with an ImportError
An incompatible version of Pygame is installed, so Python terminates with an Exception
One of the image files does not exist, so Python exits with an IOError
There is no write permission for the output image, so Python terminates with an IOError
One of the image files is corrupted, so Python terminates with an Exception
One of the image files is distorted, so the output image will be distorted as well
The program is incompatible with Python 3, so users are dependent on Python 2
Generating the image is too slow, so the game becomes unplayable
Image transparency is not handled correctly, so artifacts appear in the output image
A wrong output file name is given, so an important file is overwritten
Pygame contains malicious code, so system security becomes compromised
The program is distributed with unlicensed images, so the authors have IP lawyers at their neck
The program does not run on mobile phones, so nobody wants to use it
There is no documentation, so potential users are unable to install or use the program
I am sure you can find even more potential problems. Even in a five-line program, many more than five things can go wrong. We can be sure that even more things can go wrong in a long program. When we look at the list of problems more closely, we see that some of the problems are clearly connected to the code itself (e.g., the wrong import). Others, like the lack of documentation or the legal issues) have nothing to do with the code itself, yet they have severe consequences.
Conclusion
Some problems in programming can be solved by programming. Other problems in programming cannot be solved by programming. As programmers, we are responsible for both.
No matter what the problem is, we, the programmers, and our users will have to live with the consequences. I am frequently using my own five-line Python programs for small tasks (e.g., merging PDFs or shrinking digital camera images). As the only user, I can easily change the code or rewrite the program completely. But if we want to write bigger programs, programs with more users, and cooperate with other programmers, we need to prevent our projects from coming to a standstill. We need to prevent running into too many problems at the same time. We need techniques that keeps our program healthy and easy to work with. The premise of this book is to introduce you to established techniques or Best Practices to write better Python programs.
The Origin of Best Practices
How can we create well-written programs that solve or avoid the previously described problems? There are several schools of thought that have put a lot of energy into the subject. Here, we will explore whether they help us (see Figure ).
Figure 1-2.
Building a house as a metaphor for building software. Upper left: Hacking, love for the challenge of overcoming technical limitations. Upper right: Software engineering, a systematic approach found in large projects. Lower left: Agile, fast, iterative development. Note that the product is inhabited by the end user while development goes on. Lower right: Software craftsmanship, focusing on doing things right using a set of established tools.
Hacking
According to Richard Stallman, hackers have love of excellence and programming in common ( Hackers: Wizards of the Electronic Age, 1985, TV documentary). They enjoy the creative challenging to overcome technical limitations and achieve things not thought possible before. People who are good at hacking are indispensable in todays technology-driven society. Hacking is a key skill, and we need people who are good at it. Without doubt, hacking is a useful skill in programming. Python is an excellent language to hack things in.