DEV Community

Cover image for 🧊 Stuck On Code? Here’s How To Unblock Yourself
Cherlock Code 🔎
Cherlock Code 🔎

Posted on • Updated on • Originally published at evergrowingdev.substack.com

🧊 Stuck On Code? Here’s How To Unblock Yourself

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.

Let’s grow together 🌱

And stay in touch on 🐦 @evergrowingdev

Top comments (7)

Collapse
 
brittanyhurthead profile image
brittanyhurthead

Love your article. As a newbie, I am learning this early. I walk away for a little while if I get stuck. I ask for help from more experienced developers. I use all the online resources. Most times, walking away to clear my mind helps me push through. I always come back, which tells me I am meant to pursue becoming a developer.

Collapse
 
evergrowingdev profile image
Cherlock Code 🔎

Thank you! That's fantastic, sounds like you're already doing all the right things and on your way to becoming a great developer. I wish you all the best on your journey! 💫

Collapse
 
sehgalspandan profile image
Spandan Sehgal

Nice article, I face it many times! #devlife 🤣

But I would like to add one more point:
Relax your mind first 😌 ✨️

Collapse
 
evergrowingdev profile image
Cherlock Code 🔎

Thank you haha! Ah yes great point, a relaxed mind always helps 😊

Collapse
 
johner97 profile image
Johner97

This post is really great and very useful

Collapse
 
evergrowingdev profile image
Cherlock Code 🔎

Thank you, I'm glad you found it useful 😃

Collapse
 
vsaulis profile image
Vladas Saulis

I thought "read the doc" must be on the first place...