Source from: medium
Good programmers make better use of their analytical brain. They automatically reflect software challenges into a conditional, procedural set of rules. These rules, when executed correctly, help them define the challenge and act to solve it.
Those programmers often use that method to accomplish many other dilemmas in life; from finding their significant other to deciding what career path they should take. Many dilemmas feel very natural to be solved that way, but some might demand a more creative solution.
The Logical Truth About Coding
Writing code does not require intuition or emotion or a good sense of smell. To write good code, programmers must know their tools, must be familiar with related technologies and must know how to combine it all together and define a structured solution. If all gone correctly, the solution is then built and the challenge is finally conquered.
Most solutions require a programmer to first sketch the outline, then implement it part by part. These parts change in size and importance but always carry the same approach: define a start point, whether it is for a full class definition, or only for a single object or a function, then go on typing procedurally the whole thing.
With experience, we become more alert to where the code might collide or fail. When we write something that we feel isn’t right, we tend to undo a few steps back and re-write our code to go around the conflict. Mostly, I feel that, these undocumented, intuitively written code parts, are traps waiting to catch the inattentive programmer.
Presenting: Code By Heart
Aside from that big dominant analytical brain part, programmers also do offer an emotional, impulsive, imaginative brain. Unfortunately, they don’t make enough use of it before sitting to write code.
My secret ingredient for writing better code is imagination. I use mine for every piece of code I write. Instead of going straight from outline to writing impulsive code, I’ve added an extra step, and named it: code by heart. Practically, it means to test and learn our code so well that it can be written or implemented without even thinking.
First comes logic
After I know the problem and outlined the solution on paper (or just in my head), I write down the signature of my to-be-written function or object. That should allow me to just get into context with what I need to do next.
First, it makes me think of the way my new code part integrates into the existing code around it. Second, it makes me define the frame of the to-be-written piece of code, allowing me to better understand its responsibilities in the flow.
Then comes Heart
Then, I put my pencil aside and launch my super computer — my imagination. Fuelled with creativity, emotions and pumping blood streams, instead of tackling the code logically, I first imagine it in my head.
Just like any artist, I found that the right muse would maximize the outcome of that phase for me. You can try coding by heart while listening to your favorite music, or while watching the ocean. Some would get the best muse while in the shower, while others would love to code by heart before they fall to sleep. The need for a deeper muse is needed as the solution you’re after is more complex.
How it is done
The goal is simple: think about your code, about your structures. You know it already, cause you’ve written it or learned it. You should clearly see the main flow, you should easily know which part is responsible of which task.
Then, try to virtually put new functionalities in existing code parts. In your imagination, it takes only a second to write large and important functions. Also, it takes only another second to feel that the spot you impulsively felt it should go into, is or isn’t right for that function.
Really, when coding by heart, we save hours of forth and back coding on the computer. It takes roughly a few minutes to make a dozen runs of our whole program.
We know we’re done coding by heart, when there are no errors popping in our heads anymore. We know we’ve reached an efficient and correct solution, in our head, that is.
The last phase when coding by heart is also the most rewarding. After we’re done imagining, the code stands in our head in lines, waiting to be called to duty.
Finally, we sit to write code. When writing code that we’ve already run a few times in our head, we do it in a matter of minutes. We do it like superman reads a book. We don’t think about it anymore, we just spit it out, and it comes out fluent, clean and correct; first time.
Go on write some code. By heart.