Get Unstuck
Tags: programming, tips, stuck, xojo
Published: 2017-11-13 16:00:40

---
title: Get Unstuck
published: true
description: Programming is challenging for everyone and sooner or later everyone gets stuck on something. Here are some tips that you can use to help you get unstuck.
tags: programming, tips, stuck, xojo
canonical_url: https://blog.xojo.com/2017/11/13/how-to-get-unstuck/
---

At Xojo we want everyone to <a href="https://blog.xojo.com/2017/06/26/learn-the-fundamentals-of-programming-with-xojo/">learn programming</a>. But even though Xojo is easy to use, especially when <a href="https://blog.xojo.com/2017/05/25/making-a-cross-platform-desktop-app-comparing-xojo-and-visual-studio-for-mac/">compared to other tools</a>, 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 <a href="http://developer.xojo.com/userguide/using-the-debugger">use the Xojo debugger</a>, just set a breakpoint (click in the gutter of the code editor). You can also use the Xojo <a href="http://developer.xojo.com/break">Break</a> 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 <a href="http://developer.xojo.com/system">System.DebugLog</a> command to output information to the <a href="http://developer.xojo.com/userguide/find-errors-messages-panels$messages">Messages panel in the IDE</a>, 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, waling 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 href="http://forum.xojo.com">a forum</a>. 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 “<a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging">rubber duck debugging</a>”.

## 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.

*This post originally appeared on the [Xojo Blog as How to Get Unstuck](https://blog.xojo.com/2017/11/13/how-to-get-unstuck/).*