7. 0111 - Debugging and Generative AI

Help Make These Materials Better!

I am actively working to complete and revise this eBook and the accompanying videos. Please consider using the following link to provide feedback and notify me of typos, mistakes, and suggestions for either the eBook or videos:

What’s the Point?

  • Distinguish between compile-time and runtime errors

  • Learn some strategies for debugging your code

  • Use the debugging tools available in your IDE

  • Understand the basics of generative AI in coding

  • Use a generative AI tool to generate source code


Source code examples from this chapter and associated videos are available on GitHub.


7.1. Debugging

A bug is an error in our code that causes it to behave in an unexpected way. This is different from a syntax error, like forgetting a semicolon or misspelling a keyword—​those kinds of mistakes will prevent our code from compiling, so we can’t even run it. A bug is when our code runs, but it doesn’t do what we want it to do.

Bugs are frustrating, but they are a part of programming. Like Thanos, they are inevitable. And like Thanos, we can use ludicrous time traveling to fix (decapitate?) our bugs. Well, we can’t do that last part, but we can fix them in the present; and hopefully, we can do it without too much frustration.

Finding and fixing bugs is called debugging, and without realizing it, we’ve been developing our own strategies for debugging since we started writing code. But as our code becomes more complex, we’ll benefit from a more systematic approach to debugging—​and we’ll want to take advantage of the tools available in our IDE to help us.

7.2. Types of Errors

We categorize bugs into two general types:

Compile-time errors

Errors that prevent the compiler from fully processing our source code. These are generally the result of incorrect syntax—​in other words, breaking the rules of the language.

Runtime errors

Errors in which our code compiles, but it does not execute as intended. Crashes are obvious runtime errors, but making an incorrect calculation is also an example of a runtime error.

Fixing compile-time errors is just a matter of looking over our code and correcting the mistake. That’s not always as easy as it sounds—​especially for beginners—​but at least the compiler and/or our IDE can give us feedback about what and where the mistake is.

Runtime errors can be especially frustrating, especially since we can’t always tell at what point the actual error is occurring. Did I make the mistake at the start of the program when I calculated the answer, is the mistake at the very end where I output it? Or did I do something in the middle that accidentally changed the result? Who knows! And if you’re like me, you might have done all three…​

Time To Watch!

Intro to Debugging

7.3. Debugging Tools in Our IDE

Most modern IDEs have built-in tools to help us debug our code. These tools allow us to monitor the variables in our program, step through our code line-by-line, and set breakpoints to pause our program at a specific point. The more we know about programming, the more useful these tools become. But even as beginners, these tools can be helpful in finding and fixing bugs in our code.

The most common debugging tools we’ll use are: Watch window:: A window that displays the values of variables in our program. Breakpoints:: A marker that tells our program to pause at a specific point in our code. Step Into:: A command that tells our program to execute the next line of code. Step Over:: A command that tells our program to execute the next line of code, but not to step into any methods that are called. Step Out:: A command that tells our program to execute the rest of the current method and then pause. Resume:: A command that tells our program to continue running until it hits the next breakpoint.

These functions are available in most IDEs, but the specifics of how they work can vary. Even if you’re not using Visual Studio Code, the concepts are the same, so you should be able to apply what you learn here to your IDE of choice.

Time To Watch!

Debugging in Java with VS Code

Files from video:

The first Lab Assignment in Canvas can be completed using what we’ve covered to this point. You might choose to complete that work now, then move onto the next section—​which you’ll need for the second Lab Assignment.

7.4. Generative AI in Coding

The AI content is under construction. Check back soon for updates!

Generative Artificial Intelligence is a type of AI that can generate new content based on existing data. Tools like ChatGPT and Copilot are well-known examples of generative AI. We can ask a generative AI tool to write song lyrics about Java programming, for example, and it will produce a new song for us.

Artificial Intelligence is a broad field, and the topic is well beyond the scope of this course. But generative AI has important implications for the field of software development, so we’ll focus on just that aspect of the technology.

7.4.1. The Role of Generative AI in Education

Like many teachers, I’m actively wrestling with the role of generative AI in a coding course. I have many ethical concerns about AI in general, and about generative AI to create code in particular. Simply put, these tools can be used in ways that are indisputably unethical. Most obviously, students can easily use these tools to generate code that is then submitted as the student’s own work.

I would like to think it goes without saying, but based on the number of students who deny this, I guess I need to say it: using AI to generate code that you submit as your own is cheating. It is no different than copying someone else’s code and submitting it as your own.

It presents a real challenge for teachers trying to assess student learning, and a real temptation for students struggling to learn—​and able to simply let AI do the work for them.

But the impact this technology has had—​and will continue to have—​on the field of software development is undeniable, and I would be doing you a disservice if we didn’t learn a little about it.

7.4.2. The Promise of Generative AI in Coding

What does this AI revolution look like for coders? What does it mean to someone learning to code? How does it change the job outlook for someone considering a career in software development?

The short answer is: I don’t know. The longer answer is: I don’t know, but I’m excited to find out.

For now, we can look at what generative AI can do for us today. Here are a few of the current and near-future applications of generative AI in coding:

  • Code completion: Many IDEs already have code completion features that suggest code as you type. Generative AI can take this a step further by suggesting entire lines of code, or even entire methods.

  • Code generation: Generative AI can generate code based on a description of what you want the code to do.

  • Code refactoring: Refactoring is the process of rewriting code without changing the task the code performs. Once we get code working, we can refactor it to make it more efficient, more secure, or more maintainable. In other words, we can make it better. AI can look at our existing code and recommend changes that make it better.

  • Debugging: Generative AI can help us find and fix bugs in our code—​often, before we even run it.

  • Documentation: Writing good documentation is an important part of software development, but many programmers hate doing it. Generative AI can help us write documentation that is clear, concise, and accurate.

And that’s just a few of the obvious applications of generative AI in coding.

What does that mean for the coding profession? All I can do is guess, but here are some things I hope AI does for us:

  • Better software: If AI can help us write better code, that should lead to better software.

  • Faster development and update cycles: Again, if AI makes us more efficient, we should be able to develop and update software faster.

  • Improved security: Hopefully, AI will help us identify and address security vulnerabilities in our code.

  • More time for high-level effort: If AI can take on some of the more tedious and boring coding tasks, that should free up mental bandwidth to focus on the more interesting and creative.

My most optimistic hope is that AI will free up programmers to focus on creative applications and problem-solving—​and ultimately allow us to create software that improves the world around us.

7.4.3. Potential Negative Impacts of AI on Coding

However, I worry that AI will lead to some negative impacts on the programming profession, as well. Again, I can only guess, but here are some things I fear might happen due to AI:

  • Decreased job opportunities: If AI can write code faster and more accurately than humans, that could lead to fewer job opportunities for human programmers. I think this will be especially true for junior programmers, whose workload will be most easily automated.

  • Loss of institutional knowledge: Every programming team relies on veteran coders who have been around and have a deep understanding of the codebase. When there’s a question about what a module does, or why an algorithm was implemented a certain way, those veterans are the ones who have the answers. If AI is generating code, we may lose that institutional knowledge.

  • Decreased quality of entry-level programmers: If AI can generate code for us, it’s possible that we’ll see a decrease in the quality of entry-level programmers. People who rely heavily on AI while learning to code may not develop the same problem-solving skills as those who learn to code without AI.

7.4.4. What Does That All Mean for You?

I don’t know. As excited as I am to see AI reach this tipping point in software development, it’s kind of a scary time to be a programming teacher. Until recently, I’ve always

Coders who rely heavily on artificial intelligence tools to solve problems may be able to pass themselves off as more skilled than they actually are—​indeed, they might even believe themselves to be more skilled than they actually are. But such coders will be less skilled at testing, debugging and maintaining code, and that could lead to a decrease in the quality of software. Putting code into production (releasing it in software that people actually use) without understanding it well enough to regiorously test it is a recipe for disaster.

I think there is still a bright future for programmers, but the landscape will be different. Coders will need to have a deeper understanding of the code they write, and they will need to be able to solve problems that AI can’t. They’ll need to be able to evaluate the code that AI generates, and they’ll need to be able to maintain and update that code. The role of software architect—​the person who designs the overall structure of a software project—​will become even more important, as will the role of the software tester.

Students will need to have the discipline to learn to code without relying on AI tools, and they’ll need to develop the problem-solving skills that AI can’t provide. Testing and debugging skills will be more important than ever, and those can really only be developed through practice—​and that practice comes from writing code, testing it to find errors, and fixing problems.

7.4.5. Using Generative AI Tools

Online tools like ChatGPT and Microsoft’s Copilot can help you generate code without any specialized software. Simply enter a description of what you want the code to do, and the AI will generate code for you. You can then copy and paste that code into your IDE and work with it from there.

But IDEs are also beginning to integrate generative AI tools focused on coding. For example, Visual Studio Code now includes a feature called GitHub Copilot that has been trained on billions of lines of code. The user interface provides a chat window where you can describe what you want the code to do, and Copilot will generate code for you. You can choose to accept that suggestions, or you can refine your prompt to get a different suggestion.

Additionally, Copilot can generate code as you type. It will analyze the code you’ve already written and suggest the next line of code. You can accept that suggestion, or you can ignore it and keep typing. It essentially acts as a very advanced code completion tool—​or like the "autocomplete" feature on your phone’s keyboard, but for code.

At this time, GitHub Copilot requires a subscription, but you can use it for free for a limited number of lines of code; and students get an expanded free tier.

And yes, I used Copilot to help me prepare the content on AI in this chapter…​

Time To Watch!

Intro to GitHub Copilot in VS Code

File from video:


Check Yourself Before You Wreck Yourself (on the assignments)

Can you answer these questions?

  1. What is the difference between a compile-time error and a runtime error?

  2. How can using output statements help in debugging a program?

  3. What are some strategies you can use when you’re frustrated by a bug in your code?

Sample answers provided in Stuff That’s Tacked On The End.