Compiled by Course Staff Members from many years of CS 111. Special thanks to Anya Bardach!
Using DrRacket’s Stepper
Ever run into a situation where you want to see what the “state” of your program is step-by-step? That’s what DrRacket’s stepper is for!
Say you’ve got a function defined like the below recursive every?
function.
You can walk through your program running on an example list using the stepper: just click the STEP button rather than the RUN button. All it will do is follow the substitution model (just like we do in the slides) step by step and show you the execution. For instance, here’s a few steps of the stepper on this example:
This step-by-step execution can help you identify things like:
- missing the base case
- incorrect argument (input) order
- incorrect function calls
- weird behavior surrounding certain inputs and more!
Here’s a link to a video walkthrough from the DrRacket Academy Channel on YouTube.
Using the Check Syntax button
One of the reasons we use DrRacket as an IDE in this class is because it has some very helpful teaching tools in it including the Check Syntax button. When you click this, it will apply Syntax Highlighting to your code to help you identify imported and locally define variables. It will also allow you to view the lexical structure of your code by hovering your cursor on-top of variable names. This will then show you the bindings of various names across your program. This can be very helpful when you have many variables named similar things…or make the cardinal mistake of naming inputs to functions the same thing…
You can even “tack” the arrows so they’re always on the screen, rename variables, and more by right-clicking on the variable name in question and accessing those tools from the context menu that pops up.
Spooky code after running
When you run your code, you might notice various lines highlighted in black or red depending which light mode you use. This is DrRacket telling you that these sections of code are never being run, and therefore never being checked for errors. When writing check-expects, you want to make sure they are covering every line of code in your function definitions to ensure you don’t miss any errors (because we’ll definitely catch them!)
One note is if you make a check-expect
for an edge case, the code in the check-expect itself may get highlighted black which is okay.
Commenting out or uncommenting large sections of code
Sometimes you may want to comment out a function you’re working on or haven’t started yet to run other parts of your code without getting stopped by errors. Rather than comment out the code line by line, highlight the full section and use these handy shortcuts:
Auto formatting
As you write and edit your code, it may be hard to read without proper indentation. DrRacket allows you to automatically format your code by highlighting the section and hitting the tab button. This may also help you catch parentheses errors because they won’t format correctly.
Incorrectly formatted:
Correctly formatted:
For example, if we accidentally added an extra paren after our test in conditional, we’d see this.
Rather than this.
Stopping infinite loop code
As we get further into the quarter, it’s very likely that at some point or another you write code that produces an infinite loop or an infinite recursion — it’ll just keep running until it runs out of memory. If your computer has a lot of memory allocated to DrRacket, this can take an unfortunate amount of time. If you notice your code getting stuck in an infinite loop, you don’t have to quit DrRacket and restart it. Hit the stop button in the top right corner. It will become a kill button — hit it again.