Understanding Common Programming Errors
Learn about a few common programming errors and how to go about debugging them
As someone new to programming, there is just so much to learn and absorb when learning how to code — syntax, variables, strings, loops, etc. (the list goes on!) On top of that, every language has slightly different syntax. With that said, there’s no doubt that throughout your coding journey, you won’t come across errors and bugs when testing your code.
While it may seem intimidating and overwhelming when you first come across a whole bunch of errors you did not expect to see, errors are actually a good thing, they’re actually our friends!
You see, computers will try and execute as much of your code as they can, but if there is one little curly brace or quotation mark missing, that one tiny mistake could throw off your program. Sure it can be frustrating, but one thing that I’ve found so helpful and important is to actually read what is right in front of me, read the programming errors!
Now I know that sounds simple, but again, as someone new to programming, the first thing most people may want to do is freak out, or rather … bug out. Why isn’t this working?! Well, if you actually take the time to carefully read what your terminal is trying to telling you, you can more easily pinpoint the error and fix the problem!
SyntaxError
Let’s take a look at this example code below. We have an array of fruits and want to print a few of them out using different Ruby enumerables. I go and run the code and get the below error.
main.rb:9: syntax error, unexpected tIDENTIFIER, expecting ‘)’
The first thing to do is identify where and what type of error this is. From the example above, it says main.rb:9: syntax error. This is a great sign. Our terminal is clearly stating where the error is, line 9 from our main.rb file. It is also stating what kind of error, a SyntaxError, and what it is expecting — expecting ‘)’. As we continue to read our error, it’s pretty much telling us how to fix it.
Now if we go back to our example and look back at our block of code, you’ll notice that fruits.include?(“strawberry” was missing a closing parenthesis, just like what the terminal was saying. Perfect, one bug fixed. Many more to go!
As you continue to run tests, whenever you come across syntax errors, this is likely because of well, incorrect syntax. A few examples can include a missing curly brace, parenthesis or pipe to start or close your block of code. Make sure to double check when writing methods that if you start with a do, to make sure to end with an end. It’s very common to miss a closing end, but Ruby is our friend and will help us catch these errors.
ArgumentError
Another common error you may come across are ArgumentErrors. If you take a look at the below code, I tried to print out my favorite ice cream. However, it did not run correctly because my fav_ice_cream method was missing an argument.
1: from main.rb:9:in `<main>'
main.rb:5:in `fav_ice_cream': wrong number of arguments (given 0, expected 1) (ArgumentError)
The error above is showing that on line 9 from the main.rb file, there is a wrong number of arguments. It also shows that on line 5, in the fav_ice_cream method, it tried to run but was given 0 arguments when it was in fact expecting 1. Oops, I forgot to add an argument, let’s go ahead and fix that.
Sweet! Now our code is working properly and Ruby knows that my favorite ice cream is mint chocolate chip!
The reason ArgumentErrors happen is typically when you pass in too few or too many arguments. Sometimes, there may already be a default passed in for you, but make sure to double check for what the method or function is looking for.
TypeError
Other times, you may come across a TypeError. An example of this would be below.
main.rb:27:in `+': no implicit conversion of Integer into String (TypeError)
The reason for TypeErrors is to show that different data types cannot be concatenated. The above code is incorrect because num_of_plants is defined as an integer. We cannot add a string and integer together. We also cannot make a string into an integer. What we can do however, is change num_of_plants to a string by adding num_of_plants.to_s which will make the number 56 become “56” or use string interpolation with #{num_of_plants}.
Now, our bug is fixed and will print out “The crazy plant lady has 56 plants in her house!”. Another bug solved!
Oh and just to clarify, yes I am a plant lady, but no, not a crazy plant lady!
With TypeErrors, be sure that all of your data types are the same. If not, they will unfortunately not play well together.
NameError
The last common error I want to touch on are NameErrors, which probably happen to me the most. NameErrors happen when a name or variable is undefined or invalid. Take a look at our below example.
1: from main.rb:15:in `<main>'
main.rb:12:in `adopt_pup': undefined local variable or method `puppiess' for main:Object (NameError)
Did you mean? puppies
I just tried to run a method that would select a random puppy to adopt, but it seems like I have another error. Hmm, it says that I have an undefined variable or method “puppiess”. Ruby then asked if I actually meant “puppies”?
Typos are usually the most common reason for NameErrors, for myself at least. Another reason why this may also come up is if you try to call a method that has not been defined yet. Ruby can’t run a method if it doesn’t know about it! Make sure your method or function is clearly defined before calling it.
When coming across NameErrors, make sure to run through your code with a fine-tooth comb. It can be really tough to detect that small typo as our eyes will tend to glance right over it. Alright, let’s fix up this typo and now we should be all set to adopt that puppy!
Sometimes the answers are right in front of us …
Syntax, argument, type and name errors are just a few errors you’ll come across. I wanted to touch on these as I found them to be the most common during the start of my coding journey. If there’s anything I’d like for you to take away from this article, besides puppies being the best and that ice cream solves problems, it’s the importance of reading the actual errors and what is right in front of you.
Often times we are already given or have the resources we need to solve a problem. So next time you come across those bugs, don’t panic, just continue to read the code. As I mentioned earlier, Ruby is your friend and will help you out along the way. Keep learning and keep coding because …
Resource: Ruby-Doc.org