They make you write better, more maintainable and more quality code and make you a better developer. Algorithms, such as sorting algorithms covered in here are a great way to develop your skills, but testing can boost them even further.
To be clear, software testing is a very broad topic, with layers that I don't know yet. In this post we will be discussing more about TDD (Test Driven Development) and unit tests. Also, what will be shared in this post are personal opinions and experiences on my recent contact with the area.
As a lot of people (I would assume), I didn't think much of tests, at least not by part of the developers. We can think that it would take much development time to be actually worth it, that the time we would spend writing and thinking about tests was precious coding time that would be lost. Damn how I was wrong!
I'm not gonna lie, it does take some more time at first, but that time is completely worth it. Lets see why.
The journey and benefits seen so far 📈
I've worked with NodeJs, Vue and Python at the past. None of the companies that I worked had a strong testing habit, if any to be honest, so it wasn't something that I was familiar with. For the past months I discovered Ruby and Rails and fell in love, leading to my current job as a Rails developer.
One of the amazing things about Rails is the strong testing atmosphere that the community has, what made me stumble with it. I became interested and my current job started to be more conscious about it as well, that led me to start studying and using it in personal projects. It completely had me on its hands.
You can quickly see the benefits and the change in your coding style, the decrease of constant bugs and necessity of going through your code over and over again. It forces you to change how you write your code almost completely.
The old ways 👵
How I used to do, and probably a lot of people too, was to code this way:
- Write the code 💻
- Run it ▶
- See what errors appear 💀
- Repeat 🔁
You can see that this is definitely not a good way to do it, but it is possible that we don't realize it at the moment. So we keep doing it like that, stumbling on countless errors and aging decades with the stress that the bugs and mysterious errors that appear out of thin air causes.
That's one place where testing shines.
The testing way 🧪
So the new approach would be something like this(using TDD and unit tests):
- Think about the piece of code that you are about to write 🤔
- Write a test ✍
- Code the feature to pass that test in a simple way 💻
- Run the tests ▶
Lets get a bit more into this process, first we think about the piece of code that we must write. The thing here is to REALLY think about this piece of code. What is the goal that you are trying to achieve with it, the details on what it is supposed to work, what it should do and what it shouldn't do. Put the work and the time to THINK about it at this stage.
Secondly, write a test on one of the specific things that should happen. Think about what should happen when that part is executed and what is the expected output or behavior of that part.
Now we get to coding! Write the code that will pass the test in the simplest way possible. Think on how you can make it behave the way that you and your test are expecting.
Finally, run your test. See if the code runs the way it should, refactor it to be even more simple if you can and fix what should be fixed. The more spoken way is to write your code to fail the test first, and then write it simply to pass that test. I personally don't have the patience to do it like this every time, but it can be a good way to get the grasp of it at the beginning.
Conclusion and thoughts 💭
The testing way of coding can really increase your code quality and your skills as a developer. It kind of forces you to put the time on what we actually do, or at least should be doing: think about the best and efficient way to solve the problem at hand.
For my fellow rubyists out there that want to start on this path, a book that was recommended to me that helped and is helping me a lot and I truly recommend you to get is Effective Testing with RSpec 3. Even if you're not a Ruby or Rails developer I think this book may help you with some concepts and techniques.
As for the more experienced folks, what are your thoughts and recommendations? Anything that would help me and other beginners is welcomed 😃