These exercises are designed for students who are already comfortable with nested if
expressions. Our goal is to have you look at how nested if
expressions might get used for a specific problem in designing computing systems, and how we design and test such uses well.
allow1
Consider this function:
Feel free to copy it into CPO to get a better look at it.
First off, just look at the code as a piece of written expression. What's your initial reaction? Does this seem like well-written code? Poorly written code? What are you focusing on as you make your initial assessment?
Once you've collected your thoughts on this snippet of code and talked to your partner about it, write down your answers to the questions posed above in a shared Google Doc with your partner.
Call a TA over to discuss the questions above!
show-gradebook
The if-else
expression within allow1
introduced in Problem 1 is meant to control access to materials in a course directory. The idea is that someone could call this function to check whether a requested access is allowed before performing a particular action. Many systems have such "access-control policies": social media sites restrict certain material to friends, Brown restricts access to your personal information to certain staff, and so on.
For example, assume you are writing a web application for running courses, which has a function to display the contents of the gradebook. Your code might look like:
allow1
, who can access what resources in what ways? Summarize your policy two ways: once in prose and once as a collection of test cases (in a check:
block).Call a TA over once you and your partner have summarized your policy as prose and as a collection of test cases in your shared Google Doc.
show-gradebook2
The allow1
function seems awfully complex for guarding the gradebook. Couldn't we just write show-gradebook
from Problem 2 as follows?
Summarize the tradeoffs between the two versions (show-gradebook
using the allow1
function, and show-gradebook2
as written above).
After you and your partner have identified tradeoffs between show-gradebook
and show-gradebook2
, call over a TA to discuss.
One thing you may have noticed when assessing allow1
is something we call redundant boolean logic. Simply put, redundant boolean logic is when if
expressions can be simplified by removing specific references to true
and false
.
Consider the following if
expression:
Since is-rainy
is a Boolean
, it can either be true
or false
. Consider what the expression is-rainy == true
will evaluate to for both values that is-rainy
can take:
As you can see, is-rainy == true
will always evaluate to the value of is-rainy
. Therefore, the if
expression can be written as:
Consider the following function:
Since the expression num-inches > 0
will evaluate to a Boolean
, it can either be true
or false
. If it is true
, then we have an expression equivalent to
If it is false, the expression is equivalent to
As you can see, the value that the expression num-inches > 0
evaluates to is always returned. Therefore, the if
expression can be written as:
Now that we're a little bit more familiar with redundant boolean logic, let's see if you can fix it. Make a copy of allow1
called allow1b
, and modify this new function to eliminate any redundant boolean logic. To make sure that your modifications in allow1b
don't interfere with the original functionality of allow1
, write tests to see that the two functions return the same results.
Call a TA over when you think you've finished.
HINT: There are 3 specific instances of redundant boolean logic that we're looking for.
allow2
: Changing the PolicyAs allow1
(the policy) is currently written, TAs are allowed to write lecture notes. Create a copy of allow1
called allow2
, and edit allow2
to disallow TAs from writing lecture notes.
What tests should you run against allow2
to check that you made the edit correctly?
Call a TA over once you've finished.
Based on the previous two exercises, what would you propose as a set of guidelines for writing readable and maintainable if-else
expressions similar to what is here?
allow3
Rewrite the policy following your guidelines, calling it allow3
. Compare and contrast your guidelines and revised code with that of another student or team.
Call a TA over once you've written allow3
according to your guidelines. Be ready to disuss your guidelines and the changes you made, as well as how your guidelines and changes were different than that of other students.
Discuss the following questions with your partner:
if-else
expression?if-else
expression? How might those alternatives affect the tests?What if we wanted to write our access-control policy using a table?
Add these lines to the top of your code:
As an example of how we can create a table in Pyret, here is a table containing a couple of rows of Rhode Island municipality data:
Your tasks for this problem:
access-control
where you can store all of the data about the access privileges for each user category ("faculty", "ta", "student").allow3
in a new function named allow4
, which uses the access-control
table.HINT: How could the filter-by
function help you? (documentation here)
Call a TA over once you've written allow4
, the policy using a table.
Step back and think about the work you did in this lab. Did you encounter any new ideas about CS today? Did this lab emphasize anything that hadn't been stressed in your previous CS courses? What are you taking away from this lab?