Coding vs Writing

The problem most first-time programmers struggle with is that they treat coding like it’s writing. When you write an essay, you brainstorm, you develop an outline, and you write a first draft. Then you keep editing it and revising it until you’re happy. This is because you’re something of experts of the English language.

Unfortunately, you’re novices when it comes to Python – and that’s not a bad thing! But it does mean we have to change our approach to coding.

So when you’re faced with a daunting lab assignment (as you surely will be later on in this term,) the worst thing you can do is sit down, brainstorm for a bit, and write all of the code at once, thinking to yourself “Oh, I’ll just debug it all at once just like editing a paper.”

This is going to be miserable, and it will actually end up taking longer than the approach I’m about to suggest.

Write a single function. Nine times out of ten, even that single function already has a bug in it! Fix that bug now before trying to move on or write more functions. You should test and make sure your code works as soon as you write it. It’s more like writing one sentence and immediately making sure that it has no grammatical errors instead of writing the whole paper and proof-reading it all at once.

Why is this? When you’re writing a paper, even if one sentence has a typo or some small grammatical mishap, the general point still comes across. Think of readers as incredibly intelligent computers that can process what’s happening even though there are language errors.

Computers, however, are extremely stupid. They can only do what you tell them to, and if you have a tiny error in one of your functions, your entire program doesn’t work. Even worse, the functions that call the broken function don’t work, so you have no idea where to start looking for the error!

This is why it’s absolutely critical to write small amounts of code at a time and debug them immediately rather than later.