When Tim Peters was working on designing Python, he wrote
his beliefs down in 20 brief sentences, known as
The Zen
of Python. These can even be found built into Python
itself by typing "import this". Lines 3 and 4 from this passage
always stuck with me:
Simple is better than complex.
Complex is better than complicated.
The first is pretty self-evident. The second line threw me for a
loop. I didn't understand the difference. What did it mean, and how
did this relate to Python, and programming as a whole?
Complex or Complicated?
It comes from a more discrete meaning of the words.
Complex means a bunch of interlocking, working
parts that come together to form a system. It refers to one
concept that can be seen as a composite of smaller, independent
pieces. Complicated, on the other hand, refers to the
one concept that is large, and often confusing. To put it
another way, a building is complicated, where a city is
complex.
PHP and Python
By looking at Python and PHP, you can see the differences in how
the languages are put together. Both have a large number of
tools for a variety of purposes. in PHP, all of these
functions and objects are in the global namespace, and can be
called from anywhere. in Python, the language includes hundreds of
modules that perform a lot of the same things, but require them to
be imported. Python is complex where PHP
is complicated.
The benefit of thinking about building complex systems instead of
complicated ones, is the ease of understanding and changing these
systems. In a complicated system, you run into issues where
you have to keep a mental model of the entire system in your head
to effectively work on it. This also means that making
changes to the program is difficult, as the effects of the changes
can be much harder to predict. By breaking down large problems
into several smaller problems, it becomes much easier to solve.
Let's say we have a complicated web application, with various code
doing various things are mushed together. If we take one part of
the system, lets say database access for example, and we re-work it
so that this is all handled by its own independent module. We've
made the system more complex and less complicated, and there are
several benefits to this:
- Now, when we have to do any work that pertains to the
database, we don't have to worry about how that data is going to be
used, we only have to worry about a smaller problem.
- When we are using this database module, we only have to worry
about how we interact with it, and not about it's inner workings.
- If we were to ever change databases, now we can just replace
this one module instead of changing code in several different
places.
- We now have a database module that we can use in other systems.
So, by favoring being complex over being complicated, you can build
more flexible, maintainable, and useful systems.
Simple, right?