Wolfram Archive
Wolfram Programming Lab is a legacy product.
All the same functionality and features, including access to Programming Lab Explorations, are available with Wolfram|One.
Start programming now. »

If your code doesn't work... that means it has a bug.

How to Debug Your Code

Debugging is often what takes real programmers longest, but there are some simple principles that will help you debug quickly... (Just so you know: automation in the Wolfram Language usually makes debugging much easier than in other languages.)

Correct Your Syntax

Computer languages like the Wolfram Language have a definite syntax. If you don't follow it, you'll get a "syntax error" like this:

The error will be somewhere in the highlighted part. Things to watch for:
  • unmatched brackets etc.: every opener, like ( [ {, must have a corresponding closer of the same type, } ] )
  • make sure every string that starts with " ends with "
If something looks weird—and particularly if you got it by copying from somewhere else—try retyping it.

Follow the Autocomplete!

If the Wolfram Language doesn't seem to understand what you typed, try deleting the last thing you typed, then retyping and watching the autocompletes that the system gives:

If what you're typing isn't in the autocomplete list, there's a good chance it's wrong.

Look at the Documentation!

If you want to know what a function does, just read its documentation! Move your mouse over a function name, and you'll get a little tab. Press the (i) to get information on the function:

You can always copy an example from the documentation and run it. (In Wolfram Programming Lab for Desktop, you can run the example in place in the documentation.)

Do a Smaller Example

If your code doesn’t work as a whole, break it down and run separate parts separately... seeing if each part works. If you need to, try going all the way down to individual functions, and try them out. Get examples from the documentation.

Read the Message(s)!

If the Wolfram Language prints out a message (or more than one), it means something isn't going right:

Read the message, and think about what it says!

It Didn't Do Anything...

The Wolfram Language is symbolic, so if it doesn't have anything it can do with a particular input, it'll just leave that input unchanged, or "symbolic". Sometimes it'll print a message, though:

If a function returns without having done anything, look very carefully at what arguments are fed to the function—because there's got to be something wrong with them.

The Graphics Are Pink!

If you ask the Wolfram Language to display as a graphic something that isn't a meaningful graphic, it'll put up a pink box instead:

Use InputForm to see what the underlying graphics expression is, and then work out why it isn't correct:

It's So Slow...

If you run code and it never stops (the cell bracket stays highlighted and the notebook title says "Running"), you may be caught in an infinite loop. Abort the computation by pressing ALT+. (⌘-. on Mac):

Your code might also just be taking a long time because you've asked it to do a very large computation. For some computations, increasing the input a little bit increases the computation a lot.

This is a small computation:

But if you increase the number to 20, the result explodes to over 1,000,000 items, and will take longer to produce than you're probably willing to wait. You can abort the computation by pressing ALT+. (⌘-. on Mac):

Sorry, But It's Probably Your Fault

If the computer didn't do what you thought you asked it to do, you shouldn't blame the computer. There are occasionally bugs in the built-in code of the computer (or even in the computer itself), but they're much, much rarer than bugs in code people write. So just go on looking at your code to find out what's wrong. If you're really convinced there's something wrong inside the Wolfram Language, then find a simple example, and let us know! We want everything about the Wolfram Language to work perfectly.

Things to Watch For

Brackets ([{...}]) Matter!

Be sure you've got the correct kind of brackets. (), [ ], and {} all mean different things. Look up examples to see the kind you need.

= (Set) Is Not == (Equal)!

Use a single equal (=) to set the value of a variable:

Use a double equal (==) to test for equality:

UppER cAse/Lower Case Matters

The Wolfram Language distinguishes upper and lowercase letters. It just won't understand if you don't use the correct case. In built-in function names, like CirclePoints, each word starts with an uppercase letter.

Commas Matter!

If you leave out a comma, the Wolfram Language may try to multiply things together:

Use a comma if you want two separate things:

See Something You Don't Know How to Type?

Some symbols are typed as sequences of characters:

de es ko