Concepts:
Single Source of Truth (SSOT):
Abstraction:
Reusability:
Benefits:
Maintainability:
Readability:
Reduced Errors:
Efficiency:
Examples:
Functions:
# Repetitive Code
area1 = length1 * width1
area2 = length2 * width2
area3 = length3 * width3
# DRY Code
def calculate_area(length, width):
return length * width
area1 = calculate_area(length1, width1)
area2 = calculate_area(length2, width2)
area3 = calculate_area(length3, width3)
Classes:
# Repetitive Code
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
class Cat:
def __init__(self, name, age):
self.name = name
self.age = age
# DRY Code
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
class Dog(Animal):
pass
class Cat(Animal):
pass
Modules:
# Repetitive Code
# file1.py
def fetch_data():
# code to fetch data
# file2.py
def fetch_data():
# same code to fetch data
# DRY Code
# fetch_module.py
def fetch_data():
# code to fetch data
# file1.py
from fetch_module import fetch_data
# file2.py
from fetch_module import fetch_data
Common Pitfalls:
Over-Abstraction:
Context Matters: