0
0
Software Engineeringknowledge~30 mins

DRY (Don't Repeat Yourself) in Software Engineering - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding DRY (Don't Repeat Yourself) Principle
📖 Scenario: You are working on a small project where you need to calculate the area of different shapes multiple times. To keep your code clean and avoid repeating the same calculations, you will learn how to apply the DRY principle.
🎯 Goal: Build a simple example that shows how to avoid repeating code by using a function to calculate areas of shapes.
📋 What You'll Learn
Create variables with shape dimensions
Create a helper variable for shape type
Write a function to calculate area based on shape type
Use the function to get area without repeating code
💡 Why This Matters
🌍 Real World
In real projects, avoiding repeated code saves time and reduces errors when making changes.
💼 Career
Understanding DRY is essential for writing clean, maintainable code in any software development role.
Progress0 / 4 steps
1
DATA SETUP: Define shape dimensions
Create two variables called length and width with values 5 and 3 respectively.
Software Engineering
Hint

Use simple assignment statements like length = 5.

2
CONFIGURATION: Define the shape type
Create a variable called shape and set it to the string "rectangle".
Software Engineering
Hint

Assign the string "rectangle" to the variable shape.

3
CORE LOGIC: Write a function to calculate area
Define a function called calculate_area that takes shape, length, and width as parameters. Inside the function, use an if statement to return length * width if shape is "rectangle". Otherwise, return 0.
Software Engineering
Hint

Use a function with parameters and an if-else statement to decide the area.

4
COMPLETION: Use the function to get the area
Create a variable called area and set it to the result of calling calculate_area with shape, length, and width as arguments.
Software Engineering
Hint

Call the function with the variables and assign the result to area.