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

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.