DEV Community

Cover image for How to solve a programming problem
Jhon-Idrovo
Jhon-Idrovo

Posted on

How to solve a programming problem

Since programming is at its core problem-solving, we need to have a good ability to do exactly that. Here I'm going to walk step by step through an approach that can help you to become better at that valuable skill.

Define the real problem

There's no worst thing than having spent hours solving the wrong problem. That's why first you need to define the problem. That sounds trivial, but it's fairly easy to follow a rabbit hole along the way and end up trying to solve a specificity that has almost nothing to do with the initial problem.

To explain this part better think about this problem that I had to solve: We need to generate random colors that can be assigned to categories so that it helps users identify them easily.

In the example case, I ended up working with binary operations to generate the colors and an external package to test if the colors were different enough. Yes, it was working (kinda) but with an overly complex system.
There is an easier way! Now let's find the root problem and focus on it.

One of the best approaches I know for doing that is to ask yourself why the problem needs to be solved multiple times:

  • "Why do I need an algorithm to generate random colors?"
  • Because you need random, different enough colors.
  • "Why do I need to generate those colors?"
  • To use them as a distinction on each category.

Notice how after the questions there's no binary algorithm implicated, we only need a bunch of different colors. So, why not generate them by ourselves? We can create a list of predefined colors from which we can choose one randomly, and if they're all different enough we don't even need to test for that. We don’t need the package either!

Gather information

You don't want to make a decision and later change your opinion because there was something you didn't know. This isn't always possible, but the better you research, the less likely you're to have this problem.
You can use Google, documentation, blogs, asking other people (especially if you're a beginner), and any other source of information. Find what other people have done, and how it turned out for them.
At the end of this step, you need to have a list of options with their pros and cons.

Compare

Once you have a good understanding of the topic compare the options and pick the one you think it's the best. It doesn’t matter if you aren't 100% sure. That's why the next step exists.

Prepare for failing

You can't expect all to go exactly as you imagined. Especially in the complex world of software. So, prepare for things not working. When that happens, update the pros and cons of the options. Maybe the one you've chosen has some problems you didn't expect.

But don’t throw the towel immediately! Try to make things work with the option you've taken, learn more about it. Sometimes it's just a lack of knowledge.
But, if after all you still have problems implementing it and the cons of the current selection are becoming more than any of the other options plus the cost of changing decision. You can experiment with other options.

Wrapping thins up

That's all for this time, I hope you find it interesting and can help you in some way. If so, you can follow me to don't miss this type of posts @JhonIdrovo_D. Also if you want to say hi, just send a DM.

Top comments (0)