Turning Debugging into an Artform

Krzysztof Buczynski
Nov 7 · 5 min read

Debugging, for some people, is a part of the development process, but for others, it is a can of worms that should not be opened. Whatever your relationship with debugging, it is much easier to tackle with the right tools.

“Debugging is like being the detective in a crime movie where you are also the murderer,” — Filipe Fortes

Using the right tools can turn debugging into a form of art, rather than a losing battle. PHP programmers, for example, know there is a wide range of tools and options that will aid with the debugging process as long as they have a basic idea of how to use them.

Personally, I work mostly in PHP, so let’s start there.

https://www.monkeyuser.com/2019/bug-fixing-ways/?fbclid=IwAR1arxs5HctmEcrtZOA7t7CVj_fvRMGlVGiGBP3lAVjE2dNm-NBkOdTZGzk
https://www.monkeyuser.com/2019/bug-fixing-ways/?fbclid=IwAR1arxs5HctmEcrtZOA7t7CVj_fvRMGlVGiGBP3lAVjE2dNm-NBkOdTZGzk
https://www.monkeyuser.com/2019/bug-fixing-ways

Back to Basics

Firstly, let’s talk about debugging as a concept and forget for the moment any specific tools or code snippers

  1. The first and most important rule is to have some distance from your own code or the problem you are working on. Taking a break. 10 mins, 15 mins or even 30 mins if you have to (stretch your legs, put the coffee on…). Clear your mind, then tackle the problem with a fresh head.
  2. Talk to the duck. Rubber ducking is a problem-solving method that forces you to talk through your issue, instead of jumping straight into distracting someone else, and in the process, often discover the solution yourself.
  3. Ask for help. You are never alone in the dev world. Colleagues, friends, forums, or a really enthusiastic guy on YouTube. It is better to have a fresh pair of eyes to look at the problem if possible. In our office, we are lucky enough to have a team of experienced devs to ask, but if you are not as lucky as me, that’s what the internet is for.
https://www.reddit.com/r/ProgrammerHumor/comments/bgrlu4/stackoverflow_in_a_nutshell/
https://www.reddit.com/r/ProgrammerHumor/comments/bgrlu4/stackoverflow_in_a_nutshell/
https://www.reddit.com/r/ProgrammerHumor/comments/bgrlu4/stackoverflow_in_a_nutshell/

Obviously, blindly copying and pasting a stranger’s code is not a great idea. Engage your common sense, and be wary of the source of the advice. Sites such as dev.to or stackoverflow.com are usually trustworthy enough, but even then this is not bullet-proof, you may need to do a bit of debugging. (Take a look at this recent study which found StackOverflow code to be bug-ridden.)

Simple PHP Commands

https://me.me/i/echo-svar-var-dump-svar-patricians-choice-for-php-debugging-fe7176b3cd784c5daacdaaa6753a83cb
https://me.me/i/echo-svar-var-dump-svar-patricians-choice-for-php-debugging-fe7176b3cd784c5daacdaaa6753a83cb
https://me.me/i/echo-svar-var-dump-svar-patricians-choice-for-php-debugging-fe7176b3cd784c5daacdaaa6753a83cb

PHP provides a lot of ways to aid you with debugging, from something as simple as or commands to much more sophisticated tooling like .

Let's talk about the basics of PHP, in the language, there are three (or four) commands to help devs dump data on the screen followed by a simple statement (if we want to stop execution of the app after the print).

example:

<?php$var0 = "Hi"; print($var0);

Expected Output:

Hi

Standard or statements just print out almost anything we put as the argument. Sadly it does not handle array or object types variables too well. In case of arrays it outputs they give for non-primitive types isn’t very helpful (e.g. `Array` instead of printing the actual array contents). It will print out the content of the object only if implements function.

example:

<?php$var0 = [];
$var0[] = "Hi";
print_r($var0);

Expected Output:

Array
(
[0] => Hi
)

A is just an enhanced version of the normal or commands which is also able to print objects or arrays out on to the screen.

example:

<?php
$var0 = [];
$var0[] = "Hi";
var_dump($var0);

Expected Output:

array(1) {
[0]=>
string(2) "Hi"
}

is a very special command. Instead of a normal print, it actually dumps the contents of the variable on to the screen including types, size, and content.

These basic tools that the developer can use to debug variables are the most common approach to debugging PHP. A simple print out of data is usually enough to determine what is happening as long as the developer has a good understanding of the code they are working with.

Printing Data for the Browser

Depending on the type of content of the print, a simple position of tags around the print should help with rendering the code correctly. However, it does not cover every data structure and may have some issues with XML data, for example.

As a personal preference, I like to print my data in the pretty format. It prints out the data dump in a much more human-friendly way.

Example:

print("<pre>".print_r($array_data,true)."</pre>");<pre>
Array
(
[0] => 0
[key] => value
[nested] => Array
(
[key] => 2
)
)
</pre>

Those PHP tricks to just print out the data are usually enough to solve most of the problems/bugs but sometimes you have to use a power tool to fix a problem rather than sticking with simple hammer and nail technique…

The Power of Xdebug

…Xdebug is a powerful tool designed for PHP developers. It allows you to step line by line through the code while reading every single variable’s content. It is the most powerful tool a PHP dev can use to solve the problem and it does not require a lot to get it running.

https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwj3-_fh_qLlAhXHxYUKHWJHA8oQjRx6BAgBEAQ&url=https%3A%2F%2Ftighten.
https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwj3-_fh_qLlAhXHxYUKHWJHA8oQjRx6BAgBEAQ&url=https%3A%2F%2Ftighten.
https://tighten.co/assets/img/blog/xdebug-vscode-6.png

The setup is very simple but varies depending on the type of operating system you’re using. These online tutorials can help with the setup. Once it’s setup just hook up the connection details to your preferred IDE (if possible) and start solving deeper problems.

I am not going to go into how to use XDebug as I would be here for the next month or so. All I will say that the tool is very user-friendly and any dev should be able to pick it up quickly. However, I will point out this article which helped me to learn how to use it - learning xdebug.

What Else Can Be Done?

Other than using the tools above (or equivalents), it is up to the original dev who designed the code. So please, when you write a function, class or just a simple algorithm, remember to leave a few comments around the code or write a simple file. It will help anyone who will have to debug the code and speed up the process. Code comments are not just for you, they are for anyone else who is going to read your code in the future.

BuildEmpire

The musings of breaking web tech.

Thanks to Michael and Nathaniel Treavis

Krzysztof Buczynski

Written by

BuildEmpire

The musings of breaking web tech.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade