Debugging Tips
Debugging is the process coders use to figure out why their code isn’t working. This section provides you with tips on how to make debugging easier for you.
Sometimes, you may already know how to code something but it will still take you a very long time to do it. Great coders still make mistakes, even when they know exactly what they are doing, so it’s important to build a toolbox of debugging techniques that can help you out when you aren’t sure what went wrong. Everyone has a different method for debugging their code and you’ll figure out what yours is after you practice a bit. Debugging can often be the most time-consuming part of coding, so it’s really important to leave plenty of time for it!
Using Test Data
If you have a lot of really complicated data to use in your app it can be helpful to use test data. Test data is a simpler set of data that you can use to make sure your app is working correctly.
As an example, let’s say you are making an app that shows the user where the closest restaurant is to them. You need to find the names of restaurants, the location data, and phone numbers. To get started building your app, you don’t need to have an entire list of restaurants ready in your fusion tables to start coding your app. You can just put in one or two test restaurants and get your app working before adding the real data.
Use Alerts
This can help you figure out why your code is broken. To put an alert in your app drag it over from the user interface list. It will appear as an invisible component. Here are some scenarios when this method of testing may be helpful:
- Debugging if your button is getting stuck, or your function is not working
- Test if a screen is loading or getting stuck
- Test if a loop is getting stuck
Collapsible Blocks
This is great feature to help you keep your code organized. Collapsed blocks will compress it down to one bar on your screen. If you right-click on a block, you’ll see an option to collapse it. Collapsed blocks still work the same as regular blocks, they just take up less space. This can be helpful if you have blocks you no longer need to edit and want to clean up your screen. To expand your blocks again, right-click on them.
Adding Comments to Your Code
Programmers often leave comments in their code to explain what it does. Comments can be helpful when other people are looking at your code, such as teammates, mentors, and judges. Comments can also help if you return to your code later and have forgotten what parts of it do.
Version Control
Let’s say you have one part your app built and working. You add some new code and suddenly everything stops working. You try to delete the new code but your app still isn’t working. You aren’t sure what went wrong and wish you had an “undo” button.
To avoid scenarios like this you can create different versions of your app. These versions will save your code so that if you make a mistake and don’t know how to fix it, you can go back to the last working version you saved. You can also use version control to experiment with new features without worrying about breaking your app.
You can make a copy of your app at any point. Click the "Make Copy" button located in the top menu. Rename your saved copies so that you can easily keep track between each version of your app.
Conclusions
Debugging is a natural part of the coding process, so don’t get discouraged if your app doesn’t work perfectly on the first try. Many times, the bulk of your time coding is actually spent debugging. Make sure to plan extra time for debugging.