How to Think like a Programmer When Problem Solving
Do you want to learn to problem-solve like a computer programmer? Then you have come to the right place.
Here we explore how the average programmer tackles problems and suggest ways to help you do the very same. A better, more efficient problem-solving future awaits you.
At least in theory.
What is problem-solving in programming?
Before we begin, it might be worth nailing out what problem-solving tends to mean for programmers.
According to the School of Computing at the University of Utah, it is often defined as:
"Problem-solving [is] solving problems is the core of computer science. Programmers must first understand how a human solves a problem, then understand how to translate this 'algorithm' into something a computer can do, and finally how to 'write' the specific syntax (required by a computer) to get the job done."
Sounds simple on the surface, but, as you are about to find out, it is actually something of an "art" more so than a science (to a certain extent).
Hold on tight...
How do you think like a programmer?
Steve Jobs famously said that "everyone in this country should learn to program a computer because it teaches you to think". This is a very powerful message and doesn't necessarily mean you need to become the next Ada Lovelace, Steve Wozniak, or Bill Gates.
Developing "soft skills" or "meta-skills" like learning how to think clearly and critically will pay dividends in many aspects of your life.
Especially in your professional career, whichever route you have chosen to take, be it an engineer or not.
Along with other important "meta-skills" like perspective-taking, pain-spotting, story-telling, interviewing, and connecting the dots, mastering this skill is widely considered essential in our technologically complex world. In fact, many employers prioritize problem-solving, and basic coding to a certain extent, over most other skills when looking for potential new hires.
"Problem-solving skills are almost unanimously the most important qualification that employers look for….more than programming languages proficiency, debugging, and system design.
Demonstrating computational thinking or the ability to break down large, complex problems is just as valuable (if not more so) than the baseline technical skills required for a job.” — Hacker Rank.
So how can you develop such a skill? Well, one way is to attempt to think like a programmer.
If you know any in your friend group your first port of call could be to have a frank and open discussion on this. It may feel awkward in the beginning, but you will quickly learn and love the process.
But, essentially, programmers learn, usually through trial and error, to become progressively more effective at problem-solving. Most people, unless you have developed a robust system, will likely involve something like the following:
1. Attempt a solution.
2. If it doesn't work, try another one.
3. If that also fails, rinse and repeat step 2 until you find something that works (perhaps through luck).
While it sounds like a reasonable approach, this methodology is both taxing on your mental energy and, most importantly, valuable and irreplaceable time (your only real asset in life). This is, in effect, the brute force method of solving problems.
Programmers are masters of a more subtle and effective way of doing this through a plan, do, check, act strategy. This involves, in effect:
1. Having a framework approach to tackling a problem.
2. Practice, learn from mistakes, review, and improve until you master it.
While, initially, this takes a big investment of your time, once you are well versed in it, it will pay dividends in the future. You should also attempt to nurture some other life-style habits (like those of Elon Musk) that will stand you in good stead when problem-solving using a programmer mindset.
How can I improve my programming problem-solving skills and develop a programmer mindset?
As we have previously mentioned, the best way is to develop a framework to problem-solve and then practice, practice, practice. But what does it mean to have a framework for problem-solving?
There is a vast library on just this subject, but in essence, a problem-solving framework will have some, if not all, of the following main components:
1. Break down an understand the issue at hand
The first step in your problem-solving framework should be to attempt to know exactly what is being asked, or required. Many, apparently insurmountable, problems are just so because we do not initially understand them in totality.
So, how do you do this? One indicator that you've grasped the issue is by being able to explain it in plain English, or even a diagram.
By thinking, and vocalizing, the problem you will quickly find the holes in logic, or your own understanding that you might have missed beforehand. This is one process that most programmers will be keenly aware of.
Write down the problem, have a crack at sketching it out (perhaps using a mind map), and try to explain it to someone else.
2. Failing to plan, is planning to fail (make a plan and break down a large problem into smaller ones)
The next step is to try to formulate a plan of attack. Like anything in life, never start something until you've done this (especially things like investing your money).
Break down larger problems into smaller, more digestible chunks and think of ways to tick them off the list. You probably developed something similar to this during your time studying at university or any other time in your life.
For programmers, they do not dive straight in without getting this phase sorted. The time invested at this stage will save you a ton of wasted time later down the road.
This is the programmer mindset.
Try to answer a question like, "given an input, what are the steps I need to return another output?", etc. Not to mention a network of "if this then that, or something else" set of instructions.
This is critical for programmers as they need to develop subroutines, functions, methods, and procedures, and other smaller bits of code, that must work as a whole.
An error in one can, and usually will, impact other parts of the code and can even lead to issues like infinite loops. By working through each stage of the main problem in this way, errors and solutions can be tackled as and when they crop up.
It is also not unusual for a hang-up to require problem-solving in multiple smaller self-contained chunks of code too. This is, in essence, a way of connecting the dots.
On a side note, this is also one of the main reasons comments are used in code by programmers. Especially when working as part of a team (no one can read another's mind).
But, we digress.
You should not be afraid to ask for help, or so some research. You may not have to reinvent the wheel. But DO NOT rely on other's solutions to your problems as you won't learn and develop your own mind and skill in a particular area.
"If I could teach every beginning programmer one problem-solving skill, it would be the ‘reduce the problem technique’...
Reduce the problem to the point where you know how to solve it and write the solution. Then expand the problem slightly and rewrite the solution to match, and keep going until you are back where you started." - V. Anton Spraul.
3. "Stay on target", you can and will get stuck at times
"The best-laid plans of mice and men often go awry," as Robert Burns famously said. Don't worry, this is to be expected, and often embraced.
But, what do you do when you can't solve a problem, especially when programming?
While many will throw up their hands at the first sight of a problem, or solution that fails, most programmers have developed an attitude of curiosity towards "bugs".
Not to mention a determination to fix them.
When they crop up, programmers will usually initiate a process, a mental function if you will, of the following:
- Engage in a debugging process by going step by step through their written solution to find obvious problems (like misuse of syntax).
- If that fails, reassess by taking a step back and try to "find another angle" to the problem. Can anything be abstracted or are you missing something? In other words are you "missing the forest for the trees"? It is very, very common to get so bogged down in the detail that you often miss the bigger picture.
- If all fails, you can simply start all over again -- you'd be amazed at what solutions can be found.
4. Practice makes perfect
A little cliche? Perhaps, but you can't expect to solve all complex problem or sub-problem, overnight. Being good at anything takes time to practice, fail, try again, and master.
As you build up your experience in problem-solving, the process will become quicker and quicker as you identify methods that you have used previously that can be applied in a given situation. In fact, many of the most successful people regularly partake in "mini-problem-solving" regularly.
Some play chess, others sudoku, yet others crosswords or even computer games, on a regular basis. Try to find one that you enjoy and work that grey matter!
And, probably most importantly of all, do not fear failure. You will learn far more from where you mess up than you ever do from where you succeed. Learn to fail, and fail fast!
And that's a wrap. Your path to problem-solving as a programmer awaits!
Good luck traveler.