Try to write functions so that it does one thing. If there are few steps that had to be done try splitting it into chunks and then extract those chunks into private functions to make it easier to read. When you extract your code to another function give it a meaningful name. Sometimes it is good to look at the function after 1-2 days to see if you still know what's going on. And refactor, refactor, refactor! Many programming IDEs has nice refactoring tools so take advantage of those.
When I write a function I start with each big step of the task it has and write it as a separate function call and then write the code of the function accordingly. It's a similar approach to Carl's comments. Sometimes it helps to start with paper, programming doesn't start with writing a code ;)