Code tests

How to write Ruby code that is easy to test?

How to write Ruby code that is easy to test? February 7, 2018Leave a comment

The answer is obvious: you have to write simple code. To build a habit of writing clean and simple code, I highly recommend using Test Driven Development approach. Using TDD forces you to write only code that is needed to satisfy your current requirement. What’s more, using red-green-refactor cycle, you can end up with a simple but also well-written code because refactoring would be always the last step of the method creation process.

It’s easier to say than to do it, so you have to stick to your own set of rules. I recommend Sandi Metz rules – if you would stick to them then it would be hard to create complex methods. It’s also important to keep a balanced approach and don’t create 5 separated methods or classes for code that can be packed into one longer method which is still simple and readable.

Since simple code it’s a very subjective concept so there is no one definition of the style you should be using. However, writing code according to the TDD approach is one of the few ways to write simple and testable methods.

Download free RSpec & TDD ebook

Do you want to earn more or jump to the next level in your company? Do you know that testing skills are one of the most desired skills? There is only first step: start testing and do it right. My ebook can help you. Subscribe to the newsletter to get a free copy of the book.

Leave a Reply

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