Make code pretty using photography composition rules

Make code pretty using photography composition rules

A PHP tutorial about how photography composition rules can help make PHP code easy and beautiful to read and more valuable.

This is what I want to talk about in this article.

Browsing the web I have found an article that describes some rules that photographers use to make photos more beautiful.

I have asked myself if and how PHP coding can benefit from these rules as well.

The result of my brainwork follows.

Art helps art

I have already talked about software creation as art in my article about PHP frameworks.

Photography is art as well; how can it be of help to the art of software creation?

If you are a beginner photographer, if you follow some rules when you are composing a photo, you create more beautiful photos.

To compose a photo means to choose the point from where you will shoot the photo, which subjects to include, what to give prominence to and to make many other choices.

When creating software, if we create beautiful software, we enjoy this beauty as we enjoy the beauty of a photo. Furthermore, our software will be more durable and valuable.

While a photo is static, software is a living thing. It will benefit from its beauty because it will be easier to change and to adapt to the unavoidable changing of requirements.

Now let’s see how we can use rules borrowed from photography to make our code look better.

The rule of thirds

In photography this rule is about not putting the most important subject at the centre of the photo. We don’t want our photo to look like a mugshot. We want to make the photo lighter by giving space to other subjects of minor importance.

Writing software we can do the same thing. Actually we do it all the time. We don’t put all the important code in a single file. We balance the code among many files. Doing this our composition is lighter. It isn’t burdened by the presence of a single heavy file full of critical code.

Leading Lines

The rule of thirds wasn’t much of a surprise for me. When developing software we use the same rule all the time. The rule of leading lines, instead, has been a surprise because it has given me a new idea to use to make my code better.

When we look at a photo, our eyes are naturally captured by lines. Placing lines differently in a photo, we can change how we view the photo. A photo will draw our look differently if the lines run from left to right or from top to bottom.

How do I see lines in PHP code? I see lines of execution. Let me explain.

When we create a program, we aim to build a functionality. We imagine the software’s user typing some information and getting the wanted result, so we hope.

We imagine the user typing some typical information.

But a user might type atypical information as well. They might type a name where a number is required, and so on.

Our execution line is disturbed by many exceptions and special cases.

This way our look can’t benefit from following the leading line of execution. There are branches and bushes all around.

It could be a good idea to highlight the main execution line using custom colours for syntax highlighting. Many PHP IDEs allow customization of syntax highlighting.

We could use lighter colours for the parts of code that disturb the main execution line.

Symmetry and Patterns

Photos portraying symmetrical objects or patterns can make for a very eye-catching composition.

In software we can introduce symmetry in our name conventions and in the organization of files. Patterns of code (we can call them templates as well) can be used to reduce building time and the probability of mistakes.

Using symmetry and patterns, code can be read and understood in less time.

Viewpoint

This is easy. Change the viewpoint and you change a photo dramatically.

We humans have this wonderful feature that we each see the world in his or her own way. When we want to standardise people, we destroy this diversity. It’s a bit like destroying a tropical forest or making a rare species become extinct.

On the other hand, it’s smarter to take advantage of this diversity. It can bring a richness of solutions and ideas.

Background

Sometimes you want to take a shot of a subject and you don’t want the background to detract from it. Your photo would lack impact if the background is too busy.

When writing a PHP script, it may happen that the most important step is blended among many other less important ones.

To prevent this busy background of steps from detracting from the main one, it’s a good idea to merge the steps coming before the main one in a single “pre” function. The steps coming after can go into a “post” function.

For instance, if you have to run a query against a database, the most important step is the query itself. So you can group all the preparatory steps in a function and, similarly, all the closing steps in a another function.

This will highlight the main step, which is the query.

Depth

Photos are two-dimensional. How do you convey a sense of depth using a two-dimensional medium?

You include in your photo objects which are near you, at a medium distance and far away.

You use this idea when writing software by creating functions which break down a task in more and more detailed levels.

You write a function that calls three other functions which each call another three functions, and so on until you get to the lowest level of detail.

It may seem that to write a single function at greater length and in more detail may be better. Instead, it adds beauty, and reading easiness, to your code if you add depth to it.

Framing

Objects such as trees, archways and holes create frames around a subject, thus creating a focused image.

Developing PHP code, we have PHP frameworks. They create a frame around our code. They take care of basic functionalities and design choices so that we can focus on the high level functionalities we need to create.

Cropping

You crop a photo if you want to draw attention to a detail. You crop tight around it to prevent it from drowning in a sea of other subjects.

In your PHP code there might be details that you want to examine more closely now and then. You can tag them. I use the tag FIXME a lot. I have even defined a keyboard shortcut to insert it where I need it.

I use it to remind myself which parts of my code need more work.

You can define a tag and use it to mark all the code that needs to be changed to introduce or modify a functionality. Later, you make the changes and delete the tags.

Using a PHP IDE, you can define a custom syntax highlighting which you switch on when you want to examine code lines which have been given a tag.

Let’s call this “code cropping”.

Experimentation

Rules are made to be broken in PHP code writing as well as in photography.

A rule has to make sense for it to add beauty and value to your code. Don’t apply them blindly otherwise you may end up doing more harm than good.

Conclusion

This experiment of borrowing creative practices from photography seems to have produced interesting results.

They will help to write more beautiful and valuable software.

I don’t recommend using these techniques when writing a prototype. When your software still needs to find a final form, it’s better to write prototypes to showcase basic functionalities.

At this stage of software creation we don’t need to care about the beauty in our code. We want to create it fast and use it to stimulate the development of more complete and stable requirements.

So, at this stage, our software doesn’t need to be beautiful because that would be a waste. We will destroy this software once it has accomplished its function.

When we have stable requirements, we can begin to create new software in a final form and then we need beautiful code because this software has to last and to be easily maintained.