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.
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:
- comment everything (variables, loops, etc)
- 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.