Live data from Hacker News

Ask HN: How to reduce sloppy mistakes?

news.ycombinator.com

31–40 of 40 posts

Re: Ask HN: How to reduce sloppy mistakes?

#31
I've found that copious use of assert statements has done wonders for reducing the amount of time I spend debugging. Its a good idea to use enough assert statements that when there's a problem it is reported immediately instead of propagating and causing weird symptoms that you have to track down. At the beginning of every function I think to myself "what am I expecting here" and then put in 2-3 assert statements to make sure my expectations are met. For example, if I'm passing a data structure that I expect to be filled, I assert that it is non empty.

When I encounter a bug I write an assert that reveals the problem before I fix it in order to prevent having to deal with the same issue more than once. This is especially big for me since I work with a lot of (other people's) data.

Re: Ask HN: How to reduce sloppy mistakes?

#32

Speculation: Based on the principal that you can't improve what you don't measure (or, what you measure you improve), you could keep a log of the sloppy mistakes you make. After a while, maybe patterns will emerge and you can attack 'mistyping numbers' separately from 'missing cases in a switch block' and 'correct behaviour of nested-if took 5 attempts'.

I was going to write exactly the same thing, but you've put it more eloquently than I could. Also, it would seem to me that just "writing more test-cases" wouldn't get one very far as the tests are going to be as sloppy as the original work.

Re: Ask HN: How to reduce sloppy mistakes?

#34
post #10

Using a statically typed language might also help, as the compiler can catch a lot of really stupid errors.

for those of us using dynamic languages like ruby, make a habit of hitting Ctrl+Shift+V (Validate Syntax in TextMate) or your editor's equivalent. That will catch the dumbest of all errors (missing parens, block terminators, string terminators, etc). alternatively you can add a validate_syntax step to your deployment strategy and just run something like >> ruby -c whatever.rb on files that are part of the most recent commit.

Re: Ask HN: How to reduce sloppy mistakes?

#35
Everyone's going to say "test driven development" or "think before you write" but honestly, the only thing that has ever truly worked for me is pair coding. Just have a buddy sit over your shoulder and watch you work.

It's a bit like the integrity mechanism of developing two copies of the same software, done by totally separate teams. You and your friend are both thinking about how to solve the problem, but separately, which means the likelihood that both of you make the same dumb mistake is fairly low.

Re: Ask HN: How to reduce sloppy mistakes?

#36
Before you hit compile, look at the key lines of code that you just wrote and think about exactly what you expect them to do. Then compile and run the code. Don't rush to compile. It'll take longer in the long run. Also, it's less enjoyable to program that way. I prefer a careful, focused style where every line is carefully written.

I know what you mean about hitting F5 all the time. I usually program that way when I don't feel like thinking. I want to cross my fingers and hope that it works. When I notice I'm doing this, I need to stop and think about what it is that I'm doing. I'll often write down in text what I'm trying to do. Or I'll turn off the computer and think about it.

Re: Ask HN: How to reduce sloppy mistakes?

#38
While it can be fun to make big sweeping changes, I find that silly errors are easy to catch when I keep all my changes small so here's what I do:

- When I'm working with a fast compiler, I setup a small CI system to immediately compile my code and run my tests every time I save my changes. If I can't do this for some reason then I setup a trivial background script to do this constantly and watch the output in another window.

- I try not to cut corners. It's easy to stay in the flow once I have things setup so that I can build and extend my tests and scaffolds along with my code.

Small moves, Everest, small moves...

Re: Ask HN: How to reduce sloppy mistakes?

#39
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'…

I started coding back in the day with plain old Windows Notepad. I think not having line numbers, not having syntax highlighting and all that fancy stuff definitely helped me.

Re: Ask HN: How to reduce sloppy mistakes?

#40
Use shorter code/compile/run cycles. Use print statements instead of a debugger (much faster to scan input than step through a debugger). If something doesn't work, it's likely related to the change you just made. If you coded for a long time since last testing, it's much harder to mentally identify where the problem might be.

It's ok if you don't get it perfect all of the time. It's the 20 minutes debugging that's killing you. Shorten your cycle and eventually you will get less sloppy (or immediately know where/what the sloppiness is before you even run). It will be like that typo you "know" you made even before you finished typing (something won't feel right and you'll go back and fix it before you read the debugging output).

Post reply on HN