Skip to content

Commit

Permalink
added more clean code challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Popescu committed Jan 15, 2025
1 parent 3c93c0a commit 19cf36d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
59 changes: 49 additions & 10 deletions episodes/clean-code.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,16 @@ There are a number of source code structure rules that derive from this metaphor

##### Separate concepts vertically; related code should appear vertically dense

Goal here is to make use of visual clustering, so that parts of the code that "belong" together become obvious
without having to dive deep into the actual code.

::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::: challenge

##### Refactor the code below by following this guideline

```python
# BAD
def calculate_total(cart, discount_rate):
if not cart:
raise ValueError("Cart cannot be empty.")
Expand All @@ -129,8 +137,11 @@ def calculate_total(cart, discount_rate):
discount = subtotal * discount_rate
total = subtotal - discount
return total
```

::::::::::::::: solution

# BETTER
```python
def calculate_total(cart, discount_rate):
if not cart:
raise ValueError("Cart cannot be empty.")
Expand All @@ -148,15 +159,25 @@ def calculate_total(cart, discount_rate):
return total
```

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::



::::::::::::::::::::::::::::::::::::::::: callout

##### Declare variables close to their usage.

Follows the same principle that closely related code constructs should be in close visual proximity.

::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::: challenge

##### Refactor the code below by following this guideline

```python
# BAD
def calculate_average_grades(students):
total_grades = 0
count = len(students) if students else 0
Expand All @@ -171,9 +192,11 @@ def calculate_average_grades(students):
average_grade = total_grades / count

return average_grade
```

::::::::::::::: solution

# BETTER
```python
def calculate_average_grades(students):
if not students:
raise ValueError("The students list cannot be empty.")
Expand All @@ -189,8 +212,11 @@ def calculate_average_grades(students):

```

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


::::::::::::::::::::::::::::::::::::::::: callout

##### Keep lines short
Expand Down Expand Up @@ -377,10 +403,13 @@ A function should be small enough so one could understand it without having to
do "mental jumps" between various parts of the code. Such "mental jumps" are
time consuming and tiring. Ideally, the entire function should fit on one screen.

What is easier to read, this:
::::::::::::::::::::::::::::::::::::::::::::::::::

``` python
::::::::::::::::::::::::::::::::::::::: challenge

##### Refactor the code below by breaking it into smaller functions

``` python
# Dummy calibration function - operations shown here have no "real life" meaning
def calibrate_fridge(fridge_data, include_safety_checks):
fridge_id = fridge_data.get("id")
Expand Down Expand Up @@ -430,10 +459,9 @@ def calibrate_fridge(fridge_data, include_safety_checks):
return f"Calibration complete. Final temperature: {adjusted_temp:.2f}"
```

or this one:
::::::::::::::: solution

``` python

# Function refactored into smaller functions
def calibrate_fridge(fridge_data, include_safety_checks):
fridge_id = fridge_data.get("id")
Expand Down Expand Up @@ -502,6 +530,8 @@ def send_telemetry(fridge_id, start_temp, end_temp, safety_checks):

```

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


Expand Down Expand Up @@ -533,19 +563,28 @@ what the function does, to the point where comments become superfluous
- Spend time thinking of a good name, and change it as soon as you have found a better one
- Be consistent in your naming: use same phrases, nouns and verbs in your function names

::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::: challenge

##### Refactor the function names below so they are consistent

``` python
# BAD
def determine_optimal_temperature():
def derive_calibration_parameters():
def calculate_reset_interval():
```

::::::::::::::: solution

# GOOD
``` python
def calculate_optimal_temperature():
def calculate_calibration_parameters():
def calculate_reset_interval():
```

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::


Expand Down
7 changes: 0 additions & 7 deletions episodes/gitlab.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ teaching: 20
exercises: 20
---

<style type="text/css">
.main-container {
max-width: 100% !important;
margin: auto;
}
</style>

:::::::::::::::::::::::::::::::::::::: questions

- What is GitLab?
Expand Down

0 comments on commit 19cf36d

Please sign in to comment.