Practical tips for what to do when you get stuck when coding.
If thereās one thing thatās guaranteed in any programmerās journey itās the fact that you WILL get stuck.
Thereās no way to avoid it.
There isnāt a single developer that hasnāt been stuck on a coding problem at some point.
Solving problems is the aim of the game, itās part of the job description and itās what we do best.
If youāre learning to code, then getting stuck and overcoming those problems plays a massive part in becoming a successful developer.
A problem solved today is a problem halved tomorrow.
Getting stuck doesnāt mean youāre stupid, or youāre not cut out for programming. Thereās always a solution, but the challenge lies in how and where to find it.
With so many bugs and errors you could possibly encounter, it would be impossible for me to describe how to fix each issue separately.
But what I can do is give you some tips on how to unblock yourself no matter the issue.
In this article, weāll look at some techniques for unblocking yourself and knowing what to do when you get stuck.
So before you start to panic or get frustrated, try these things first:
1) Embrace the problem š¤
If youāre faced with a coding challenge, itās easy for those feelings of being overwhelmed or annoyed to creep in.
However, the first step to unblocking yourself as a programmer is to embrace the problem and see it as a valuable learning experience.
It requires a mindset shift, where you view each challenge not as a roadblock, but as a stepping stone toward growth and mastery.
Adopting a growth mindset is key.
Instead of seeing challenges as these great big hurdles, see them as opportunities for personal and professional development.
Understand that every bug, every error message, and every roadblock holds a lesson waiting to be learned.
By approaching coding challenges with a growth mindset, you open yourself up to new possibilities and empower yourself to tackle even the most complex problems.
Once you get your mind right, and settle your thoughts youāll then find yourself more open to seeking the solution.
2) Break it down šØ
So now you know youāve got a problem, youāve embraced that it is what is, and now youāre ready to find a way to solve it.
No matter how big or small the issue may be, it is always best to try and see how you can break it down into smaller, more manageable chunks.
This approach not only simplifies the problem at hand but also makes it easier to troubleshoot your issue moving forward.
To effectively break down problems, start by analysing the issue. For example, if itās a bug in your code, identify the key components or functions involved.
Then, think about the logical steps needed to achieve the desired outcome. Think about the input and output. Say to yourself if I input X then I want my code to output Y. And if the output is not what youāre expecting, you can then try to determine what is stopping it from working.
As good practice try to organise your code in a structured manner, by dividing your code into smaller functions or modules.
That way you can encapsulate specific functionalities and make them more manageable.
This modular approach allows for easier debugging, maintenance, and code reuse over time.
3) Try to debug it šµļø
In programming, the most common problem youāre likely to get stuck on is a bug.
And just like in nature bugs can be invasive and annoying.
There are several debugging techniques you could use to try and help you find a solution to your issue.
Letās look at a few key ones:
1. Print Statements
This involves strategically placing print statements (or console logs) within the code. By inserting print statements at critical points, you can output variable values, execution flow, and other pertinent information.
This allows for real-time insights into the program's behaviour and helps to identify problematic areas.
For example, when encountering unexpected outputs from a function, inserting print statements at different stages can provide visibility into variable values and help pinpoint the source of the issue.
2. Debugging Tools
Integrated Development Environments (IDEs), code editors and other development tools offer advanced debugging features that enhance the efficiency and accuracy of the debugging process.
These tools provide capabilities such as breakpoints, step-by-step execution, and variable inspection, enabling you to closely examine code execution and diagnose issues at a granular level.
For example, when faced with a runtime error that is difficult to reproduce, setting a breakpoint at the suspected line of code will allow you to pause execution, inspect variable values, and analyse the program's state.
By stepping through the code, you can identify the precise location where the error occurs.
3. Error Handling
When code lacks error handling, it is prone to crashing when encountering bugs. However, with error handling in place, errors are intercepted before they can disrupt the application.
The error is then processed, such as through logging or storing it in a database, allowing the application to continue running smoothly.
This is especially effective in identifying bugs in a production environment. By collecting errors as they occur, you can analyse them later to determine the underlying bug causing the errors.
4. Commenting out code
One straightforward yet useful debugging method is to "comment out" sections of code.
By converting functional code into comments, those lines are rendered inactive and not executed. The process starts by commenting out code in the specific part of the application where the problem is suspected. Gradually, each line of code is reintroduced one at a time, then you can monitor for the reappearance of the bug.
This technique allows you to isolate the problematic code section, go through it line by line and hopefully identify where the bug is occurring.
Debugging will take a while to practise and master, but over time youāll be able to quickly use and combine all of these techniques, which will help you to solve your issues much faster.
4) Try Rubber Duck Debugging š„
Tell your problems to a Rubber Duck š¦.
No this isnāt a joke, and I kid you not.
Rubber duck debugging is an unconventional yet remarkably effective problem-solving technique.
The principle behind rubber duck debugging stems from the idea that verbalising a problem forces you to articulate and examine it from different angles.
The concept of explaining code problems to an inanimate object, such as a rubber duck, can work wonders in decoding complex issues.
By explaining the code step by step to an attentive rubber duck listener, you're essentially talking through the problem aloud.
This process helps to organise your thoughts, highlight assumptions, and identify gaps in your understanding. You then may find yourself naturally scrutinising the logic behind each line of code.
Rubber duck debugging provides a fresh perspective and serves as a non-judgmental sounding board for developers.
Thereās no shame in talking to yourself (or a rubber duck).
5) Read the Docs š
This one may sound obvious, but youāll be surprised just how easy it is to forget that most programming languages, frameworks and libraries have official documentation available to help support you.
These docs may seem lengthy and in-depth, but thatās the whole point.
They are written to give you insight into how things work, how to use certain methods, components or blocks of code etc. And they may even contain troubleshooting information.
Now we even have many community-contributed documentation, where people have added more information to support the official docs.
Chances are a lot of the time, that the issue youāre facing has been captured and written about somewhere.
Also, by reading the documentation you may also discover techniques for making your code more robust, and less bug-prone.
Sometimes the easiest problems are solved simply because we werenāt implementing or using something correctly. Docs are a useful way to help with that.
A good tip is if youāre using something in your code that youāre unfamiliar with, read the documentation about it thoroughly first.
That way you can help avoid making simple mistakes that turn into big frustrating problems later.
6) Seek Online Help š
Many of the techniques I mentioned above may already involve doing this, but I thought it was still important to highlight the biggest problem-solving tool youāll have access to - the Internet.
Thereās a reason most developers will often joke and say that 80% of their time is spent Googling.
And thatās simply because Google and the rest of the online world is the most comprehensive place to find solutions to programming problems.
If you have an error message, all you have to do is Google it, and youāre very likely to find information written by others.
Online communities, such as programming forums (like StackOverflow), developer communities (like Dev.to & Hashnode), and open-source project spaces (like GitHub), provide excellent platforms for seeking help and engaging with other developers.
These communities are full of passionate programmers who are eager to share their knowledge, offer guidance, and collaborate on projects.
By actively participating in these communities, you not only gain access to a wealth of expertise but also build a network of supportive peers who can provide encouragement and motivation during challenging times.
7) Take a step back š¶āāļø
If youāve tried all of the above and youāre still stuck or frustrated, itās time to take a step back.
Sometimes weāre so focused on getting something to work that we lose track of time and ourselves.
In our quest to solve our problem or debug our code, we get into tunnel-vision mode and this can actually hinder our thought process and capabilities.
If youāve already spent a significant amount of time on a problem, the best course of action is to temporarily step away from the code and allow yourself to gain some fresh perspective.
Stepping away from the screen and shifting your focus to other activities can have remarkable effects on problem-solving.
One of the main benefits of taking breaks is the opportunity for your mind to relax and reset.
Doing activities unrelated to coding helps you detach from the problem at hand and gives your brain a chance to process information subconsciously.
This mental rest often leads to breakthroughs and "aha" moments when you least expect them.
I canāt tell you how many times Iāve found a solution to a bug whilst I was casually going about my day and distracting myself from solely focusing on my problem.
Stepping away doesnāt mean giving up, it means youāre giving your brain and eyes a well-deserved rest, so you can come back more energised and empowered than ever!
Conclusion
To conclude, as weāve recognised getting stuck is not a matter of "if," but rather "when."
Adopting a growth mindset and viewing setbacks as valuable lessons enables you to transform frustration into motivation.
Breaking down complex problems into manageable chunks enhances troubleshooting and problem-solving capabilities.
Practical debugging techniques, such as print statements, logging, debugging tools, error handling, and commenting out code, helps you to efficiently identify and resolve issues, ensuring smooth code operation.
Exploring the concept of rubber duck debugging, where explaining code problems to an inanimate object like a rubber duck brings clarity and reveals solutions, opens up new perspectives.
Reading documentation, and online resources provides access to a wealth of expertise and alternative approaches from the main source and others.
So the next time you get stuck, remember it wonāt be the last time, but hopefully, these steps can help you get through it and you can unblock your way to victory!
From your fellow ever-growing dev,
Cherlock Code
š If you liked this article...
I publish a weekly newsletter to a community of ever-growing developers, seeking to improve programming skills and stay on a journey of continuous self-improvement. Focusing on tips for powering up your programming productivity š.
Get more articles like this straight to your inbox.
And stay in touch on š¦ @evergrowingdev