One of the books I am very fond of is Paul Graham's "Hackers and Painters". So much so that I asked my wife to read it to understand me better! Ah, youth and naïveté..
In it, among many other beautiful insights, Paul compared the process of writing code with the process of painting.
We don't really know what our end-result is going to be like as we don't really understand our problem until we start working on it.
We start with a crude draft. Getting it perfectly right isn't important in beginning. Running our early versions makes it obvious what doesn't feel right so we add more details, move things around to make the foundation stable. Touch up here and there to fix some edge cases. Running it, you may discover you have to optimize the code to run much faster, so you dive deeper into the problem to discover better data structures, representing the source problem more efficiently.
How is this related to painters? The main point is - painters start with a draft. They paint a very rough outline, diving deeper into the topic and adding progressively more detail and sophistication as they go. Always building on their existing work.
Occasionally, if you are very good and highly motivated, you can just site and write a fantastic complex piece of code, that works great from the first try. And is impossible to maintain.
The process of writing code is the process of perfecting your understanding of the problem.