Technical Debt: The Dirty Kitchen ๐ฝ๏ธ๐งน โ
In Agile, we often talk about Technical Debt. It sounds fancy, but it is a simple concept.
The Analogy: Cooking Dinner ๐ณ โ
Imagine you are cooking dinner (writing code).
- Option A: You wash the dishes as you go. It takes a bit longer, but the kitchen stays clean.
- Option B: You throw everything in the sink to cook faster. You eat sooner, but now you have a huge pile of dirty dishes.
Technical Debt is that pile of dirty dishes.
- Clean Code: Washing dishes as you go.
- Technical Debt: "I'll fix this messy code later."
Why is it called "Debt"? ๐ณ โ
Like a credit card, a little bit of debt is okay!
- Sometimes you need to cook fast because guests are arriving (Deadline).
- So you leave the dishes (Bad Code) in the sink.
The Catch: You have to pay "Interest".
- If you don't wash the dishes tomorrow, the food hardens. It becomes harder to clean.
- If you don't refactor your bad code, adding new features becomes slower and harder.
Examples of Tech Debt in Python ๐ โ
1. The "Magic Number" Debt โ
Bad Code:
python
if score > 75: # What is 75?
print("Pass")Refactored (Paid Debt):
python
PASSING_SCORE = 75
if score > PASSING_SCORE:
print("Pass")2. The "Copy-Paste" Debt โ
You copied the same 10 lines of code into 5 different files.
- Interest: If you find a bug in one, you have to fix it in 5 places.
- Payment: Refactor it into a single function
calculate_stuff()and import it.
3. The "No Tests" Debt โ
You wrote a script but didn't write a test for it.
- Interest: Every time you change the script, you are scared you broke it. You have to manually run it 10 times to check.
- Payment: Write a
pytestcase so you can check it instantly.
How Agile Teams Handle Debt ๐๏ธ โ
Agile teams don't ignore debt. We manage it.
- The Rule of Boy Scouting: "Leave the campground cleaner than you found it."
- If you touch a messy file, fix one small thing (rename a variable, add a comment).
- Refactoring Tasks: We create specific tasks in our Backlog called "Refactor Login Module" (paying off debt).
- The "Definition of Done": We agree that a task isn't "Done" until the dishes are washed (code is linted and tested).
Task: Spot the Debt ๐ง โ
Task
Scenario: Your friend wrote this code to check if a user is an adult.
python
# check age
if int(input("Age? ")) >= 18:
print("Adult")
else:
print("Minor")What is the debt here?
Possible Answer
- Hardcoded Logic: The input and logic are mixed. Hard to test automatically.
- Magic Number:
18should be a constant likeLEGAL_ADULT_AGE. - No Error Handling: What if the user types "twenty"? It crashes.
Conclusion ๐ โ
It is okay to take on Debt (write quick-and-dirty code) to meet a deadline. But remember: You have to pay it back eventually, or your project will go bankrupt (become unmaintainable)!