What to do when met with a tough coding problem

Let's explore what options are available when we're stuck while coding

By: Ajdin Imsirovic 26 December 2020

In this article we’ll count down some tips to get unstuck when coding.

What to do when stuck trying to solve a coding task Image by Unsplash

18. Understand the problem

Most developers, when given a new task, do the following:

  • Start coding right away
  • Get stuck
  • Research solutions
  • Research more solutions
  • Write code
  • Delete all code
  • Re-read the problem
  • Ask for assistance

Instead, you should:

  • Read the problem
  • Read the problem again
  • Once you understand the problem, write it down in your own words
  • Research solutions
  • Write code
  • Ask for assistance
  • Refactor / optimize the code

17. Check your text values for capitalization (aka “validate the values in your app”)

This is a silly one that I was faced with recently. I’ve written my code, but it wasn’t working. Tried a different approach, and that wasn’t working either. I’ve spent about an hour trying to understand why this was happening, and in the end, it turned out that I was checking:

if (varName = 'yes') { // ...

But I should have checked the following:

if (varName = 'Yes') { // ...

So, I failed to write more robust code, such as:

if (varName = 'Yes'.toLocaleLowerCase()) { // ...

Because I failed to “write code defensively” - i.e with all the precautions that I could think of - it cost me about 60 minutes of unnecessary debugging.

Long story short: If you believe your code should be working, but it “magically” isn’t - validate the values you’re checking against.

16. Relax

It won’t help thinking it’s the end of the world and having a breakdown over it.

Understand that it’s normal to get stuck, even for seasoned developers. In fact, getting stuck and dealing with errors and bugs is an everyday occurrence. It comes with the job.

So take a deep breath, calm down, and take it easy, slowly, one step at a time.

15. Make a screen recording of you trying to solve the issue

Record the screen of what you’re doing and what’s happening. Then re-watch it. Or send it to a more skilled programmer for some advice and guidance.

14. Check the spelling

Very often, it’s a comma where it should’ve been a semicolon, or a single letter in a variable name where it should’ve been two letters, or a square bracket instead of a curly…

Go through the code and check your spelling.

13. Try rubber duck debugging

Rubber duck debugging is the approach where you explain your problem to someone else, be it to a colleague on a whiteboard in an office next to yours, or a rubber duck on your desk.

Sometimes, explaining the problem out loud in a step-by-step fashion is enough to solve it.

12. Search the codebase for a similar piece of code

If you’re working on a project, you might “get away with copy-pasting”. Simply search the project for similar solutions to a problem you’re working on, and if they’re close enough, you might get away with copy-pasting and possibly adding only some slight improvements.

11. Split the problem into smaller problems

Once I had a huge php file that produced 1000 lines of HTML.

Instead of trying to untwine the huge file’s php and html (which were “glued together”), I built a prototype of the resulting html with html and css only, and then I implemented it in the original file.

Sometimes all you need to do is simplify the problem you’re working with instead of attacking it straight on.

10. Limit the number of tries

If you’re working in a team, and the task is small, you should limit the attempt to fix it to, for example:

  • 3 tries
  • each try should take maximum of 30 minutes

That way, you know that you’ll invest 90 minutes of your workday on trying to fix this task before you can ask a co-worker for help.

For more difficult tasks, you should increase the time and the number of tries.

9. Read the error message and understand what it complains about

Newbie coders often panic when they see an error message, and immediately ask for help.

A much better approach is to read it slowly and fully comprehend what is happening.

Do you know which line is causing the error?

Do you understand what the error says?

Read the code again.

Read the error again.

Google for the error.

Sometimes, doing these few things is enough to get unstuck.

8. Improve the readability of your code for yourself (and others)

Go back to your code, and:

  1. comment everything (variables, loops, etc)
  2. read the code line by line and make sure you understand what’s happening

At a certain step of the above-described process, you might find the cause of the error or understand in general why your code doesn’t work.

7. Print out the values that your progam is working with

Sometimes it’s simply a matter of logging out the values of the used variables to the screen, and following the execution step by step.

How are the printed values different from the expected behavior? Your solution might show up in the difference between the actual value and the expected value.

6. Get back to basics (simplify your code)

Copy the snippet on code that you’re working on or save it safely in some other way (think git and branching).

Now start deleting or commenting out the “redundant” parts of the code, while making sure the app still works. Once you’ve removed all but the essential code, you can start re-introducing the initial complexity back into the code, until you get the error back too.

This might show you the reason why this error is occuring and help you fix it.

5. Get some rest

Maybe you’ve worked for a long time and you’re tired and not thinking straight. Sometimes having some time away from keyboard is the best strategy. Give yourself time to relax and not think about the code. Once you’re back, it could happen you’ll find the solution immediately.

4. Find a friend to look at your code

Preferably, this friend should be more senior than you, but even a pair programming session with you equal can often lead to a solution of the issue.

3. Read the docs carefully

Go back and read the docs for each language construct and method used in the code. Go step by step. Take your time.

It’s possible that you are not invoking the methods properly, or with the expected parameters.

2. Ask a question in an online forum

Go to stackoverflow and try to find your answer there. If that doesn’t help, ask you question and wait for responses.

1. Write test cases

Start by writing the easiest possible test case you can come up with and continue with other gradually more advanced ones.

Save your test cases to test files so if a bug appears again you can save time by running them against the new bug.

Feel free to check out my work here: