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

Apr 15
🔥 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
Apr 10
Column order is the most important thing in a composite index.

Let me show you why🧵 Image
1/8 You can imagine the composite index as a table: Image
2/8 This index contains the created_at and the user_id columns in this order.

As you can see, the index is only ordered by created_at.

user_id is only ordered in relation to created_at.

Just look at the value 1. It's all over the place: Image
Read 9 tweets
Apr 7
Here are my favorite 22 Laravel packages📦

I would immediately DIE without them!

🧵Keep Reading
1/ laravel-excel

The single best package to handle Excel exports and imports:

- Exporting collections
- Storing files
- Exporting from a query
- Queue jobs
- And more Image
2/ laravel-health

This amazing Spatie package can monitor the health of your application and server. It sends you notifications if something's wrong. For example:
- CPU load
- Used disk space
- Database connections
- Redis
- and more Image
Read 24 tweets
Mar 12
MySQL can do more than you think

- CTEs
- Window functions
- Partitioning
- ...and more

🧵 Keep reading Image
2/ Common Table Expressions (CTEs) turn nested spaghetti queries into clean, readable temp tables.

Instead of subquery hell, write modular SQL that mirrors how your brain works.

Without CTE: Image
3/ WIth CTE: Image
Read 14 tweets
Jan 30
Redis is way more than just a cache system - it's a powerful distributed key-value in-memory database with rich features. Let me break down why Redis is awesome and what you can actually build with it.

Keep Reading🧵 Image
2/12 Redis stores everything in memory, making it blazing fast compared to traditional databases like MySQL that need disk reads.

But don't worry - Redis still persists your data using AOL and RDB, so you won't lose anything if you restart.
3/12 Redis Lists are perfect for building queues, stacks, and temporary storage.

You can implement job queues, undo/redo functionality, recent activity feeds, and data buffers. Think Spotify's "Add to Queue" or Amazon's "Recently Viewed Items"
Read 12 tweets
Jan 16
Want to improve your MySQL query performance?

Here's a thread with fundamental tips and techniques to optimize your database queries.

🧵 Keep Reading Image
2/ First tip: Use EXPLAIN to identify inefficient queries.

Watch out for 'ALL' access type. It means full table scan. MySQL scans the entire table. It's like a for loop that iterates through each row,

The 'Index' access type isn't great either, as it reads the whole BTREE index.
3/ 'Range' access type is good, sinbce MySQL is able to identify a range of rows fromn an index. But check if 'Using index' appears in Extra column. If not, you're doing unnecessary I/O operations.

'const' or 'ref' means your query is optimized.
Read 12 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!

:(