Quiz 3 - Design Rules and Version Control
In class November 19
- Study key terms from slides and labs.
- Study your notes, the examples, and the slides (Weeks 7-9 on Canvas) about the Low-Level Design Rules. You will be asked to critique code for design rule violations.
- Study Knowledge Checks from all the Version Control labs.
- Sample questions for examining a Git Repo’s state or critiquing code for design rule violations are provided below.
- You may use your own written notes during the quiz.
- Multiple choice, fill-in-the-blank, long answer.
Sample Git Repo state questions
Git analysis - sample 1
Consider the following depictions of a Git repository’s state:
Repo state
Conceptual branch history
- What is the current active branch?
- How many versions will running the command
git log
show? - How many versions will running the command
git log
show after runninggit checkout main
? - Suppose the following changes and commands are made in order:
git checkout main
- Add some lines to
app.py
. git add .
git commit -m "added new functions"
Update both the Repo state diagram and the Conceptual branch history diagram to reflect the changes.
Solution for Git sample 1
- The current active branch is
feature-1
, as indicated by theHEAD
git log
runs in the active branch by default, andfeature-1
has 3 versions.git log
in themain
branch will shown only the first version.
Git analysis - sample 2
Consider the repo state above as the starting point for each question. Briefly explain the impact of running the following commands in terms of the versions, head, and the workspace files.
git reset .
git reset --hard HEAD
.git reset --hard HEAD~1
.
Solution for Git sample 2
- The changes to
main.py
andhello.py
will be unstaged (removed from the Index), but will still be present in the workspace. - We discard any changes since the most recent version:
- All changes will be unstaged.
main.py
will be replaced with its version fromb424cc
.hello.py
will be as-is in the workspace because it is untracked, meaning it has not yet been added to version control. You can tell thathello.py
is untracked because it does not exist in any version. Runninggit status
would tell you that it is untracked.- The
HEAD
will continue to point to versionb424cc
.
- All tracked files in the workspace and the local repository are reset to version
8356ea
. Essentially, we are resetting to the point before the most recent version.README.md
will be removed from the workspace.main.py
will be replaced with its version from8356ea
.- Version
b424cc
will be deleted from the Local Repository. hello.py
will be as-is in the workspace because it is untracked.- The
HEAD
will now point to version8356ea
.
Sample low-level design rule questions
Design critique - sample 1
Consider this simple Python program:
|
|
Identify which of the 6 low-level design rules this program violates and why. Suggest how these violations might be addressed.
Solution for Sample 1
You could potentially argue for other design rule violations, or a violation in rule of the ones below. In my mind, the rules below are clearly violated.
- Violation of “Separate input/output logic from business logic”:
- Issue: The function
process_order
mixes calculating the total cost with printing output directly to the console. - Solution: Separate the calculation logic into a distinct function and handle input/output operations elsewhere.
- Issue: The function
- Violation of “Avoid magic literals”:
- Issue: The prices 1.0, 0.5, and 2.0 are hardcoded directly into the function, making them “magic” numbers without context.
- Solution: Use named constants for these values. They could be placed into a dictionary, for example.
- Violation of “Handle errors at the lowest sensible level, and re-raise/re-throw them otherwise”:
- Issue: The function checks for positive quantities but does not raise a specific error.
- Solution: Use exception handling to raise a specific error when quantity is non-positive.
Design critique - sample 2
Consider this Python code:
|
|
Solution for sample 2
You could potentially argue for other design rule violations, or a violation in rule of the ones below. In my mind, the rules below are clearly violated.
- Violation of “Single Responsibility Rule”:
- Issue: The OrderProcessor class handles multiple responsibilities: it maintains the list of orders, processes them, and prints order summaries.
- Solution: Split responsibilities into dedicated classes, such as
OrderManager
for managing orders,OrderProcessor
for processing, etc.
- Violation of “DRY (Don’t Repeat Yourself)”:
- Issue: The calculation
total = order["quantity"] * order["price"]
is repeated across multiple methods (process_orders
,print_summary
,find_order
, andupdate_order
). - Solution: Create a helper method to calculate the total for a given order to avoid repeating this logic.
- Issue: The calculation
- Violation of “Avoid Magic Literals”:
- Issue: The product-related attributes such as “product”, “quantity”, and “price” are string literals used throughout the class. If any of these strings are mistyped, or if there is a change in the attribute names, the program may fail without easy traceability.
- Solution: Use constants or, better yet, use a dedicated Order class with attributes for product, quantity, and price to encapsulate these values.
- Violation of “Handle errors at the lowest sensible level, and re-raise/re-throw them otherwise”:
- Issue: When removing or updating orders, the methods simply print messages if the order is not found rather than handling the error in a structured manner (e.g., raising an exception or returning an error code).
- Solution: Raise specific exceptions like OrderNotFoundError if an order is not found, and allow higher-level methods to decide how to handle them.
- Violation of “Raise specific errors and define custom errors if needed”:
- Issue: No custom error handling is used for operations that could logically fail (e.g., removing or finding non-existent orders). The use of print statements for error messages is not a robust approach.
- Solution: Define custom exceptions such as OrderNotFoundError or InvalidOrderError to handle various conditions gracefully.