Programming

my faceChris Foley is a computer programming enthusiast. He loves exploring new programming languages and crafting nice solutions. He lives in Glasgow, Scotland and works as a software developer.

My Code Has Dozens of Errors

Imagine the situation: You're in your programming 101 class and are working on your homework assignment. You have to write a program to do something. Mortgages seem to be a dull but popular topic.

So, you're writing your dull Mortgage program (like you will even care what a mortgage is for another decade). You might have spent a couple of hours on the 20--50 lines of code but you're finally finished. You fire up the compiler and it spits back dozens of errors. It's all gibberish: "Undefined hieroglyph here", "wiggle bracket expected there".

It doesn't matter what you do, the error messages seem to change every time. There are two forces at work here. You suck at writing code (more on this later) and the compiler sucks at error messages.

When the compiler reaches the first error it might misunderstand the context for the rest of the code. Instead of doing the sensible thing and stopping it carries on blindly, misinterpreting the rest of the file and spewing out loads of garbage error messages. This is why the error messages seem to all change when you edit the file.

How to Fix Messes Like This

Delete it all.

Well, maybe not but do comment it out. Add a // or a # or whatever the comment syntax is to the start of each line.

This might seem like madness, especially after spending three hours on the code but an empty file (or one that's all commented out) has one very important property: nothing is broken.

Now uncomment one line. Just one. You can choose it but in some languages it might have to be a class or method definition (and you might need to uncomment a closing brace or end too). Then compile and run the program. If you get errors or unexpected behaviour then fix the program before continuing. Nothing is broken again.

Now do another line. Compile and run. Make sure nothing is broken.

Continue like this line by line until your program is finished. If you do encounter problems then you know exactly what line introduced the errors. You can fix that line or comment it out and choose another to reintroduce.

How to Avoid Messes Like This in the First Place

Even good programmers suck at writing code. Most of my lines of code are wrong in some way the first time I type them. It's even harder for new programmers because they are still learning the language. It's quite a lot of effort to remember all the punctuation, spacing, naming rules, nesting and all the rest. It's not a skill that comes immediately. It takes a lot of practice.

However, you can still write working code in a reasonable time frame. Basically it's the same as above.

After each baby step, compile and run your code. Stick to this and nothing will ever be broken for long. You'll certainly never have a mass of syntax errors to hunt down at once again.

I bet you are wondering if this is what "real" programmers do. In a sense it is. We write a small piece of code, test it and repeat. At some stage, you will be introduced to test driven development which is a really nice way of making sure large projects keep working all the time. Until then, my suggestions above should keep you from getting into a mess.

21 October 2014

Comments