0
0
Software Engineeringknowledge~10 mins

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

Choose your learning style9 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.