- Don’t assume – debugging is like science, not magic. You can spend a lot of time trying to assume what the issue is and tweak your code (only to undo it all later), or you can take a more systematic approach to finding the problem. It’s tempting to try to guess what the issue is and try to fix it quickly, but this way usually costs you more time in the long run.
- Stay calm. Sure it can be very frustrating, but debugging is part of the process and a great learning experience. It is also a crucial skill in programming so put on your detective hat and start looking for clues.
- Don’t be afraid of changing your code. You already have a bug, so you have to do something and while changes can feel scary this is a great time to implement them.
- Pay attention to the error message – it can tell you quite a bit. Also, when writing the code ment to handle potential errors in your program make sure to provide sufficient error information.
- Read the code and documentation. This one compliments rule nr.1. Read the code carefully and if you are working with a library or an API or any external piece with documentation – read the documentation and follow what it says without assumptions. Just because in Vue you might use X, that does not mean that it’s the same in React.
- Use descriptive names – it will make reading and understanding the code so much easier later on. That is more of a writing tip, but it sure makes debugging easier when you can follow along instead of having lots of weirdly named variables and functions that do / store who knows what.
- Console.log is your best friend. Console logging in different areas of your program can really help you understand what happens with your code. Do certain sections get executed, what values are returned, what is the data type? If you do console log more values, write a short message before the variable to help you know which message comes from where in the code.
- Is it the right data type? This one is especially useful for JavaScript as it is a dynamically typed language. You can just declare a variable without the need to specify its data type. The types are automatically converted during script execution and that can sometimes be tricky as what you might expect to be returned as a number will end up being a string. So console log typeof that variable whenever you might not be sure.
- Google is your other best friend as whatever problem you’re facing someone has encountered it before. I mean, what would we do without Stack Overflow, right? Googling will not only help you find a potential solution but will also lead you to trying out and learning new things which is always great.
- Split your code and think about the structure from the begining. It might feel hard when starting to learn programming, but it becomes easier with practice. First make sure that you split your functions so that each does just one task and as you progress think about using modular programming to separate and structure in manageable smaller pieces.