-
Notifications
You must be signed in to change notification settings - Fork 23
Stage 0: Debugging #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Stage 0: Debugging #240
Changes from all commits
232eafd
f47ef3f
7406f76
54a4fde
71d479a
ef5cf94
224aa80
aed0213
8f4af7a
ebc319c
ad55b61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright 2026 FRCSoftware | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| void main() { | ||
|
|
||
| //[logicError1] | ||
| int width = 2; | ||
| int height = 5; | ||
| int area = width + height; | ||
| System.out.println("Area of the rectangle is " + area); | ||
| //[/logicError1] | ||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,194 @@ | ||||||
| --- | ||||||
| title: Debugging | ||||||
| description: An Intro to Debugging | ||||||
| prev: learning-course/stage0/conditionals | ||||||
| next: false | ||||||
| codeRegionSources: | ||||||
| default: stage0/snippets/src/Debugging.java | ||||||
| --- | ||||||
|
|
||||||
| When completing the Exercise for Java Fundamentals, you probably ran into errors and then fixed them. | ||||||
| This means that you did some debugging! | ||||||
| Debugging is the process of finding those problems, often called bugs, in our code then fixing them. | ||||||
| This section will cover effective strategies to debugging your robot code. | ||||||
|
|
||||||
| <Aside type="note" title="Did you know?"> | ||||||
| Using the word "bug" to refer to an error in a program was popularized by Grace Hopper, an American computer scientist. | ||||||
| While her and her team was working on the Mark II computer, they were able to trace an error to moth that was stuck | ||||||
| in the computer. | ||||||
| They put the moth in their notebook with the note "First actual case of bug being found". | ||||||
|
|
||||||
| </Aside> | ||||||
|
|
||||||
| # Types of Errors | ||||||
|
|
||||||
| There's a few different types of errors that you can run into when writing code. | ||||||
| Let's take a look! | ||||||
|
|
||||||
| <table> | ||||||
| <thead> | ||||||
| <tr> | ||||||
| <th>Name</th> | ||||||
| <th>Timing</th> | ||||||
| <th>Symptoms</th> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example column?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe? I think because there are examples below, it's probably fine |
||||||
| </tr> | ||||||
| </thead> | ||||||
| <tbody> | ||||||
| <tr> | ||||||
| <td>Syntax Error</td> | ||||||
| <td>Compile time</td> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| <td> | ||||||
| The compiler will display an error message, and your code will | ||||||
| not finish building | ||||||
| </td> | ||||||
| </tr> | ||||||
| <tr> | ||||||
| <td>Runtime Error</td> | ||||||
| <td>Run time</td> | ||||||
| <td> | ||||||
| The program will crash and restart, and display an error message | ||||||
| </td> | ||||||
| </tr> | ||||||
| <tr> | ||||||
| <td>Logic Error</td> | ||||||
| <td>Run time</td> | ||||||
| <td>The code will run but not function as intended</td> | ||||||
| </tr> | ||||||
| </tbody> | ||||||
| </table> | ||||||
|
|
||||||
| ## Syntax Error | ||||||
|
|
||||||
| As part of the build process, there's a program called a compiler that takes all the code you wrote and turns it into instructions a machine will understand. | ||||||
|
|
||||||
| Just like English has grammar rules, Java also has a grammar to ensure the compiler can understand the code you've written. | ||||||
| If you don't follow this grammar, the compiler will complain by giving an error. | ||||||
| VS Code shows these errors by putting a red line under the part of code that it thinks is wrong, | ||||||
| just like a typo in Google Docs. | ||||||
|
|
||||||
| This error is called a **syntax error** because these errors occur when you mess up the syntax of a language. | ||||||
| We say this error happens at compile time because it happens when the compiler is translating the code, | ||||||
| before it ever actually runs. | ||||||
|
|
||||||
| <Aside type="note"> | ||||||
| When programming, a yellow line might appear under a part of code. Most of | ||||||
| the time, this is not an error. It's a warning and it typically does not | ||||||
| affect the output of the code. However, you should pay close attention to | ||||||
| the error as it might cause undesired outputs. | ||||||
| </Aside> | ||||||
|
|
||||||
| For instance, the following code would generate a syntax error: | ||||||
|
|
||||||
| <ContentFigure | ||||||
| src="/learning-course/stage0/debugging/syntaxError1.webp" | ||||||
| alt=" int number = 4; Systme.out.println(number); // <- System is misspelled!" | ||||||
| width="600px" | ||||||
| height="800px" | ||||||
| /> | ||||||
|
|
||||||
| Misspelling variable names is also a syntax error. | ||||||
| Not using proper syntax, as shown, also generates a syntax error. | ||||||
|
|
||||||
| <ContentFigure | ||||||
| src="/learning-course/stage0/debugging/syntaxError2.webp" | ||||||
| alt=" int number 4; // <- missing the equal sign! System.out.println(number); " | ||||||
| width="600px" | ||||||
| height="800px" | ||||||
| /> | ||||||
|
Adrianamm marked this conversation as resolved.
|
||||||
|
|
||||||
| If you notice that the code has the red line under it, hover over that line of code. | ||||||
| VS Code will give more information about the error. | ||||||
| Let's look back at the previous misspelled variable name example, | ||||||
| when putting the cursor over the error, it reads `numer cannot be resolved to a variable`. | ||||||
| This means it's looking for a variable called `numer` but it can't find it. | ||||||
|
|
||||||
| <ContentFigure | ||||||
| src="/learning-course/stage0/debugging/syntaxError3.webp" | ||||||
| alt=" int number = 4; System.out.println(numer); // <- number is misspelled! with numer cannot be resolved to a variable, on the top" | ||||||
| width="600px" | ||||||
| height="800px" | ||||||
| /> | ||||||
|
|
||||||
| When the code runs, the error message can also appear in the terminal. | ||||||
| This message is called a **stacktrace**. | ||||||
| A stacktrace traces or identifies the spot in the program where the error happened. | ||||||
|
|
||||||
| Sometimes, errors are too subtle for the compiler to notice immediately, | ||||||
| and it may look like a different part of code is causing the error. | ||||||
| Therefore, it's important to read the stacktrace carefully to try to understand it is saying before | ||||||
| trying to fix the error. | ||||||
|
|
||||||
| ## Runtime Error | ||||||
|
|
||||||
| The code might look like it has no bugs, but when the code runs, an error appears. | ||||||
| When this happens, it's a runtime error. | ||||||
| Runtime errors are errors that can only be found when the program is running. | ||||||
| One common types of runtime error is dividing by zero. | ||||||
|
|
||||||
| Let's look at an example. | ||||||
| {/* rli:ignore */} | ||||||
|
|
||||||
| ```java | ||||||
| int answer = 10 / 0; | ||||||
| System.out.println(answer); | ||||||
| ``` | ||||||
|
|
||||||
| The example looks like it should run. | ||||||
| However, if the code was to run, it would give a runtime error for Diving by zero. | ||||||
| The terminal will show an error similar to `Exception in thread "main" java.lang.ArithmeticException: / by zero at main.main(main.java:5)` | ||||||
| By reading the error message, it's clear that the error is a divide by 0 error. | ||||||
|
Adrianamm marked this conversation as resolved.
|
||||||
| This happens in `int answer = 10/0;` because it's diving by zero. | ||||||
| One way to fix this error would be to swap the 10 and 0. | ||||||
|
|
||||||
| <Aside type="tip"> | ||||||
| The stacktrace will tell you what line the error is on. | ||||||
| For example, `Exception in thread "main" java.lang.ArithmeticException: / by zero at main.main(main.java:5)` | ||||||
| has `(main.java:5)`. | ||||||
| That means the error is on line 5 of `main.java`. | ||||||
|
|
||||||
| </Aside> | ||||||
|
|
||||||
| ## Logic Error | ||||||
|
|
||||||
| There will be times when the code runs, has no errors but it doesn't give the desired output. | ||||||
| Computers don't know what we are expecting them to do. | ||||||
| They only know what we told them. | ||||||
| If the instructions given are wrong, then the output will be wrong too. | ||||||
|
Adrianamm marked this conversation as resolved.
|
||||||
|
|
||||||
| Let's look at simple example | ||||||
|
|
||||||
| ```java #logicError1 | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| This example finds the area of a rectangle. | ||||||
| The width is 2 and the height is 5. | ||||||
| If this code was to be ran, it would print out `Area of the rectangle is 7`, but that isn't right. | ||||||
| The area should be 10. | ||||||
| The code runs with no errors, so why it the code doing the math wrong? | ||||||
| When trying to find the area of a rectangle, the equation for the area is width times height. | ||||||
| In the code, it isn't multiplying the width and height, it adds the two. | ||||||
| Therefore, changing the addition sign to a multiplication sign will fix the issue and it should print out `Area of the rectangle is 10`. | ||||||
| This is a simple example of a logic issue. | ||||||
| The code ran with no errors but, but had bad instructions. | ||||||
|
|
||||||
| Software bugs are often not as easy to spot as this example. | ||||||
| Be sure to take time to read through the code and verify that the intended behavior matches what the code is saying. | ||||||
| Adding `System.out.println();` can also help break down what the code is doing at different spots in the program. | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the heart of the debugging tips is still missing. You're testing your elevator code, and notice that it isn't going to the desired 'up' setpoint, though it is going back to the 'zero' position correctly. How might you debug this issue? Step 1: Understand the system. The first step in being able to fix anything, be it software, hardware, both, or neither, is to understand how the system is designed to work. This knowledge is crucial because it allows you to compare your expectations with reality. For an FRC elevator, this includes knowing such things as:
Trying to solve the problem without knowing these can lead to wasted time. If you didn't know the elevator travel limit, but the setpoint was above the max travel, then of course it will never get there. You might not know to check that if you were unaware of the limits. Step 2: Check your assumptions. Once you know how the system is intended to behave, you can systematically compare your assumptions to the code's. If they are misaligned, then the resulting behavior is going to be undefined, and may result in the observed behavior. For each assumption, try to determine what behavior would be exhibited if that assumption were wrong:
Step 3: Compare the symptoms with the observed behavior This process is at the heart of all debugging: Understanding the system, checking your assumptions, and then deriving the problem from the observed behavior. Not all problems are as simple as a one line gear ratio change though. What if instead, a complex algorithm, like a vision pipeline, seemed to not be working? How would you pinpoint exactly where the problem lies? <insert more debugging info like println, unit tests, etc>
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The debugging page is for basic java not so much for FRC. It goes after java fundamentals so that when students get into the exercises for loops and such, they have some understand of how to fix any errors or issues that come up. That info could be helpful for stage 1 though |
||||||
| ## Helpful Tips | ||||||
|
|
||||||
| Errors will always appear in code, but there are things to do to help avoid errors appear as often. | ||||||
|
|
||||||
| - Use variable names that match their meaning. | ||||||
|
Adrianamm marked this conversation as resolved.
|
||||||
| - Split up complex logic blocks into smaller, simpler ones. | ||||||
| - Keep the code easy to read. | ||||||
| Readability is important. | ||||||
| - Code is made up of building blocks. | ||||||
| Test the smaller blocks so that you're sure your foundation is solid for the larger blocks. | ||||||
|
|
||||||
|
Adrianamm marked this conversation as resolved.
|
||||||
| When there is an error, read through the error message carefully. | ||||||
| If you're stuck, use online resources! | ||||||
| Looking up an error can help point you to the right direction on how to fix it. | ||||||
|
|
||||||
| - For some, changing VS Code to be in a more accessible font (i.e Comic Sans) can be helpful in reading code to find or avoid errors. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a dev environment setup article? This would go well there (accessibility / preferences in general) |
||||||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.