Crash early

A dead program normally does a lot less damage than a crippled one.

The Pragmatic Programmer, Tip #38, page 113

Have you ever thought about what can go wrong when the application’s logic is broken, but it continues to work? That might happen when the values are not the ones that you expect. For example, a method/function returns an integer, but a negative one, but you expect it to be positive. Another example is trying to access an unknown array key. The database returns an invalid value. You name it.

What if you won’t allow it and crash early? Yes, the client will see “an error occurred, our engineers are working on it”. The actual benefit is that the program will exit and not cause any harm. No corrupted data in the database nor missing files.

How to crash early in the PHP? There are two ways. Throwing a \LogicExpcetion or enabling assertions on production and using them.

Continue reading Crash early