Process that enhances both the quality and efficiency of code. Three steps are essential for writing effective and reliable code.
The Big Picture
Writing code is like constructing a building. The initial focus should be on making sure the structure stands (Make It Work), then ensuring it meets all safety standards (Make It Right), and finally, adding the finishing touches to make it look and function beautifully (Clean It Up).
Core Concepts
- Make It Work: Focus on getting your code to function correctly.
- Make It Right: Ensure your code is correct through testing.
- Clean It Up: Refactor and improve your code for maintainability and readability.
Detailed Walkthrough
Step 1: Make It Work
Code that doesn't compile or accomplish the task at hand is useless. The first step is always to make your code work.
- Focus on Functionality: Prioritize getting your code to perform the intended task.
- Avoid Premature Optimization: Don't get bogged down with doing things perfectly from the start.
- Use a To-Do List: Note down improvements and refactoring tasks to revisit later.
- Example: Wrapping certain code in a helper method or converting hardcoded strings to constants.
- Stay on Track: Resist the urge to refactor while ensuring functionality.
Analogy: Imagine building a house. First, make sure the structure is standing before worrying about interior design.
Step 2: Make It Right
After ensuring your code works, the next step is to make it right by writing tests and ensuring correctness.
- Write Tests: Create tests to verify your code works correctly, not just in the happy path but also in edge cases.
- Use Mutation Testing: Improve test quality by introducing mutations and ensuring your tests catch these changes.
- Example: Modify a conditional in your code and check if tests fail. If not, add tests to cover this scenario.
- Handle Edge Cases: Ensure your code gracefully handles unexpected inputs and conditions.
Analogy: This is like inspecting your house to ensure it meets all safety standards and building codes.
Step 3: Clean It Up
With your code working and its correctness verified, it's time to clean it up and refactor.
- Refactor Freely: Use your tests as a safety net to refactor and improve code without fear of breaking functionality.
- Address To-Dos: Tackle the list of improvements noted during the first step.
- Example: Wrap duplicated code blocks in methods, rename variables for clarity, migrate logic to utility files.
- Run Tests Frequently: Continuously run your test suite to catch any issues early during refactoring.
Analogy: Now, it's time to decorate and furnish your house, making it both beautiful and functional.
Understanding Through an Example
Let's say you need to implement a new feature on your website that handles user logins.
- Make It Work: Write the code to handle login requests and ensure users can log in.
- Make It Right: Write tests to cover successful logins, failed logins, and edge cases like invalid credentials.
- Clean It Up: Refactor your login code, extract common logic into helper functions, and ensure variable names are clear.
Conclusion and Summary
Following the steps to Make It Work, Make It Right, and Clean It Up ensures your code is functional, reliable, and maintainable. This process helps you build robust software efficiently.
Test Your Understanding
- What should be your primary focus when you start writing code?
- How can mutation testing help improve your test suite?
- Why is it important to refactor code after ensuring its correctness?
For further learning, you can refer to Clean Code by Robert C. Martin for more insights on writing clean and maintainable code.
'800===Dev Docs and License > 이론 문서' 카테고리의 다른 글
객체지향의 핵심 개념 정복하기 🎯 (0) | 2024.11.03 |
---|---|
What is the Internet (0) | 2024.06.07 |
Network Subnet Introduced (0) | 2024.05.29 |
소프트웨어 크랙 (해적본)은 어떻게 만들고 개발자들은 왜 못막는 걸까? (0) | 2024.05.25 |
The Compilation Process (0) | 2024.05.25 |