Do Less

I’ve thought a lot about performance this past year. Both on the client side and the server-side as well. While it might not come as a surprise, the number one thing you can do to improve performance is to do less.

And this applies to most everything that you can actually do.

Code should do less. The more your code has to do, the longer it’s going to take. Obviously, not all operations in your language are equal, but beneath the covers, the computer has to do more.

But we need to do more

That network call out to Facebook has to do a lot. It has to travel a larger distance than keeping things local. It has to perform DNS lookups, or it has to wait around for the remote machine to do more stuff.

When that user signs up and you decide to send out that email? That’s doing more. Fetching something from cache that you’ve already got in memory? That’s doing more than you should.

It seems obvious, but at every step, you should seek to do less. And the problem is that while it’s obvious, it’s something we overlook. We forget that every action, every line will add to the cost of running the program. And no, I don’t mean that one line is equal to another. As I said before, it’s what’s happening beneath that really matters.

Of course, you can’t help but do more. After all, first it was just a simple sign-up. Then you needed to send out an email. You also needed to update the mailing list database on sign-up. And now you need to run another set of operations for fighting spam.

The Solution to doing more with less

The easiest solution to doing more is to not do more when you would normally do it. Push what you need to do off to a later time. Either using a messaging system like RabbitMQ or even later using crons, you can probably find something that will suit your needs. The best part is, if you architect it that way, instead of adding more code to the part where you’d normally add it, you are instead just adding code some place else, listening for that event.

This really emphasizes the Open-Closed principal in SOLID. Open for extension, closed for modification. Every time you find yourself modifying existing code to add a new action on a particular event, ask yourself if you really need to have that action take place at that exact time. Ask yourself if all the other processing should be held up waiting for that action to take place.

Leave a Reply

Your email address will not be published. Required fields are marked *