How to think Code? – A Guide to problem-solving with Code
By: Charlessia Robinson, October 2020
Coding is 10% working and 90% thinking. Truthfully, it can be 90% understanding why the code is not working. In other words, you are just staring at the screen as if you have been bamboozled. That unpredictability might keep experienced developers on their toes, but for beginners this could be a breaking point. Before you feel the pressure to quit, I would suggest considering the ways in which your approaching code.
Coding is no different than cooking up dinner for the holidays, organizing and categorizing your pantry to losing weight and running a marathon. All these tasks may seem too big and complex to take on. However, once you begin to break the problem down into steps, it gets easier and less tedious to solve. It is all about building a system that is functional and maintainable for you.
In the coding world, we call this incremental development which intends to avoid debugging by adding and testing only a small amount of code at a time (1). Unsurprisingly, the more lines of code you write, the more lines of code you will need to debug. Therefore, its important to write, test and execute. So, from experience, I would make this a habit. It is easy to get defeated when your code is not working or behaving properly. However, errors in your code allow you to see its flaws. It is better to catch them now, before someone else does.
There are different types of errors that you may come across with buggy code. Identifying the source can save you time and confusion. They normally fall into the following three categories.
Syntax errors
These are errors that occur when you violate the rules of writing in that specific language. This compiler error indicates something that must be fixed before the code can be compiled (2). These are the most common errors that beginners see first like a missing semicolon or bracket.
Runtime errors
Errors which occur during program execution after successful compilation (2). Starting out, I would not stress over this. Your programs will be simple enough where you will not run into this issue just yet.
Semantic errors
This error occurs when the statements written in the program are not meaningful to the compiler (2). Therefore, this is more difficult to see. So, as a programmer, you must make a mental note of what the code should intentionally be doing.
Tips for Debugging
Look behind you
For beginners, it’s hard to pinpoint where the errors are coming from. The error might not be happening on the line that your developer tool is suggesting. It could be occurring in the code above you. For instance, you could have mistyped or forgot to add proper headings to your document.
Look in front of you
Are you experiencing problems in the code below? Did you forget to add a return statement or a closing tag? Whichever one may be the case, it is important to separate the code out into sections. You should always divide and conquer.
Undo and start over
If I know that I have gotten ahead of myself, I like to start over. It gives me a clean slate to think about my code and refocus. Although, you should always save the code previously worked on somewhere else. So, if you must go back to it, you will have it.
Write and test line by line
Coding requires a great deal of precision and patience. Yes, you wrote the same code for the thousandth time. However, you are not immune from the same errors occurring for the thousandth time. Your mind could be focused on the next line of code, errands you need to run, or what you want for lunch.
Take a deep breath
Its okay to take a break and step away from the computer. Sometimes, its easy to experience programmer burnout. Remember, programming is not a sprint, but a marathon. Work at your own pace and you will get there.
Take time to refresh your memory
Do not beat yourself up if you do not know what is wrong with your code. Its fine to look things up. Always consider that you are probably not the first programmer to have this issue.
Do not get too attached to your code
With code, there are multiple ways to solve a problem. I know how easy it is to get caught up solving a problem a certain way. It can be difficult to let go of your original code and implement another solution. However, it is important to explore your options. This is how you make progress and strengthen your skills.
Follow coding conventions
Following programming conventions has made debugging code more efficient for developers. This could be creating separate files, adding classes, indenting space to using identifiers like id tags to variables and functions. Separating and defining your code allows for code reuse. You can fix buggy code for multiple files without going line by line.
(1) Downey, Allen B., Think Python. O’Reilly Media Inc., 2016.
(2) “Errors in C++”, Geeks for Geeks, https://www.geeksforgeeks.org/errors-in-cc/ Accessed 5 October 2020.
Source: Images
- Piacquadio, Andrea. “Woman in Red T-Shirt Looking at Her Laptop.” https://www.pexels.com/. 17 February 2020. https://www.pexels.com/photo/woman-in-red-t-shirt-looking-at-her-laptop-3755761/. Accessed 10 October 2020.
- Mentatdgt. “Woman posing. Wearing White Dress Shirt Sitting on Window.” https://www.pexels.com/. 2018 March 13. https://www.pexels.com/photo/woman-posing-wearing-white-dress-shirt-sitting-on-window-937453/. Accessed 10 October 2020.
Pixabay. “Turned-on Computer Monitor Displaying Text.” https://www.pexels.com/. 2016 December 23. https://www.pexels.com/photo/business-code-coding-computer-270360/. Accessed 10 October 2020.
Nice post!
I appreciate you guiding me through some advice, tips, and tricks about coding. A lot of us can find coding to be frustrating and overwhelming sometimes. However, once I crack the code, I will straight up happy dance.
I have messed up so many coding attempts, but you are right, this takes time. I can’t expect to always be right, but I should think about all of the steps that I put in, and above all, I should have the original code stored somewhere before I even start tinkering with it. I think that is some phenomenal advice!
At my old job, I would have a cheat sheet that I would refer to a lot. The sheet had some coding commands on it that would help me to do some of the things on our website. The more that I would practice and use those codes, the better I would remember them. They became embedded in my memory.
I spoke to my brother-in-law about what he experiences as a coder over the weekend. I found myself curious about what it is like in the shoes of a coder in real life after doing some work in Codecademy. He basically described his current project as a puzzle. Sometimes things work. Sometimes they don’t. His mission is to find the missing pieces and to complete the puzzle. I just had to share that little story with you because I feel like it supports your 10% working and 90% thinking.
Man, more power to you guys! Mad respect to people who code for a living. 🙂
Great post!
I really like how you have break down everything in a category and explain it really well. It will definitely going to help a lot. For me, I am not a very big fan of coding or I should say that I run away from it. When you make a mistake while you are coding and you mess the tiniest thing up then it just become so hard to find out. I know you can’t be right all that time but it just frustrates me. I personally like more of a designing part because I like to decorate/ redesign the site. I think being a designer is more fun because you get to design the site and for those people who likes to be a designer. At the beginning of my major I used to code a lot and I really enjoyed it at that time, but as I transition to the designing part of the program (totally forgot about the coding) it changed my mind. I really enjoy doing project where I get to design a certain thing. But I have all my respect for people who code you guys are doing great job!
I like how you normalize the struggles of coding, remind everyone that it is ok, and provide encouragement to not give up. I think it’s easy to become discouraged and think that you are the only one not understanding coding or feel like you are making too many mistakes. I find myself getting discouraged sometimes when I feel like I’m not comprehending something fast enough. However, your post reminds me that learning is a process and concepts like coding are hard for anyone. Literally laughed out loud at your quote “90% of coding is figuring out why it’s working.” I feel like my life is sometimes like that, lol!
Thank you for this blog. It is a nice encouragement for those, like myself, who are new to coding. I appreciate how you advise to keep saved copies of code. For writers, whether code or content, nothing is more devastating than losing your work. It only makes sense to keep copies of codes. I also appreciate the advice to of organizing and categorizing tasks. I think that is an important mindset to writing and I’m still learning to apply it to writing code. Well done!
I am throughly obsessed with this post. I 100% struggle with coding, and I found this post so relatable. I’m often stuck staring at my screen willing my brain to understand what it is I’m looking at. As a beginner, this makes me believe that coding isn’t for me, and that, as soon as this class is over, I’ll be done with it. I have a hard time focusing in on the smaller, less-complex problems that turn into the huge problems you can’t unsee. Hopefully I can take this into codecademy and really put some work in this week! Great post!