đ§Â Stuck On Code? Hereâs How To Unblock Yourself
Cherlock Code đ
Posted on June 22, 2023
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
Posted on June 22, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.