Document the behavior the program is meant to exhibit.
Write tests that would work if the program behaved as documented.
Write code that will pass the tests.
:-)
update: and sprinkle with liberal amounts of review by peers and refactoring at each step; if the documentation can't be understood, the interface is too complicated, so aim for clear documentation first.