The Coding Nightmare - We’ve all Experienced 😵
Imagine spending hours crafting what you believe is the perfect piece of code, only to discover a cascade of bugs when you finally run it. The debugging marathon begins: fixing one issue reveals three more, and suddenly, your elegant solution looks like a house of cards ready to collapse 🐛 .
This was my reality during a local coding meetup while implementing Conway's Game of Life. I wrote extensive code, confident in my approach, only to find the implementation completely missed the mark. Hours of debugging later, I was frustrated and demoralized. 😤
The Red-Green-Refactor Cycle 🔁
TDD follows a simple yet powerful cycle:
Red: Write a failing test
- Define the expected behavior
- Ensure the test fails initially ❌
Green: Write minimal code to pass the test
- Implement just enough code to make the test pass
- Focus on solving the immediate requirement ✅
Refactor: Improve code without changing its behavior
- Clean up the implementation 🧹
- Enhance readability and efficiency
- Ensure all tests still pass
Step 3: Implement the Initial Function 🟢
Create a file named index.js and implement the initial checkPasswordStrength function to pass the first test.
function checkPasswordStrength(password) {
if (password.length < 8) {
return 'Weak';
}
return 'Medium'; // Default return value for now
}
module.exports = checkPasswordStrength;
Run the test again:
npm test
It should pass now.
Step 5 : Optimization and Refactoring 🔵
Once all tests are passing, it's time to improve and refactor the code.
Here’s the refactored version of index.js:
function checkPasswordStrength(password) {
if (password.length < 8) return 'Weak';
// Evaluate complexity conditions
const conditions = [
/[A-Z]/.test(password), // Uppercase letters
/\d/.test(password), // Numbers
/[!@#$%^&*(),.?":{}|<>]/.test(password), // Special characters
];
const score = conditions.filter(Boolean).length;
// Determine strength based on score
if (score === 0) return 'Medium';
if (score === 2) return 'Strong'; // Uppercase + Number
if (score === 3) return 'Very Strong'; // Uppercase + Number + Special char
return 'Weak';
}
module.exports = checkPasswordStrength
Instead of relying on multiple if-else conditions, we optimized the logic by calculating a strengthScore based on the password's complexity. This approach makes the code easier to read and maintain.
Edge Case :
What happens if the password is an empty string (
'') or contains only whitespaces (' ')?
Try adding tests for these scenarios to strengthen the function. You can explore the complete solution
A comprehensive guide that walks you through the TDD process step-by-step, making it beginner-friendly and practical.
A hands-on tutorial focusing on JavaScript and React, perfect for anyone working with modern frontend frameworks.
Jest Documentation
The official Jest docs—a must-read for understanding how to write and run tests effectively with this powerful framework.
Wrapping Up 🎉
Test Driven Development is more than a methodology – it's a mindset. It transforms coding from a reactive to a proactive process, where quality is built-in, not bolted-on. 🧠
Ready to revolutionize your coding approach? Start small, be consistent, and watch your code quality soar. 🚀
Happy Testing! 🧪
💬 Let’s Connect!
Got any doubts or feedback? Share your thoughts in the comments below!
- What topic should I cover in my next blog?
- How did you like this one?
Your feedback and suggestions mean the world to me. Let’s keep the conversation going! 🌟
SOCIAL SHARE CARD GENERATOR