I usually think about the problem far away from the computer until I know which algorithm to use and how to structure the code. For harder problems, sometimes I consult books or the Internet, to get inspiration. Then I just sit down and write the code, in small steps, checking each step if it works correctly, and writing appropriate unit tests. Writing the code usually takes me less than 1/5 of the whole time I was thinking of the solution, for the current project I work on (which I must admit, is pretty hard - see QUCS[1] source code to get the idea)
Another thing is, when I write enough code, I refactor it, because after writing the code I usually understand the problem better than before. It is just for improving performance, readability, and maintainability because at this time the code usually works.
Of course, for larger projects, the whole process is more incremental; nevertheless I usually don't start writing code if I haven't thought enough well what I want to accomplish, what I'm going to change and what it is going to break.
Trying to write the code without thinking first, in belief that "I'll change it until it works", is a bad idea. Even if it finally happen to work by accident, it probably has some serious flaws in it, and you also didn't learn too much.
I get easily confused, mix my variables up and I can not put in words what I want to write in code. |
Maybe try to switch paradigms? Sometimes imperative programming is not a natural way of programming for some kinds of brains. Many people don't have problems learning to use spreadsheets (which is kind of data-oriented functional programming), but have terrible problems understanding imperative programming. E.g. try to learn Python. Really, really, great language to grasp the basics. When you know the basics, move back to C++.
[1] No, I'm not a devleoper of QUCS, we just work on a similar closed-source product. BTW, we have about 15x less code then they have, yet it offers similar functionality and orders of magnitude better performance, although it is run on JVM, not native compiled C++.