Debugging is a key skill – spend time learning how to debug

All too often I see new developer focusing on learning how to write code, but not spending as much time learning how to debug code. Knowing approaches to help you find why code is not working is just as important as knowing how to write the code in the first place.

There a too many questions posted online that look like this:

“I’ve written this code but it’s not working and I don’t know why”

My first question is always “have you stepped through the code to understand what the code is doing?”. Many of these types of question and issues can easily be resolved just by using a debugger, stepping through the code and validating that each step does what you expect. As you are learning development skills, a lot of issues in code are from assuming the code is doing one thing but the way you’ve implemented it you have a subtle difference somewhere where you didn’t initialize something, you referred to the wrong variable, or the logic in an if statement is incorrect. Stepping through the code will help you find the point where everything’s working up to the point where there’s an issue.

Spend time learning how to use your debugger in your IDE of choice. There are of course many other complementary techniques to debugging code, like logging and trace statement, unit testing etc, but don’t overlook the usefulness of your debugger.

[Top tip series: rather than spending time writing lengthy articles over several days I’m going to try and post shorter, hopefully useful tips more frequently]

Effective Java: recommended 18 years ago, still top of my recommended reading list today

In past years I used to put together a recommended reading list for software developers, particularly Java developers. I think I stopped doing it as often because year to year my list really didn’t change much, if at all.

As an example, 18 years ago, Effective Java by Josh Bloch I would say was required reading for all Java developers, new and experienced. It would still be at the top of my list today. It’s essential reading and solid guidance for all Java developers. If you’re a Java developer and you haven’t read this book yet, pick up a copy and read it now.

As an industry we do a poor job of teaching new developers how to develop software

This is not a criticism of any specific school course, bootcamp or on the job training, it’s merely an observation over the years: early Spring and late Autumn/Fall there is always an increase online of very basic developer questions. These questions always include things like: “why doesn’t my code work”, “can someone debug this for me”, “why does this code not produce the result I’m looking for”, and even the flatout “please help me with my homework – here’s the assignment”

These points in time coincide with the start of a new term as new students are starting to learn a programming language, I guess it’s pretty much expected that we would see the number of these types of question increase during these times of the year. My point though is that these types of questions appear again and again every year. Every. Year.

Why have we not progressed past the point where new developers don’t need to ask basic questions as they are starting out? Why are software development courses not equipping new developers with basic problem solving skills to help them trouble solve basic problems for themselves?

Most of these questions and problems could easily be solved by the developers themselves if they stepped through their code in a debugger in their IDE and looked at what happens in their code step by step up to the point where something goes wrong usually because of a simple logic error.

Other problems could easily be solved by applying basic problem solving and software development core techniques:

  • breaking a large problem down into smaller parts
  • writing unit tests to test each smaller part of the app in isolation to confirm each part works by itself
  • testing with a range of valid and invalid values to find what works and what doesn’t work
  • using log statements to trace execution through an app
  • identifying what changed since it was last working, incrementally backing out last changes to find the point where a change was added that broke something
  • using a debugger

There’s too much emphasis on learning a programming language to write code, and not enough focus on techniques to develop software. None of things I listed above are new ideas or concepts, experienced developers have applied these concepts daily for decades. It seems we’re failing short in training new developers if we’re missing out the basics.

Fix the problems you have, not what you don’t have

It’s easy for new developers to get caught up spending time on things that really don’t matter. Spending time trying to work out whether approach A is using more or less memory than approach B is a common distraction, and usually the answer to whether it does or not is irrelevant (why would you be concerned if memory for a variable is stored on the heap or the stack, and try to do do ‘weird things’ to reduce memory usage).

It’s important to know if you have a problem or not, and then spend time investigating and resolving that specific problem. Don’t spend time worrying about things that are not important or irrelevant. Knowing what is important often comes with experience, but if you’re unsure then ask one of your more experienced collegues.

[Top tip series: rather than spending time writing lengthy articles over several days I’m going to try and post shorter, hopefully useful tips more frequently]