Live data from Hacker News

Ask HN: How to reduce sloppy mistakes?

news.ycombinator.com

11–20 of 40 posts

Re: Ask HN: How to reduce sloppy mistakes?

#12
I like to write my code in my head before I write it with code. Try to imagine all your code and then the code you are going to write and then run through it in your mind. Think of some special cases in your mind. Suppose you are writing a function that accepts a parameter. Okay, so go write it in your head and then imagine, what if someone passes in a string or an int? are these different cases? do I throw an exception, do I return a particular value? How will that affect the rest of the code? What if the code is converted to a library and used by someone else? Could a string be a valid parameter?

Then run through the code in your mind. Does it work? Pass in some variables.

Your mind can process information much faster than you can type it out. If you build the image of your code in your mind, you can map it out in a matter of minutes, but if you start coding without the image, you make more mistakes.

Some other commenters have said write it out or draw it out. This is a good idea if you can't keep it in your head. Do flow charts and state diagrams and things like that before you start coding.

Re: Ask HN: How to reduce sloppy mistakes?

#13
...the number of sloppy mistakes I make is still way too high...

Compared to what?

You may not have a problem.

Sometimes "code, mistake, fix, code, mistake, fix, code, mistake, fix,..." is faster than "code it perfectly once".

For me it usually is. I'd rather crank stuff out quickly and fix it than drive myself nuts thinking it through.

Stupid syntax and typing errors will go away with repetition. Otherwise I wouldn't worry too much as long as you keep cranking forward.

[For what it's worth: I do not use an IDE, I do not use color or syntax highlighting, and I use debug as a last resort. Many here disagree with me, but I believe my brain internalizes those things that help me avoid stupid mistakes by coding without training wheels.]

Re: Ask HN: How to reduce sloppy mistakes?

#14
“The road to wisdom? Well, it's plain and simple to express: Err and err and err again but less and less and less.” ~ Piet Hein

In other news, I try to check my answers by deriving them two ways. If they agree, then I at least have a good defense :). If I'm working on something that can only be reached one way (like pulling data from a database), I assert boneheaded things that should be true about code + data structures. Sometimes it's superfluous, sometimes it catches mistakes.

If you don't work in a language that provides stack traces on crashes, add logging. You'll get a rudimentary stack trace of what was happening when everything went to hell, as well as have a small radius for where the crash occurs.

Re: Ask HN: How to reduce sloppy mistakes?

#15
Making fewer mistakes isn't the only solution. You can also make them cost less.

For example, I make a lot more typing mistakes typing into text editors than I used to do when typing on a typewriter. But I type net faster, by a lot, because fixing mistakes is so easy.

There are several ways to make mistakes cost less in programs: writing functional code; using a very abstract language; writing programs in layers; testing every change as you make it, in a repl.

You can switch modes, too. I sometimes start by writing something rapidly and comparatively carelessly, and then later when I reach a natural stopping point, I'll reread it carefully.

Re: Ask HN: How to reduce sloppy mistakes?

#16

Try setting yourself a mental challenge of having a subroutine/code snippet work the first time you run it. Start very small. You'll be surprised at how hard this is, and trying this repeatedly can build good habits of reasoning about code.

Strangely I had the experience (almost 46 years ago) of writing a program a week for a semester, without encountering either a bug or compilation error. It wasn't until my first assembly language program that I encountered one (a period in one statement where a comma should have been). They were to be sure, very simple programs (similar to the very easiest problems on Project Euler).

Re: Ask HN: How to reduce sloppy mistakes?

#17
post #13

...the number of sloppy mistakes I make is still way too high... Compared to what? You may not have a problem. Sometimes "code, mistake, fix, code, mistake, fix, code, mistake, fix,..." is faster than "code it perfectly once". For me it usually is. I'd rather crank stuff out quickly and fix it than drive myself nuts thinking it through. Stupid syntax and typing errors will go away with repetition. Otherwise I wouldn'…

Why would syntax highlighting, etc. be "training wheels"? I understand that training wheels restrain what you can do with your bike, but what's the downside to coding in an IDE?

Re: Ask HN: How to reduce sloppy mistakes?

#18
Code review. Even if you don't have someone to review it, you can pretend to explain it to someone else, a line at a time.

Error logging. When you make a mistake, and find it, document it, with as much of the reason why you made the mistake as you understand. Keep a diary of these for a few months, and you'll be surprised by how much you learn, as well as learning which mistakes you're most prone to.

Re: Ask HN: How to reduce sloppy mistakes?

#19

Try setting yourself a mental challenge of having a subroutine/code snippet work the first time you run it. Start very small. You'll be surprised at how hard this is, and trying this repeatedly can build good habits of reasoning about code.

This is good advice, but the best way to do it is paradoxical. Before you even run the code, debug it in your head. Pretend you ran it, and it failed in whatever way that you'd expect it to, assuming it were going to fail. You will find that this exercise causes you to hold yourself accountable, in ways that the traditional edit-compile-test cycle does not. When you truly hold yourself accountable, the silly mistakes will happen less often.

Re: Ask HN: How to reduce sloppy mistakes?

#20

“The road to wisdom? Well, it's plain and simple to express: Err and err and err again but less and less and less.” ~ Piet Hein In other news, I try to check my answers by deriving them two ways. If they agree, then I at least have a good defense :). If I'm working on something that can only be reached one way (like pulling data from a database), I assert boneheaded things that should be true about code + data struct…

+1 for Piet Hein - I love his hypererllipse and super eggs, as well as Melior (based on hyperellipes).
Post reply on HN