=====
CS 111 - Week 4 Lecture 1 - 2025-09-16
=====
=====
TODAY WE WILL:
=====
* announcements
* concept of REFACTORING
* intro to branching - cond expression
* maybe more?
* prep for next class
=====
* Math Club has their FIRST meeting TODAY
Tuesday, September 16 - 6:00 pm - BSS 308
* should be working on Homework 3
deadline/at least first attempts due by this Friday, Sept. 19
* submit early, submit often!
=====
Refactoring
=====
* fixing an expression with an error? or a function
with an error? etc.? That's debugging;
* and sometimes we write a function, and then think
of something ELSE it could do
* refactoring is different from either of those --
it is taking WORKING code and IMPROVING it without
actually changing what it does;
* why?!
* some examples reasons:
* might make it more readable
* might make it more efficient
* might make it easier to reuse
* ...
* NOTE: ANY time you change your code, there is a chance you
might accidentally introduce an error --
do you see how check-expect (and other check- expressions)
may help make refactoring less dangerous, since these are
re-run every time you click Run?
so if all your tests still pass after you refactor, that
increases your confidence that you have not introduced
an error while refactoring; (not a guarateee, but helps!)
=====
cond (conditional) expression
=====
* ONE of the ways to write a branch in BSL Racket!
* a multi-way branching expression!
...make ONE choice from 2 or more options
* syntax:
(cond
[boolean-expr1 result-expr1]
[boolean-expr2 result-expr2]
...
[else result-expr-N]
)
* the [bool-expr result-expr] parts can be called
branches or clauses
(yes, that includes the [else result-expr-N] also)
* semantics:
see if the boolean-expr1 is true --
if it is, the value of this cond expression
is result-expr1
if boolean-expr1 is false,
see if boolean-expr2 is true --
if it is, the value of this cond expression
is result-expr2
and so on...
if you reach the else, else is ALWAYS true,
and then the value of this cond expression
is result-expr-N
* NOTE:
AT MOST one branch will be "taken" --
as soon as one of the clauses' boolean expression is #true,
that clause's result-expr becomes the value of this cond.
so the order of the cond clauses MATTERS;
* CS 111 Class Style:
* start each [boolean-expr result-expr] clause in
a cond expression on its own line
* INDENT the start of each [boolean-expr result-expr]
clause in a cond expression under its (cond
* (for a long clause, indent its continuation line(s)
so it is clear which clause it is continuing)
* as always, ASK me if you have any questions about CS 111
class style!