Martin Joo Profile picture
Upcoming Laravel performance book: https://t.co/X1errH6QKl Blog, DevOps, DDD & microservice books: https://t.co/yJPUsoqTLh

Jan 5, 2022, 7 tweets

This is the red-green-refactor cycle from the Test-Driven Development world.

It helps you:
- Approach problems from a different perspective
- Avoid bugs
- Build more maintainable applications

🧵Keep reading

1/6 The red

First, you write a test. You don’t have any code yet, but you know what you want to write.

So you write a test for the most simple use-case.

Something like that:

2/6 The green

(Yes, we are doing the calculator example!)

Now it’s time to make this test pass, or in other words, make it green!

In this phrase you should write the minimum amount of code that makes your test green:

3/6 In the red again

After that, you may find new requirements or edge-cases for your class.

For example, what happens with division by zero?

You found a new edge-case, so you write a test for it:

4/6 In the green again

Of course, you don’t have any code yet that takes care of division by zero, so you should write it:

5/6 The refactor

Now everything is green, but during lunch, you read about clean code, and now you hate the divide function.

At this point, you have 100% code coverage meaning the whole class is covered with tests. You’re almost unable to make mistakes! So let’s refactor:

6/6 Thank you for reading this!

Today I released a book “Test-Driven APIs with Laravel and Pest” where I cover these topics in great detail.

So far it got only 5-star ratings:
martinjoo.gumroad.com/l/tdd-api-lara…

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling