The art of debugging

In a perfect world, our software never run in an unexpected way.

Unfortunately, we don’t live in a perfect world, so sometimes (quite more than sometimes) we have to face some strange errors.

That’s when we must learn the gentle art of debugging

Follow me through easy steps and you’ll wipe the bugs out of your life

1) A bug never appears “sometimes”

Believe me, the word “sometimes” has been invented for losers. A drunk would say he “sometimes” gets drunk, but in my opinion he gets drunk EVERY TIME he drinks too much. That’s another way to live your life. “Sometimes” means you don’t know when. You can’t live this way. You are a programmer.

So, you must know when your script has a problem. Exactly. Try to replicate it. Insert breakpoints, print variables, do whatever you can to recreate the problem EVERY TIME you want.

Do you want an example? … let’s see this script

a = a random number between 0 and 9
b = 10/a

This script sometimes crashes… but you must know when it crashes… and we can say the script crashes EVERY TIME a = 0, because of a divide by zero error.

When you can describe the problem starting with EVERY TIME, proceed to step 2

2) A bug never appears for the sake of appearing

Believe it or not, Gods aren’t upset with you. There isn’t any virus in your computer. “THEY” aren’t trying to drive you mad, and leech your brain because they want to rule the world.

The bug appeared for a reason. In the previous example, the reason was a number cannot be divided by zero. It’s not a plot against you. You simply cannot divide a number by zero.

You must understand why your script does not work… in this case it was a division by zero error, you have to find your case.

Now you know when the bug appears, and why it appears

3) A bug is a useless creature

Ok, bugs are the only good actors I can see in horror movies for a couple of years, but they are quite useless… and maybe they can survive to a nuclear war, and I don’t really want to survive to a nuclear war just to face giand radioactive bugs, so make up your mind, you don’t need bugs.

I mean you must find a way to get what you want with no risk to encounter a bug. In our example, you must decide if you really need a random number between zero and 9, or if a number between one and 9 would do.

With numbers from 1 to 9 you solved the bug, but sometimes you may decide you need the zero too, and in this case you would perform the division only if the number is different than zero.

This is the step where you must decide if you want to obliterate the bug or handle it as an exception.

Needless to say the first option is the best, because you can manage one, two… maybe five… TEN exceptions, but sooner or later your script will collapse if you work this way.

4) Put a big shoe on the bug’s head

It’s the time to rewrite the bugged code… keep in mind what your script is intended to make, and what you don’t want to happen. Since at this time you will probably are a bit tired, proceed step by step. Throw a shoe to a bug from a big distance, and you’ll miss it. Get closer and closer, until… SQUASH! It will know the power of the mighty shoe.

5) It wasn’t a simple bug. It was a cyborg bug

If your bug dies with a little red light fading away, and you can hear something like “I’ll be back”… then it wasn’t a simple bug… it was a cyborg bug. In real life, fixing a bug can bring new bugs to life.

In my previous example, I can fix the bug setting a as a random number between 1 and 9.. but I can fix the bug coding the second line as b= 10/(a+1).

According to what I am expecting from the script, one of the ywo ways of fixing the bug can make the bug return later. You have to forecast future bugs and prevent them

That’s all… this was obviously an ironic post, but there is some truth in it… how do you debug your scripts?

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers