How do I train myself to code faster and with fewer bugs?

Answer by A Quora admin:

I suggest that you start using Test-driven development .

TDD seems like you would be coding slower, but in fact, it has shown that TDD is a great leveler. For rockstars, TDD doesn't add overhead and doesn't provide any advantage either. TDD makes average to poor programmers much faster. in fact the worse programmer you are, the better TDD is for you.

The reason TDD makes you faster is because
a) Adding bugs is easy
b) Fixing bugs is difficult
c) The more time lapses between introduction of the bug and fixing of the bug, the harder it becomes to fix the bug

This is because of the very nature of programming. When you are programming, you are trying to construct of what the program should look like in your head, and then translating it into code. The problem is you are human. If God or Doctor Manhattan were a programmer, they could keep the entire structure of the program in their head and bang it out in one go.

Problem is, you are not God, or Doctor Manhattan. If you are of average intelligence, your intellect is always going to be smaller than the complexity of the program you write. If it weren't, people wouldn't be throwing gobs of money at you. The way you, the programmer, handle complexity that is larger than your intellect is by dividing the task into parts. You divide and you divide until you reach a point where the complexity fits into your head. The term that we use for this process is called modularization. You create the image of one module in your head, You see it as it should be.. in all it's perfection, down to the minute detail. and bang the code out. Once, you done  with the module, you throw that perfect image, take another module and create it's image. Essentially you are doing a mental context switch. Practically, once you discard that mental image, it doesn't go away immediately. It degrades in your memory. Basically, the biggest differrence between rockstars and poor programmers is the amount of complexity they can fit in their head at one time, and how slowly that model degrades over time. There are people who can fit highly complex algorithms in their head. There are people who can recall what exactly the code looks like 6 months after they wrote it. They are rockstars. Doctor Manhattan can probably remember code he wrote when he got his powers. Most people cannot do that. You cannot do that.

So, where are bugs coming from? Well, you could have misunderstood the needs, or the mental image that you make isn't as perfect as you think it is, or maybe you made a silly mistake while typing out the code. Now, when you have to fix that bug, essentially, what you are doing is recreating the mental image and looking at it for defects. Recreating that mental image requires work. The amount of work depends on how much that image has degraded in your mind. How many times have you found yourself reading the code saying "What the eff was I thinking here?" It gets harder the older the code is right? 6 months from now you say "Which idiot wrote this.. oops that was me", right? That's mental degradation of the model in practice.

TDD makes you faster by letting you find a bug the moment you introduce it. It avoids that costly context switch. Peer programming works because of the same principle. Since 2 people are creating the same mental model in their head, it's more likely that one of them will see a defect. It's easier to fix the defect while the mental model is in their heads. The basic principle here is don't let go of that mental image until you got the code perfect. TDD and pair programming are essentially 2 differrent ways of doing the same thing. If you are a novice to mid level programmer, most of your time is spent in doing mental context switches. You are essentially eliminating that overhead by doing TDD/pair programming. You might not perform like a rockstar because you are still keeping tiny pieces in your head. But, you can get pretty damn close to being a good to great programmer

Yes, yes, you can do a lot of other things which leads to you doing more design and writing less code. But, becoming a better designer is hard! It requires you to grow intellectually. It doesn't happen in one day. It takes years, decades to be skilled at design. You need to do that too If you are a beginner and/or a novice to medium programmer, using TDD will make you a better programmer as soon as you start using it

How do I train myself to code faster and with fewer bugs?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s