search
top

Ice cream: The Debugger

“Never use print() to debug again.”

In programming Journey, we have heard errors or bugs. All programming languages like C, Java, .Net etc. have their ways to handle these errors and bugs.

 
 
 
 
I remember, when I was learning programming and used to write my programs in C and C++, how difficult it was to debug the errors. This happens with everyone. It sometimes takes very long time to locate the issue and its reason and then we are in trouble!

 
When it comes to find errors in any programming language, it seems like a nightmare for those who are learning to program. This becomes more difficult when working on a big program with massive amount of code. While I was reading online resources, I came across an interesting tool ‘Ice cream’ which is a handy debugger and it can be used in many programming languages. That’s why I thought of sharing with the readers of this blog.

 

Introduction

 
Whenever we see errors or warning while writing code in any programming language, it is really difficult to figure out the erroneous lines of code. The use of “print()” or “log()” is a commonly used method where we insert additional lines of code to trace errors in existing code. But this might be confusing sometimes to find the output for a particular code section because of the many lines of code.

 
If you have some lines of code like:


str1 = 'Welcome to TechnoDossier blog.'
str2 = 'TechnoDossier is a popular technology blog.'

print(str1)
print(str2)

This will give the following output:

Welcome to Techno Dossier blog.
Techno Dossier is a popular technology blog.

 
Here it can’t be found which one of these outputs is from variables str1 or str2. If we go back to the code, we can easily figure that out. But what if we have hundreds of lines of code and with many variables printing different outputs? To try to find the source code that is responsible for the output can be time-consuming. One way to do so is to add text to the print statements to make it easier to figure out.


str1 = 'Welcome to TechnoDossier blog.'
str2 = 'TechnoDossier a is popular technology blog.'

print('str1', str1)
print('str2', str2)

This will give the following output:

str1 Welcome to TechnoDossier blog.
str2 TechnoDossier is a popular technology blog.

 
Now this is better, but again, it is time-consuming to write text with each print statement for longer codes. Is there any way to print the code that is responsible for the output without writing additional text help tracing? Yes, let’s have a look.


str1 = 'Welcome to TechnoDossier blog.'
str2 = 'TechnoDossier a is popular technology blog.'

ic(str1)
ic(str2)

This will give output as follows:

ic| str1: ‘Welcome to TechnoDossier blog.’
ic| str2: ‘TechnoDossier is a popular technology blog.’

 
voila!!!, that is where ice cream comes into the picture.

 

What is ice cream?

 
Obviously, when we hear about ice cream, we think about the different flavors of Ice cream, candy, etc. But this ice cream is a little bit different. This is a python package which is used for debugging. Yes you heard it right!! Ice cream is a tool to let programmers debug their code without writing extra lines of code and without worrying too much about locating the erroneous code.

 
The rest of the discussion will use Python language, but it can be used in many other languages and it is under development for other languages. It is very easy and simple to use even if you are a beginner.

 
Ice cream was first released on Jun 7, 2018 with version v1.3. After that, every subsequent versions have some fixes with more features. In Ice cream v2.0.0.

  • Added: Support for Python 3.8.
  • Removed: Support for Python 3.4.
  • Changed: Whitespace in arguments is no longer collapsed. Indentation in multi-line arguments is now preserved.

Latest version of Ice cream is v2.1.0. Ice cream supports Python 2, Python 3, PyPy2, and PyPy3. Let’s get started.

 

How to Use ice cream?

To install ice cream, open the terminal and type:
 
pip install icecream
 
and press enter, that’s it, you are good to go. This will install latest version of ice cream.


from icecream import ic
 
def greeting(name):
    return 'Welcome to Techno Dossier blog, ' + str(name) +'!'

ic(greeting('John'))

The output is printed as:
ic| greeting(‘John’): ‘Welcome to Techno Dossier blog, John!’

 
By using ic , we do not only see the output but also see the function and its arguments! How convenient!

 

Inspect Execution

 
Many a times, we might use print to determine which parts of our program are executed, and in which order they’re executed? For example, we might write the code shown below.


def interested(user:bool):
    if user:
print('I am interested.')
        #any logic
    else:
print('I am not interested.')
        #any logic

interested(user=True)

The output looks like:
I am interested.
 
ic() helps here, too. Without arguments, ic() inspects itself and prints the calling filename, line number, and parent function.


from icecream import ic
 
def interested(user:bool):
    if user:
ic()
    else:
ic()

interested(user=True)

The output is:
ic| :5 in interested() at 06:30:11.433
 
Just call ic() and you’re done. Simple.

 

Return Value

 
ic() returns its argument(s), so ic() can easily be inserted into any existing code. By doing this, we can know which variable got which return value.


d=4
def sum(i):
     return i+2
n= sum(ic(d))
ic| d: 4
ic(n)

This produces the output:
ic| n: 6

 

Ice cream in other Languages

 
As mentioned above, ice cream can be used with many programming languages besides Python.

Dart icecream
Rust icecream-rs
Node.js node-icecream
C++ icecream-Cpp
PHP icecream-php
Go icecream-go
Ruby Ricecream

 
So, start using this amazing tool in your favorite programming language and get relaxed when you debug your code. Hurray!!!

  • author's avatar

    By: Divyesh Mangroliya

    Divyesh is passionate and enthusiastic towards artificial intelligence and machine learning technology. He loves programming and also likes to share his knowledge with people. He likes to read technical invention articles as well as non-technical books.

  • author's avatar

  • author's avatar

    See all this author’s posts

6 Responses to “Ice cream: The Debugger”

  1. Eric says:

    Very Informative, Thanks for sharing.
    Keep it up.

  2. Aishawariya Athawale says:

    Really helpful article. Keep it up 👍

  3. Mohit Talreja says:

    Very handy and useful for the programmers. Awesome explanation

Leave a Reply

Your email address will not be published.

top