What is the main purpose of the DRY principle in software development?
Think about how repeating code affects maintenance and errors.
The DRY principle encourages developers to avoid duplicating code. This helps reduce errors and makes maintenance easier by having a single source of truth.
You have two functions that calculate the area of a rectangle and a square separately, but both use the same multiplication logic. How can you apply DRY to improve this?
Think about how one function can handle both shapes by using parameters.
By creating one function that accepts length and width, you avoid repeating the multiplication logic. This follows DRY by reusing code.
Which of the following code snippets violates the DRY principle the most?
def greet_morning(): print('Good morning!') def greet_evening(): print('Good evening!') def greet_night(): print('Good night!')
Look for repeated code blocks that differ only slightly.
The code repeats the print statement in each function with only the greeting text changed. This is a clear DRY violation because the logic is duplicated.
Which statement best explains the difference between DRY and WET principles?
Think about what each acronym stands for and their goals.
DRY stands for 'Don't Repeat Yourself' and promotes avoiding code duplication. WET stands for 'Write Everything Twice' and accepts duplication, often leading to harder maintenance.
In a large software project, which of the following is a potential downside of strictly applying DRY everywhere without careful design?
Consider what happens if you try to reuse code too much without clear structure.
While DRY reduces duplication, applying it without good design can create complicated shared code that is difficult to maintain and understand, which can hurt the project.