Subscribe to Jack of all Languages        RSS Feed

My Code Has Dozens of Errors

Icon 4 Comments
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.

Start with a blank file.
Write a class definition. Make sure it compiles.
Write a main method signature. Compile it. Run it.
Print something to the console. Compile, run.
Read some user input. Compile, run.

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.

4 Comments On This Entry

Page 1 of 1

Martyr2 Icon

21 October 2014 - 09:04 PM
You tricked me, I thought you were going to go on some rant that involved telling people to go read a book or something and not mention the baby step approach. But then you came around and gave the excellent advice. Nice! :)

cfoley Icon

21 October 2014 - 11:33 PM
Oh yeah, you should read a book too. I very much enjoyed A Clockwork Orange by Anthony Burgess. It's short and sweet so you can get back to coding.

Nallnaom Icon

03 February 2015 - 10:09 PM
3 months from now people are still reading this, sir. Thank you for reminding! Now off to stress again! :gun_bandana:

cfoley Icon

04 February 2015 - 06:04 AM
Thanks for the nice message. Don't stress too hard!
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2017

15161718 19 2021


    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)