How to use Debug Tools [Part 1]

Featured

The king of debugging

You know him, everybody does.

print("I am here")

Isn’t he great? He never let you down when you needed him, he never failed. Unfortunately, he has one big problem, he wastes your most valuable resource. Time

But, what do you mean?

Imagine being a line painter on a highway, your job is to paint lines across the highway. You have a variety of options to accomplish your task. You’ve started. First 10 meters were easy you just soaked the brush in the paint and started painting, first meter, second, third and you ran out of paint, so you came back to bucket with paint and again soaked the brush in it, you came back to the point where you finished. fourth, fifth, sixth meter and so on. After twenty times walking back and forth you started to feel tired.

This is when you are printing in your code. You will eventually find the issue, but it will cost you more effort and time.

Debugging tools

Most of the IDEs and great text editors have debugging tools. If yours don’t, you should switch it then.

Breakpoints

Probably the best debugging feature ever made.

Let’s show it in practice. We want to write a program that makes three steps forward (in a strange way). This is the code with a bug. I’ll be using C#.

static int MakeThreeStepsForward(int currentStep)
        {
            int newSteps = currentStep - 2;
            newSteps = newSteps + 5;
            newSteps++;
            return newSteps;
        }

Can you spot the bug? Of course, you can because it is a super easy example. But let’s say you don’t. So what would be the first thing you would do? Print statements (Console.WriteLine() in C#). Let’s try that for currentStep = 2

 static int MakeThreeStepsForward(int currentStep)
        {
            Console.WriteLine(currentStep);
            int newSteps = currentStep - 2;
            Console.WriteLine(newSteps);
            newSteps = newSteps + 5;
            Console.WriteLine(newSteps);
            newSteps++;
            Console.WriteLine(newSteps);
            return newSteps;
        }

The output of the program is 6, expected is 5. And there are our prints

2
0
5
6

This is not very readable, isn’t it? At this point, you start analyzing console output with the code. All right the bug can be fixed in the first line, there can be -3 instead of -2. It still isn’t hard, but this is “alghoritm” that has 4 lines of code. Imagine doing so in 40 places in your code. It would be a nightmare.

Now let’s jump to example with breakpoints

Breakpoints are stopping the program at the beginning of the line, not the end!

By creating a breakpoint on second line program stopped, showed values and you could inspect every value in the current context, you can check out everything just by creating one red dot! No more prints!

Remember our highway painter? He found a perfect solution on how to speed up his job, he started taking the bucket with paint with him! No more going back and forth. This is when you use breakpoints.

More tools

Every IDE or text editor that supports debugging has a lot more debugging tools, conditional breakpoints, function breakpoints, call stack, live watches and more! You should definitely check out what your editor has and not be afraid of them! This is key to fast and efficient development.

Debugging friendly editors

Most of the IDEs have great debugging tools, but not all the text editors. Personally, the best text editor for a programmer is a Visual Studio Code, it has a huge amount of community extensions, it is fast, and has great debugging tools!

Conclusion

I was surprised that my friend who is definitely not a beginner programmer still uses prints instead of proper debugging tools, they are not decorations, use them! Sit for 30 minutes and try what they can do, you’ll thank yourself later. Also, I’ll write about using some debug tools in detail. It was an introduction to the idea of debugging.

Update 1

It’s worth to notice that there are some cases where using print is fine.

My first open source project

Introduction

I’ve started building my first open source project back in November 2017. I actually thought to write it for fun, I didn’t know that I’ll be extending this idea, It was supposed to be a 30 min project. But many ideas came to my mind and this is how it’s started. Also, Pixi (this is how I named it) was a closed-source at first.

Heavy Squares.

When I was learning Microsoft’s GUI Framework called WPF I decided to make a very simple program to color pixels on click. Yes. Something very similar to MS Paint, but on a smaller scale, like a 3×3 grid. This idea came to me when I was reading about rectangles from WPF’s toolbox. Young stupid me thought “What if I’ll connect 9 rectangles and color them on click”. I quickly started to construct my brilliant idea. It was working. I can even say, that it was working perfectly. But don’t forget that it was just 9 rectangles. I saw potential in this project, so I started expanding it. This is when It’s getting funny, when canvas with squares began to be bigger (1024×1024 for instance) program was eating resources like stupid. A huge amount of ram (about 3 GB) just to create a 1024×1024 grid with squares. And of course, the code was messy too.

Refactoring

I started having troubles in understanding what was going on in the code. It was caused by my unconcern. Quality of code caused my fear to do any changes, so I stopped working on Pixi for half a year. After that time I got motivated again, and I started refactoring the code. In fact it wasn’t refactoring, it was rewriting program from scratch. I took me about a month to finish rewriting it, I’ll write more about refactoring in other post.

Current state

After successful refactoring, I decided to publish PixiEditor to github. You can contribute, download and modify the code. First usable version is available on releases page. Stay tuned!

Screenshot from first release of program

My first game

How it started

As I mentioned in My programming story I made a game. Bad one of course, but from scratch, by myself. And it was a HUGE milestone to me. It was the time when I stopped copying the code from videos/articles and built something mine. It’s worth to notice that I was 13 years old while building this game.

What was it about?

It is a labyrinth game called Potato Labyrinth, where you move a guy that is a potato (literally) to find a way to the exit. But it isn’t all. There are “fancy” mechanics in it, like portals, gates and I even made a dark level, where you only can see a few meters away.

First level

Dark level

Building

I made this game using Unity game engine and C# programming language, It’s a really powerful piece of software. Here is a short snippet of the game code that is responsible for the pause menu:

public class PauseMenu : MonoBehaviour {
    public GameObject pauseMenu;
    public static bool pauseMenuActive = false;
    // Use this for initialization
    void Start()
    {
        pauseMenu.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape)&&pauseMenuActive == false)
        {
            pauseMenu.SetActive(true);
            pauseMenuActive = true;
        }
        else if (Input.GetKeyUp(KeyCode.Escape)&&pauseMenuActive==true)
        {
            pauseMenu.SetActive(false);
            pauseMenuActive = false;
        }    
    }
   public void BackToMenu()
    {
        SceneManager.LoadScene("Menu");
        Points.points = 0;
        LevelPointsRequired.activelvl = 1;
        pauseMenuActive = false;

    }
}

The code is terrible, as you can see, but it was my first true creation.

Summary

My first game was a bad one, but that’s okay! Everybody has to start somewhere, even a bad game like that is the next step for something better, bigger and more ambitious. I’ve decided to share this game if you want to check it out here.

My programming story

It’s all the games fault.

That’s good! When I was a little kid I was playing games a lot. And because of this I always wanted to create my own. When I was about 9 years old I started to look for different game engines. Honestly, it ended always the same. I installed, looked at it and said “I don’t know how to make a game, this is stupid (I didn’t realize that I have to learn them first! I thought that when I open them I will know how to make a game lol). 

Minecraft

Yes, Minecraft. When (Again) I was about 9 years old, Minecraft was my favorite game. I was fascinated with mods. I started installing them, It was quite black magic for me, but slowly I understood how to do it. After some time I could install every mod I imagined (I felt badass 😎 ). My dream back then was creating my own mod, but again, It never happened (even to this day). After a time I wanted to have my own Minecraft server, and yes, this actually happened. First was a hamachi one, it was a milestone to me. Then I bought the server on hosting (I was 10 years old) and I set it up, even some people played on it, the server had plugins, and I knew how to use them. Next Minecraft multiplayer experience was combining mods and server. I made a modded server. After that nothing big happened in my Minecraft “carrier”.

My first touch with programming

I have finished 13 years old and finally installed the first game engine (Unity) that I learned. Started to watch tutorials on youtube, and I began to create my first game (I still haven’t got any coding skills). I after a few episodes od tutorial I understood that I have to learn to programme in order to achieve something working. And I did, I asked my older brother for advice, he told me that C++ is nice to start with. And that is how it began, it was the best decision I made in my life.  I watched a few tutorials and actually learned something. But I was thinking that it is waste of time and I jumped to C# (Unity is using C#). I started learning, I was writing console programs for about 2 months and after that, I started with Unity. For real. I made my first game. It was shitty but I learned a lot. It was my real milestone. To these days C# is my primary programming language, and I am using Unity.

Next steps

I was learning Unity and C# for a long time, but I felt that games are not enough for me. Then I read that there is technology that allows creating desktop applications using C#, it was called WPF. I got excited and Immediately started learning it. First, without any rules/design patterns etc. I was making big progress, but soon I’ve started to losing control of the code. The code was messy, hard to refactor and even harder to extend. My big project (I will write about it in another post too) has been no longer supported/frozen by me. After that, I learned HTML and CSS but it is not that important. I just know them.

I met with an interesting snake

Python. I managed to learn Python. It was something new to me because Python syntax is slightly different than most of the languages. Learning it took me 1.5 weeks I guess? I mean learning syntax. Now Python is my second primary language. I am still not an expert but I am using it often.

My first design pattern

My first design pattern was MVVM. I learned it to write applications in WPF. I began to revive my big project (it is called Pixi). I decided to rewrite the whole application with MVVM than refactor old. It was a good choice. It took me about 1.5 months, but finally did it, it is currently on GitHub and I am supporting it to this days (but again, I’ll write about it soon).

Now

When I am writing this post I am learning Kotlin, mobile development is very interesting and fun. I hope I didn’t forget anything important. See you in the next article 🙂