How to solve problems in programming: A step by step guide

How to solve problems in programming: A step by step guide

ยท

5 min read

Problem solving is one of the greatest skills any developer can have. So if you want to be a great developer, consider topping your problem solving skills. Problem solving involves taking a logical and rational approach. After all, programming is all about logic.

In this article I will be sharing with you the four steps of problem solving skill as taken from Jonas Schmedtmann in his course; The Complete JavaScript Course: from zero to expert. These approaches passes as standard approach to problem solving skills and has helped me in becoming a better developer as I have adapted them in my problem solving activities. I hope it does for you too. Though I used JavaScript for my example, these steps indeed can be effectively incorporated in other programming languages and generally, in other areas of life.

  • Understand the problem:

Problems or tasks usually look overwhelming at a first glance without deep understanding of the problem. Make sure to 100% understand the problem even if it means reading it over and over again. Take a high level look of the big picture. And the most important part of this step is to ask the right question in order to get a clearer picture of the problem.

For Example, the project manager tells you to write a function that reverses whatever that's passed into it. You can ask the following questions.

๐Ÿ‘‰ What does 'whatever' mean in this context? What should be reversed? - Only strings, numbers, and arrays can make a reverse because they have order

๐Ÿ‘‰ What should I do if something else is passed in?

๐Ÿ‘‰ What should be returned? Should it always be a string or same type as passed in?

How to recognize whether the argument is a string, number or an array

๐Ÿ‘‰ How to reverse a string, a number or an array.

With these questions above, a background understanding is gained on the task.

  • Divide and conquer:

Break a big problem into smaller sub-problems. The smaller problems are easier to solve. This ties with the previous step whereby you understand the bigger problem by dividing it into smaller problems.

Smaller problems example:

๐Ÿ‘‰ Check if argument is a number, a string, or an array

๐Ÿ‘‰ Implement reversing a number

๐Ÿ‘‰ Implement reversing a string

๐Ÿ‘‰ Implement reversing an array

๐Ÿ‘‰ Return the reversed value

Now, this looks like a task list. Following such an approach will definitely get us to the result we seek. This task looks small but its same procedure for a bigger problem. The previous two steps are the most important of the problem solving steps.

Nevertheless, assuming you were not able to carry out any of the previous steps, then comes the third step which is research.

  • Research:

Don't be afraid to do as much research as you have to. It is recommended to always first try to solve a sub-problem using your own coding abilities but if you're constantly hitting a wall and cannot move on, then there is absolutely nothing wrong in searching Google. Check Stack Overflow, Mozilla Developer Network JavaScript documentation or other developer blog for a solution. But do make sure to understand the logic behind the code instead of just copying and pasting codes. Copying and pasting codes will land you into more problems (I am a witness ๐Ÿคฆโ€โ™€๏ธ).

Google search Examples

๐Ÿ‘‰ How to check if a value is a string in JavaScript

๐Ÿ‘‰ How to check if a value is a number in JavaScript

๐Ÿ‘‰ How to check if a value is an array in JavaScript

๐Ÿ‘‰ How to reverse a string in JavaScript

๐Ÿ‘‰ How to reverse a number in JavaScript

๐Ÿ‘‰ How to reverse an array in JavaScript

  • Write pseudo-code for bigger problems:

This fourth step especially comes to play when its a bigger problem. This is an informal description of the actual code that we want to write. Its like code for humans to understand and not computers. For our example the pseudo-code may look like the following:

Function reverse(value)
if value type is not string and not number and not an array 
return value;

if type of value == string
reverse string
if type of value == number
reverse number
if type of value == array
reverse array
return reversed value

Already, you can see this is not JavaScript, as such, there is no hard rule to writing pseudo-codes. You just write it the way you and probably, other people in your team can understand what you have written.

From this pseudo-code now, you can clearly see that writing the function will be an easy one.

Note: Following these steps does not necessarily mean you will not run into bugs. You may still have bugs but debugging equally becomes easier because at each stage, you know exactly what the code is supposed to be doing and then you can fix the problem.

Conclusion

Develop a genuine passion and curiosity for knowing how things actually work in everything around you and this will help you get better at programming too. The four steps programming problem solving skills is one that you will learn from constant practice. Practice makes perfect as its said. I hope you find this article helpful as I did. Comments and reactions will be appreciated. Thanks for reading.