I use a little tool that runs unit tests on compilation, when Im doing something new that unit tests works well with, I write a bunch of tests and just code until I have 100% tests passing.
Ask HN: How to reduce sloppy mistakes?
11–20 of 40 posts
Re: Ask HN: How to reduce sloppy mistakes?
#12Then 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?
#13Compared 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?
#14In 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?
#15For 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?
#16Try 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.
Re: Ask HN: How to reduce sloppy mistakes?
#17...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'…
Re: Ask HN: How to reduce sloppy mistakes?
#18Error 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?
#19Try 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.
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…