How Do You Approach Your Coding Problems

twitter logo github logo Updated on ・1 min read

Whenever I got stuck in codes, my first response is googling or ask for help. I'd like to improve my problem solving skills.

This example below is from freeCodeCamp, but you're welcome to use any codes, problems, and programming languages.


function convertToF(celsius) {
  let fahrenheit;
  return fahrenheit;
}

convertToF(30);
twitter logo DISCUSS (11)
markdown guide
 

The trick is to break it down into small steps.

Step 1: Understand the problem.
What is it exactly you're trying to accomplish?

In the example above, it's converting a temperature from celcius to farenheit.
There's a formula for that. Google time.

Step 2: Draw out what has to be done.

Literally, get a pen and paper and draw a picture or write down the steps.

This helps clarify your thinking and keeps you focused. Focus and clarity are two very important things when working through a problem.
Yeah, a rubber duck helps. ;-)

In this case,

  • create a function
  • get the celcius number input
  • apply the conversion formula
  • return the results.

As you're thinking it through, you realize you need to make sure it's a number, not a string to do the math etc.
Note that, but don't get caught up in these details yet.

Step 3: Start writing the code step by step you outlined.

Get a basic working model. Don't worry about the 'best way to code it' just get it done!

Write it so it works with simple inputs.

Now you have something that works!

Step 4: Add the polish.
Ok, now make your code more elegant. Clean up the var names, etc.

Test again.

Step 5: Idiot proof it.
What if it's a string instead of a number, or a null etc.
Lock it down.

I find this method works for problem solving. Break it down into small pieces, be clear about what you're accomplishing and take it step by step.

 

Awesome answer!

I would add one thing: as part of step 1, ask yourself "Do I really need to solve this problem?" There could be any number of reasons for the answer to be No. For example, sometimes the requirements may be wrong ("We don't actually need the temperature in Fahrenheit"). Other times someone else -- a colleague, a third-party library -- will have done the work for you.

I've been there -- wasting time and brain power on a problem that didn't need solving. It's painful.

Of course, if you're solving the problem just to learn or have fun ... well, completely disregard this part, and move on to Lisa's steps.

 

Fun is an important part of this, otherwise what's the point? ;-)
... mind you, sometimes you're having fun, you just don't know it yet.

True: check to see if the problem has been solved elsewhere, although beware of falling into a black hole of searching for and implementing solutions that would be easier to just code yourself.

Absolutely, there's a balance to be found. That black hole is a very real danger. Still, I tend to err on the side of not writing the code if I can avoid it ... which probably makes me lazy and/or lacking in curiosity.

Fun is an important part of this, otherwise what's the point? ;-)

So true!

 

What a wonderful answer!

A lot of time, people keep telling beginners to break big problems into small ones and I'm already doing this. But what I am lacking is how to draw plans to solve problems. In other words, I have tactics, but no strategy.

Now that I'm aware how to approach problems, it's just a matter of being patience with time and be perseverance.

 

Because I do low level programming these days, it is very hard to get results in Google, so I just stare at my code, hoping that it makes sence. When what I am doing is very new, I ask a question in stack exchange.

But most of the times, it all comes down to giving time.

 

I know how you feel. Just put the problem aside, do something else which is non-programming and ultimately you may get ideas to solve that problem.

 
 

A Java programmer's approach:

Understand the problem:

  1. Make a function to convert Celcius to Fahrenheit.
  2. The function argument may be a floating-point number.
  3. The function should return a precise answer.

Code the problem:

  1. Use proper function and variable names.
  2. My function takes an argument which is of type double because even if I pass it any other numerical primitive data type, it will be upcasted to double, by the compiler.
  3. Then comes the logic which solves the problem. Try to be concise and optimize the logic if possible. Why write more, if I can get the problem solved in lesser code.
  4. My function returns a double because it gives me a more precise answer.
public class Demo {
    public static double toFahrenhite(double val) {
        return val*1.8+32;
    }
    public static void main(String[] args) {
        System.out.println(toFahrenhite(26));
    }
}

Output:

78.80000000000001

Know the limitations of your solution:

  1. My code can only handle numbers.
  2. My code can't handle exceptions.
 

Interesting question, but there is not much coding/problem solving required for that example. You just need (to google :)) the formula and insert it into to your function.

 

Trying to ask for help when you really dont know how to solve the problem. Sometimes people ask for help, "why my code dont work" and in the end Its just a typo on code.
This example It's hard not to google unless you know the Formula.
More you practice more you'll know how to approach the coding problems, It just take time and you'll learn it naturally, don't worry.

An example, you have to do a Filters for a list of items with pagination. There are a lot of different ways to build this.

Try to split the problem in small problems, sometimes people know If's and else, for, objects, arrays and they can't build something more complex despite this complex thing have everything they know.

Let's see I need to build filters page with a list and a pagination, in this case we dont have server side so this is how I approach:

I create an object with all information about my list and render on DOM.
I create filters, lets think about categories, we have Math, History and English(It's our filters).
I need to know what items on our list belongs to each category, on my object I already have this information, on my filters maybe I have a data attribute that I can match with my the item Category. If I click on Math I'll check for every Item on my list that matches with this category and then It's just show this items and hide the others.

After you finish, in 6 months you'll look through your code and you'll think you could do it in another way. Will always be like this.

There are many ways to build this, and if you have server side will be more.

This is an example of how I approach a problem

Classic DEV Post from Sep 27

What was your win this week?

Got to all your meetings on time? Started a new project? Fixed a tricky bug?

Mohammed Asker profile image
Front-end developer newbie documenting my journey in coding • Anime fan • Bookworm • Gamer • #100DaysOfCode challenger