Bird
Raised Fist0
Software Engineeringknowledge~10 mins

DRY (Don't Repeat Yourself) in Software Engineering - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - DRY (Don't Repeat Yourself)
Identify repeated code
↓
Extract repeated code into one place
↓
Replace repeats with reference
↓
Use the single source everywhere
↓
Maintain code easily
↓
END
The DRY principle means finding repeated code, putting it in one place, and using that single place everywhere to avoid repetition.
Execution Sample
Software Engineering
def greet():
    print("Hello!")

greet()
greet()
This code defines a function once and calls it twice instead of repeating the print statement.
Analysis Table
StepActionCode ExecutedOutput
1Define function greetdef greet(): print("Hello!")No output
2Call greet first timegreet()Hello!
3Call greet second timegreet()Hello!
4End of programNo more callsProgram ends
💡 Program ends after two calls to the single greet function, avoiding repeated print statements.
State Tracker
VariableStartAfter Step 2After Step 3Final
greetFunction definedFunction calledFunction calledFunction exists
Key Insights - 2 Insights
Why do we put repeated code inside a function instead of copying it?
Putting repeated code inside a function means we write it once and call it many times, as shown in steps 2 and 3 of the execution_table. This makes code easier to maintain and less error-prone.
Does defining a function run the code inside it immediately?
No, defining a function only tells the program what the function is. The code inside runs only when the function is called, as seen in steps 2 and 3 where output appears.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 3?
ANo output
BHello!
CError
Dgreet
💡 Hint
Check the Output column for Step 3 in the execution_table.
At which step does the function greet get called for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action column in the execution_table to find when greet() is first called.
If we repeated the print statement twice instead of using a function, what would change in the execution_table?
ANo output at all
BFewer steps because no function is defined
CMore steps showing repeated print statements
DFunction calls would increase
💡 Hint
Think about how repeated code would appear as repeated actions in the execution_table.
Concept Snapshot
DRY means Don't Repeat Yourself.
Write repeated code once (like in a function).
Call or use that code wherever needed.
This reduces errors and makes maintenance easier.
Avoid copying and pasting the same code multiple times.
Full Transcript
The DRY principle helps programmers avoid repeating the same code. Instead of copying code multiple times, you put it in one place, like a function, and call it whenever needed. This example shows a function greet defined once and called twice, printing 'Hello!' each time. Defining a function does not run its code immediately; it runs only when called. Using DRY makes code easier to maintain and less error-prone.

Practice

(1/5)
1. What does the DRY principle in software engineering mean?
easy
A. Don't Repeat Yourself - avoid writing the same code multiple times
B. Debug Regularly Yourself - test code often
C. Develop Rapidly Yesterday - finish tasks quickly
D. Design Reusable Yields - focus on output

Solution

  1. Step 1: Understand the acronym DRY

    DRY stands for "Don't Repeat Yourself" which means avoiding duplicate code.
  2. Step 2: Match the meaning to options

    Don't Repeat Yourself - avoid writing the same code multiple times correctly explains DRY as avoiding repeated code to keep it clean and maintainable.
  3. Final Answer:

    Don't Repeat Yourself - avoid writing the same code multiple times -> Option A
  4. Quick Check:

    DRY = Don't Repeat Yourself [OK]
Hint: DRY means write code once, reuse it everywhere [OK]
Common Mistakes:
  • Confusing DRY with debugging or speed
  • Thinking DRY means writing less code overall
  • Assuming DRY is about output or design only
2. Which of the following is the correct way to apply DRY in code?
easy
A. Use functions or methods to reuse code
B. Copy and paste the same code block multiple times
C. Write all code in one long script without breaks
D. Repeat variable declarations for clarity

Solution

  1. Step 1: Identify how to avoid code repetition

    Functions or methods allow reusing code by calling them multiple times instead of copying code.
  2. Step 2: Compare options for DRY compliance

    Use functions or methods to reuse code uses functions to reuse code, which follows DRY. Others increase repetition.
  3. Final Answer:

    Use functions or methods to reuse code -> Option A
  4. Quick Check:

    Functions help reuse code [OK]
Hint: Functions help avoid repeating code [OK]
Common Mistakes:
  • Copy-pasting code instead of using functions
  • Writing long scripts without modular parts
  • Repeating declarations unnecessarily
3. Consider this code snippet:
def greet():
    print('Hello!')

greet()
greet()

What is the main benefit of using the function greet() here?
medium
A. It causes a syntax error
B. It repeats the print statement twice
C. It makes the program slower
D. It avoids repeating the print code by calling the function

Solution

  1. Step 1: Analyze the function usage

    The function greet() contains the print statement and is called twice.
  2. Step 2: Understand the benefit

    Instead of writing print('Hello!') twice, the function is called twice, avoiding code repetition.
  3. Final Answer:

    It avoids repeating the print code by calling the function -> Option D
  4. Quick Check:

    Function calls reuse code [OK]
Hint: Functions let you write code once, run many times [OK]
Common Mistakes:
  • Thinking function calls duplicate code
  • Assuming functions slow down simple prints
  • Believing this causes syntax errors
4. Identify the problem in this code snippet related to DRY:
def calculate_area(radius):
    area = 3.14 * radius * radius
    return area

print(calculate_area(5))
print(3.14 * 7 * 7)
medium
A. The value of pi is incorrect
B. The function is not called for the second print, repeating code
C. The function has a syntax error
D. The function returns nothing

Solution

  1. Step 1: Check code repetition

    The first print uses the function, but the second print repeats the area calculation manually.
  2. Step 2: Identify DRY violation

    Repeating the formula outside the function breaks DRY; both should use the function.
  3. Final Answer:

    The function is not called for the second print, repeating code -> Option B
  4. Quick Check:

    Repeated formula outside function breaks DRY [OK]
Hint: Always reuse functions instead of repeating formulas [OK]
Common Mistakes:
  • Ignoring repeated formula outside function
  • Thinking the function has syntax errors
  • Assuming pi value causes DRY issues
5. You have a list of prices and want to apply a 10% discount to each without repeating code. Which approach best follows the DRY principle?
hard
A. Write a loop that calculates discounted price inside the loop body each time
B. Manually calculate discount for each price separately
C. Create a function to calculate discount and call it for each price
D. Copy the discount calculation code multiple times in the program

Solution

  1. Step 1: Understand the task

    Applying the same discount calculation repeatedly should be done without copying code.
  2. Step 2: Choose the DRY-compliant method

    Creating a function for discount calculation and calling it for each price avoids repetition and keeps code clean.
  3. Final Answer:

    Create a function to calculate discount and call it for each price -> Option C
  4. Quick Check:

    Functions prevent repeated discount code [OK]
Hint: Use functions to apply repeated calculations [OK]
Common Mistakes:
  • Repeating calculation code in loops or manually
  • Copy-pasting discount logic multiple times
  • Not using reusable code blocks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard