Skip to content

How To Get Unstuck

At Xojo we want everyone to learn programming. But even though Xojo is easy to use, especially when compared to other tools, programming is still challenging. At some point, everyone gets stuck somewhere. Here are some tips that might help you out the next time you get stuck, regardless of what programming language you use.

Think about what you expect the code to do

This is probably the most important thing. Don’t just write (or copy/paste) code and then blindly run it. Sure, that can work, but when you get stuck it will be much harder for you to understand why your code is not working like you expect.

So the first thing to do is to carefully read through your code and get an understanding of what you expect it will do. Understand each line and the behavior it should cause. Don’t assume anything.

Often this seemingly simple process can make something click in your mind that will reveal the problem to you. You’ll also learn a lot.

Break your code into multiple lines

It is often convenient to have your code do multiple things on a single line. For example, this Xojo code does 4 different things:

ValueField.Text = Str(Calculate(Val(InputField.Text)))

If you get a run-time error on this code or the values are just not what you expect then it can be trickier for you to figure out.

Instead, break this into multiple lines like this:

Dim inputText As String = InputField.Text
Dim calcResult As Double = Calculate(inputText)
Dim outputValue As String = Str(calcResult)
ValueField.Text = outputValue

In particular this will also help you out when you use the debugger.

Use the Debugger

Speaking of which, take advantage of the debugger to help you understand what your code is actually doing. When your code does not work as you expect, the debugger can help show you exactly what the code is doing rather than what you might be thinking it should be doing.  You’ll be able to step through the code, line-by-line and watch variables as their values change.

To use the Xojo debugger, just set a breakpoint (click in the gutter of the code editor). You can also use the Xojo Break command to set breakpoints based on conditions.

Use Logging

Sometimes the debugger is not an option, such as when you are working with things that are called very frequently such as UI events for drawing or mouse movement. In this case you can revert to the old-school logging technique. Every language has some sort of logging command that outputs text to the system log. With Xojo you can use the System.DebugLog command to output information to the Messages panel in the IDE, which you can review while your app is running.

Take a break

If you just can’t figure something out then sometimes you just need to take a break. If you’re stuck on one train of thought and can’t break out of a pattern, walking away from the problem and coming back to it later with a fresh set of eyes can make a world of difference. You may find the solution jumps right out at you immediately.

Explain it to someone else

A great way to work through being stuck is to try to explain the situation to someone else. I’ve lost count of the number of people that have told me they answered their own question after taking the time to write up an email to a coworker or a post to a forum. In the process of writing the post (or shortly after they posted it), the answer came to them before anyone had a chance to reply.

This is sometimes called “rubber duck debugging”.

Reduce your code

Sometimes you just have too much in front of you and it is overwhelming. Maybe your method, design or project is just too large and you’ve lost a grasp of it.

So when all else fails, start reducing your code. Strip things down to the bare minimum that is working like you expect. And then start adding back things one at a time, verifying your expectations along the way. Eventually you should be able to pinpoint where the problem first occurred so that you can focus on solving it.

I hope one of these techniques is able to help you out the next time you get stuck. If you have other techniques share them with the community.