Week 7 Day 1 - Using Invariants to Debug Linked Lists

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=01ErWJvcPYs



Duration: 1:32:42
142 views
1


A good way to look at code and write code is to think about what invariants need to be preserved. An invariant is something that will always be true about your class, both before and after any function call. Once you know what must be done, you can look at your functions and see if you're maintaining them properly.


I first showed how I used that philosophy when writing a delete function for linked lists (and combined it with thinking about edge cases as well).


I then showed how to use this approach when debugging code. (Debugging other people's code helps build experiences, so I highly recommend becoming an undergraduate TA if you can.) By looking at which invariants were in a function, you can immediately spot some bugs without really even needing to read it closely.


We then went through some actual student code, and saw a lot of different ways linked list code can go badly. Never preserve a reference to a local variable, and always sketch out your code to make sure that it's doing what you think it is doing!







Tags:
CSCI 41
linked lists
invariants
debugging