# Writing clean, maintainable, and reusable code

Hey there! Today, we’re going to learn how to write code that’s easy to read, easy to fix, and can be used again and again. Think of it like building with Lego blocks – if each piece is well-made, you can build lots of cool things without getting frustrated.

#### 1. Clean Code

Clean code is like a neat and tidy room. Everything is in its place, and it’s easy to find what you need. Here are some tips to keep your code clean:

**Meaningful Names**: Use names that tell you what the thing does.**Comments**: Write little notes to explain what the code does, but don’t overdo it.**Consistent Style**: Stick to the same style for writing code.

###### Example:

```
# Bad Code
def f(x):
return x * x + 2 * x + 1
# Good Code
def calculate_quadratic(y):
return y * y + 2 * y + 1
```

In the bad code, `f`

and `x`

are not clear. In the good code, `calculate_quadratic`

and `y`

tell us what the function does and what it needs.

#### 2. Maintainable Code

Maintainable code is like a book that’s easy to update. If the story changes, you can easily add, remove, or change parts without ruining the whole book.

**DRY Principle**: Don’t Repeat Yourself. If you have code that does the same thing in many places, put it in one place and use it everywhere.**Modularity**: Break your code into small, manageable pieces (functions or classes).

###### Example:

```
# Bad Code
def calculate_area_and_perimeter(length, width):
area = length * width
perimeter = 2 * (length + width)
return area, perimeter
area1, perimeter1 = length1 * width1, 2 * (length1 + width1)
area2, perimeter2 = length2 * width2, 2 * (length2 + width2)
# Good Code
def calculate_area(length, width):
return length * width
def calculate_perimeter(length, width):
return 2 * (length + width)
area1 = calculate_area(length1, width1)
perimeter1 = calculate_perimeter(length1, width1)
area2 = calculate_area(length2, width2)
perimeter2 = calculate_perimeter(length2, width2)
```

In the bad code, the area and perimeter calculations are repeated. In the good code, they are in separate functions that can be used wherever needed.

#### 3. Reusable Code

Reusable code is like a versatile tool – you can use it in many different projects without having to make big changes.

**General Functions**: Write functions that can work with different kinds of inputs.**Avoid Hardcoding**: Don’t put fixed values in your code. Use variables or parameters instead.

###### Example:

```
# Bad Code
def greet_john():
print("Hello, John!")
# Good Code
def greet(name):
print(f"Hello, {name}!")
```

In the bad code, the greeting only works for John. In the good code, the greeting works for anyone because it uses a parameter `name`

.

#### Practice Questions

**Refactor the Code**

Here’s some messy code. Make it clean, maintainable, and reusable.

```
# Messy Code
def sum_and_average(x, y, z):
sum = x + y + z
average = sum / 3
print("Sum:", sum)
print("Average:", average)
```

**Write a Reusable Function**

Write a function that takes a list of numbers and returns the largest number.

#### Solutions

**Refactored Code**

```
# Clean, Maintainable, and Reusable Code
def calculate_sum(numbers):
return sum(numbers)
def calculate_average(numbers):
return sum(numbers) / len(numbers)
def print_sum_and_average(numbers):
total = calculate_sum(numbers)
avg = calculate_average(numbers)
print("Sum:", total)
print("Average:", avg)
# Using the functions
numbers = [1, 2, 3]
print_sum_and_average(numbers)
```

**Reusable Function**

```
# Function to find the largest number
def find_largest(numbers):
if not numbers: # Check if the list is empty
return None
largest = numbers[0]
for number in numbers:
if number > largest:
largest = number
return largest
# Using the function
numbers = [1, 3, 5, 7, 9]
print("The largest number is:", find_largest(numbers))
```

By writing clean, maintainable, and reusable code, you make your coding life easier and your projects better. Remember:

- Use meaningful names and consistent styles.
- Keep your code DRY and modular.
- Write functions that can be reused in different situations.

Now go ahead and practice these tips to become a coding pro!