Stop Using Print and Die Statements
Tags: tutorials, debug, debugger, javascript
Published: 2019-06-28 00:14:35

---
title: Stop Using Print and Die Statements
published: true
tags: Tutorials,debug,debugger,javascript
canonical_url: https://codejourney.io/print-and-die-statements
---

Today we are going to go over debugging 101 tips for using print or die statements. Discussing what you should do & why. The tips presented here are suitable for devs of all skill levels. Simply follow the instructions below to get your code on.

### The Traditional Way

The statements below exist in every language & we all rely on them to [#debug](https://codejourney.io/tag/debug) at some point.

### Ruby:

```ruby
puts "Message goes here"
exit
```

### PHP:

```php
print_f("Message goes here")
die();
```

### Javascript:

```javascript
console.log("Message goes here");
fail;
```

### You Get the Point

Instead of the above, try using purpose built debugging tools. Believe it or not, using tools _can_ make you a better programmer. A senior engineer once told me (early in my career) that anyone who doesn’t use a debugger is a chump. I could not agree more.

### Seeing the Entire Picture

For those who are unfamiliar with debuggers, here is a quick introduction.

Debuggers stop a program mid execution, show the state of it & allow you to take a deep dive in.

![](http://www.codejourney.io/wp-content/uploads/Screen-Shot-2019-06-25-at-3.43.59-PM.png)<figcaption>A PHP script stopped mid execution using a debugger.</figcaption>

A debugger also allows you to step through your code, so you can see what is happening live.

![Code being stepped through with debugger.](http://www.codejourney.io/wp-content/uploads/Screen-Shot-2019-06-25-at-3.44.09-PM.png)<figcaption>A PHP script stepped down two lines in the same method.</figcaption>

Without this tool, seeing the entire picture of a program is next to impossible (if I am wrong about that, please show me a different way).

A debugger also allows you to check all current variable values at a specific point in execution. Globals, local variables, even variables buried deep in a private method. You can see it all.

![A list of variables shown in the debugger.](http://www.codejourney.io/wp-content/uploads/Screen-Shot-2019-06-25-at-3.43.00-PM.png)<figcaption>A look at all variables in the current execution context.</figcaption>

![A variable object extended.](http://www.codejourney.io/wp-content/uploads/Screen-Shot-2019-06-25-at-3.43.09-PM.png)<figcaption>An in depth look at the “$request” variable.</figcaption>

> Anyone who doesn’t use a debugger is a chump.

Debuggers also show you the call stack leading up to that point of execution. Some even let you jump around in the stack. It shows the file, and method that was executed.

![A call stack show in a debugger.](http://www.codejourney.io/wp-content/uploads/Screen-Shot-2019-06-25-at-3.43.31-PM.png)<figcaption>A call stack showed by the debugger.</figcaption>

### Engage with It

Not only can you take a look, but you can manipulate as well. Change the value of anything and execute the program based on the values that you have chosen at that time. The power that comes with this can be wonderful. For instance, you want to assure that your data being fed into the method is correct, you can change what is incorrect just to test if your code does what you expect. This is a double edge sword though, the rest of the program executes on the data that you changed. Be careful.

### Gain a Deep Understanding

By stepping into the code, you automatically make the source code of whatever framework/library you are working with relatable. You are able to visably see the code change your data. This helps gain a deep understanding of system control flow, especially when using a third party library or framework. I think one of the best ways to learn a framework is to dive into the source. A debugger forces that.

 ![Underwater turtle diving](https://www.codejourney.io/wp-content/uploads/dive-in.jpg)
Dive into the source

### Up Next

My next article in this series will be a closer look at the finer details of a [#debugger](https://codejourney.io/tag/debugger). So today I want to leave you with one action item that you can start to implement using Javascript.

### debugger;

```javascript
function someThing(test) {
  test.er = 'howdy';
  debugger;
  test.ing();
}
```

This keyword in [#Javascript](https://codejourney.io/tag/javascript) allows you to fire a debugger automatically when executed in Chrome or Firefox. Note that you must have your console open in order for the debugger to stop execution. Then you can step through and into methods as needed. No more console.log();

Use it. Get comfortable with it. It works.