You should really distinguish between "programmers" and "software engineers." A programmer is someone who writes code, a software engineer is someone who plans, designs programs, tests, and maintains code.
A monkey can program. You just sit and type lines of code until you meet your specs. What goes through a programmers mind is fairly simple: find a way to meet the requirements. Usually this requires referencing some manual on the platform interfaces and arranging the correct function calls and objects into a way that works, and by works I means passes unit test.
A common task for most software engineers is to debug code already written. This is like detective work, except the culprit who wrote the code probably no long works at your company. This is very difficult as often times there is little to no documentation, so you have to be good at divining the programmer's intentions from the code. This requires much inductive and deductive reasoning to be able to find the cause of a bug, then fixing it is easy. When a softE changes existing code, they have to worry about teh change breaking another part that already worked (this is called regression), and they have to investigate a lot of avenues in order to boost their confidence that the fix worked.
So to answer your question, most programmers don't even know what they are thinking. They just try to get it to work, and if it looks like it does, great. Some poor software engineer down the road will have to ask the same question "what was this guy thinking?" and fix their mistakes.