Stack Traces

m(i)s.adventures
3 min readDec 1, 2020

Ughhhh. One of the most frustrating things about learning to code is seeing a bunch of errors and stack traces. I used to freak out and not pay close attention to them because they were daunting to me. However, when I take one stack trace at a time, it’s not so bad. Stack traces are intended to help us learn where the exception came from.

Here’s an example:

After refactoring one of my Exercism exercises, I was expecting to see all passing tests. Instead, I saw 18 errors. Upon taking a closer look, they all appeared to be the same error — TypeError: Array can’t be coerced into Integer. Ok?! This was the first time I had encountered this error, and I had no idea what this error meant so I took a look at the last set of stack traces. According to the stack trace, this TypeError existed on line 42. What a great clue! So I went to line 42 in my code and took a closer look.

Original code:

My instinct told me that something was off with [@sides]. Here I had put the instance variable @sides into an array and my program doesn’t like it. To verify, I removed both sets of square brackets so [@sides] became @sides. Then I re-ran all my tests. Typically, we would just run one test at a time. Since all 18 stack traces were referring to the same error, I ran the whole test suite: ruby triangle_test.rb .

Revised code:

Happy tests:

In my code @sides refers to an array that contains 3 integers. @sides is the same as typing out [@side_1, @side_2, @side_3]. When I wrote [@side], I was telling Ruby this: [[@sides_1, @side_2, @sides_3]], which indicates that there is an inner array containing 3 integers. By removing the square brackets [] from @sides, we are telling Ruby, there are just 3 elements inside the array, which is the equivalent of [@side_1, @side_2, @side_3]. When #min is being called on an array, it gives us the array inside. We want this to be an integer, so we can call #sum on this array.

See, stack traces aren’t so bad after all. If I could give my past self an advice, it would be: stack traces are your friend, not your enemy. The sooner you learn to work with them, the sooner you’ll progress.

--

--