User Avatar

Tim Urian

Software Engineering

4y ago

Writing about programming and learning.

I used see an error and groan, now I smile.

Why do beginning programmers rush past error messages? When you're new to programming, a lot of your code doesn't work. It seems easier to read your code and find the mistake instead of parsing an intimidating error message.

It's a bad habit, and one you should break early, because there's a better way.

Luckily, it is easy to understand these 'SOS' signals from your program. Here's how:

Read the whole message

That sounds silly, but how many times have you just passed your eyes over it? Some errors will be more clear than others, but don't just rush back to your code.

NoMethodError: undefined method `upcase!' for nil:NilClass

In Ruby the error has it's own class, in this case "NoMethodError". Then, it gives you a message that contains everything you need. It points out the name of that method that should have been defined, and the object it was expected on.

Go to exactly where the error occurred

The error will also come with a 'Traceback', which is a print out of the stack of calls that were made to get this error. The very top of that stack will have the number for the last line that was run.

test.rb:35:in 'cool_method': wrong number of arguments (given 1, expected 2)(ArgumentError)

In the above the error is saying "In the file test.rb, on line 35, while calling `cool_method' you sent the wrong number of arguments".

Simple enough here, but in sticky situations this line number will help you navigate quickly to the scene of the crime.

Remember the patterns you encounter

When you start paying attention to error messages you'll see the same ones over and over. You'll start to recognize the patterns that get you to the error and you'll start to defend against them in your code.

By doing these simple things you'll become a more efficient debugger and accelerate your journey to becoming a Ruby master.