side question, which CS class(es) teach about invariants?
The well-known algorithms book Cormen et al. describes a lot of algorithms using loop invariants. I must say I never really liked this approach but I admit it makes things easier to reason about.
Usually goes under Formal Methods: https://github.com/luigiapetre/Formal-Methods-Courses
There’s a book called Logic for Programmers: https://leanpub.com/logic#table-of-contents