Debugging is a skill that can be improved by doing. You will get better at it by trying and learning from your mistakes.
In this article, I share with you a super simple method that I use to get better at debugging in less than 5 minutes of work.
Let’s say you have an error in your code. You try to fix it but it does not work. You are not sure what the problem is and how to fix it. What do you do?
You could read the manual, search on Google, ask questions on StackOverflow, but most importantly: Try to think like a computer!
What kind of error message did you get? What was the last thing you did? What was the last thing before that? What is the first thing that came to mind when you saw the error message?
Now try to write down this chain of thought in pseudocode.
Example
def multiply(a, b)
return a * b
end
puts multiply(5, "string")
Error message:
TypeError: no implicit conversion of String into Integer
Pseudocode:
def multiply(a, b) # def means function definition; multiply is the name of our function; (a, b) are parameters or arguments; # a comment; return means return value; a * b is the return value; end; # end means end of function definition; puts means print out; multiply(5, "string") is an argument or parameter for our function.
Why is this useful?
- “Try to think like a computer”: Computers are stupid! They only do what they are told to do. If they make a mistake or do something unexpected, it is usually because we (the programmers) did not tell them what to do correctly.
- “Don’t repeat yourself”: The more similar two pieces of code are, the more likely there is an error in one of them.
- “Always look at the error message”: The error message tells you exactly what went wrong and where. It often points directly at the line where there is an error.
- “If you don’t know how to fix it… ask for help!”: We all make mistakes and we all learn from each other. Don’t be afraid to ask for help! That’s why we have StackOverflow and mailing lists! Also: Try using asking smart questions. Don’t just post your code and say “It doesn’t work!” Help us help you by giving us more information about what you tried already.
What to do when you get an error?
- Read the error message.
- Don’t panic! At this point we know that there is an error in our code. So don’t panic and try to understand what happened.
- Look at the traceback (the sequence of method calls that lead up to this error). This shows us which code caused the error and where in our code it happened.
- Try to understand what kind of error it was (NameError, TypeError etc.). If you are not sure, search for it online or ask someone who knows more about programming than you do.
- Google for your specific problem or error message. Find some solutions online and try them out on your computer.
- Ask someone who knows more than you do (and who uses good English). Maybe they can give you a solution right away. If not, try asking people on mailing lists or StackOverflow.
- Keep trying different things until you find something that works. If nothing else helps then try asking for help again (go through all steps from “Try to think like a computer” again). Remember that asking for help isn’t a sign of failure but rather an indication that you are still interested in learning and improving your skills as a programmer.
{title: “Using Ruby Classes”, body: ‘
Classes – Object Oriented Programming
Ruby is an object oriented programming language which means that everything in Ruby is an object (an instance of some class). Even numbers are objects! Let\’s see how this works…’, user_id: 1, notebook_id: 2},
])