Martin Joo Profile picture
Jan 5, 2022 7 tweets 3 min read Read on X
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 Image
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: Image
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: Image
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: Image
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: Image
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: Image
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…

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Martin Joo

Martin Joo Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mmartin_joo

Dec 25, 2024
🔥 Here are some of the most basic monitoring techniques you can start using immediately:
- Spatie laravel-health
- Error tracking
- Syntethic checks

It takes ~30 minutes to set up these.

🧵 Keep Reading Image
Health checks with laravel-health

The package can check:
- CPU load
- Disk space utilization
- Database connectivity
- Redis
- And even query speed Image
The example contains pre-built checks. For instance, it notifies you if the average CPU load is higher than 2.5 in the last 5 minutes or 2 in the previous 15 minutes.

It also notifies you if the used disk space is more than 70% Image
Read 9 tweets
Dec 25, 2024
💡 This is a DTO or Data Transfer Object from the Domain-Driven Design world.

It helps you:
- Get rid of random arrays
- Structure your unstructured data
- Type-hints everywhere!

🧵 Keep Reading Image
1/11 Life Without DTO

A DTO is used to store and transfer data inside your app. It’s really just a boring PHP class with some read-only properties.

I guess you have seen 500 lines functions from legacy systems where the only argument is an array called $data, like this: Image
2/11 Enter the DTO

First, you need to define a very simple class. It has only read-only properties that define a subscriber in this example: Image
Read 12 tweets
Dec 23, 2024
🔥 Most applications need to export and import large amounts of data.

It is very easy to:
- Waste lots of memory
- Perform hundreds or thousands of unnecessary DB queries

You can avoid them by learning some pretty simple techniques.

🧵 Keep Reading Image
1/

When it comes to working with larger datasets one of the best you can apply to any problem is chunking. Divide the dataset into smaller chunks and process them. It comes in many different forms.
2/ Let's start with a simple CSV or XLS export since it's a common feature in many applications. laravel-excel is a pretty good package that you can use.

Here's a basic export: Image
Read 11 tweets
Dec 6, 2024
💡Dealing with statuses and states can be a real headache in larger applications. You can use the state pattern together with transitions and enums.

Some benefits:
- Encapsulation
- SRP
- Small, easy-to-understand classes

🧵Keep Reading Image
1/14 The first step is to create an abstract class or an interface. This is the contract for our states. This is a very simple example.

We have an OrderStatus. The only difference between statuses is that the order can be changed or not: Image
2/14 After that we can implement a DraftOrderStatus class.

A draft order can be changed: Image
Read 15 tweets
Dec 4, 2024
💡How to communicate between microservices?

In the microservice world, we often have 10, 20, or even 50+ services. Communication can be pretty complex, so let’s make it easy!

🧵Keep Reading Image
1/20 In the microservice world, we split the app into different, small services and multiple databases.

We obviously need a way to communicate between these services. There are two main ways of communication:
- Sync
- Async, or event-based
2/20 Sync

In the sync world, services use a request-response model to communicate with each other.

In most cases, it’s HTTP communication, but it can be something else.

Let me show you a diagram: Image
Read 21 tweets
Dec 4, 2024
💰 How to handle money in PHP

There are lots of low-quality projects when it comes to dealing with money, numbers, percentages, transactions, and all that financial stuff.

Here are some tips that you can use:
- Value objects
- moneyphp/money
- Model casts

🧵Keep Reading Image
1/9 Cent values

In order to handle money values in a reliable way we need to store them as cent values. Instead of storing $19.99 as 19.99 in the database, we store it as 1999. This way we'll have no problem with decimal points, rounding, multiplying, or dividing numbers.
2/9 Value objects

The other thing we can do is to use value objects instead of treating them as float numbers in PHP. So instead of this: Image
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(