Red, Green, Refactor is the mantra for Test Driven Development. When I started out with Unit Tests the idea of creating tests to make sure that a function behaved properly was easy to understand, but the rest of the cycle didn't really sink in until later.
Red, making a failing test, is important to the TDD approach. When practicing TDD, if you start out by creating a test and it passes without the actual code having been written yet, then there is something wrong with your test. By making sure that your test fails first, then you can be sure that whatever changes you make to pass the test will have been valuable changes that were needed to make the function work properly. If you are writing a test against existing code and it passes immediately, then you do not need to make any changes to your code as your are already passing whatever that new test is validating for.
Green, making the test pass, is the obvious part of Unit testing. You want lots of green in your project. You want to be able to sit back and quickly have tests run across your system and tell you that things are working as intended. You can have Green without practicing the rest of TDD, and there will be some benefit to doing that.
Refactor, modifying working code to be more efficient and more easily changed, is where to true value of TDD lies. Lets focus on how the refactoring step allows for you to change your code to make it easier to modify in the future because that is where using TDD can really improve development time. Most of the time, if you need to write a piece of code, you or someone else will need to come back to it again in the future. It will need to be changed or added to in some way. Being able to safely rewrite your code to be as easily modifiable as possible will make it much quicker for that next person to come in and do their work.
Utilizing the Refactor step also allows you as the developer to try out different approaches to the problem. Lets say that you have a large case statement or some nested if else statements. You can hopefully look at that logic and recognize that there is a better approach to solving that problem. Now that you have your tests wrapped around that logic and know that the logic works as intended, you can rewrite that logic using a different approach and know whether or not that rewrite broke anything or not. Rewriting and improving code will make whatever you are working on better, but it will also help you as a developer improve. Once you rewrite a select statement into something better, you will recognize that same pattern in the future and rather than needing to start out with a select statement, you can jump immediately to the improved code, which you may then be able to further refine through more refactoring.
No matter what you are doing in life, practice and repetition allows you to improve upon your action. The Refactor step of TDD is the best way for developers to practice improving their code. You get to practice on Real code with Real logic. You are actively improving your code base and adding value to your project. And of course, because you have tests around everything, you know that you aren't causing any bugs.
Red, making a failing test, is important to the TDD approach. When practicing TDD, if you start out by creating a test and it passes without the actual code having been written yet, then there is something wrong with your test. By making sure that your test fails first, then you can be sure that whatever changes you make to pass the test will have been valuable changes that were needed to make the function work properly. If you are writing a test against existing code and it passes immediately, then you do not need to make any changes to your code as your are already passing whatever that new test is validating for.
Green, making the test pass, is the obvious part of Unit testing. You want lots of green in your project. You want to be able to sit back and quickly have tests run across your system and tell you that things are working as intended. You can have Green without practicing the rest of TDD, and there will be some benefit to doing that.
Refactor, modifying working code to be more efficient and more easily changed, is where to true value of TDD lies. Lets focus on how the refactoring step allows for you to change your code to make it easier to modify in the future because that is where using TDD can really improve development time. Most of the time, if you need to write a piece of code, you or someone else will need to come back to it again in the future. It will need to be changed or added to in some way. Being able to safely rewrite your code to be as easily modifiable as possible will make it much quicker for that next person to come in and do their work.
Utilizing the Refactor step also allows you as the developer to try out different approaches to the problem. Lets say that you have a large case statement or some nested if else statements. You can hopefully look at that logic and recognize that there is a better approach to solving that problem. Now that you have your tests wrapped around that logic and know that the logic works as intended, you can rewrite that logic using a different approach and know whether or not that rewrite broke anything or not. Rewriting and improving code will make whatever you are working on better, but it will also help you as a developer improve. Once you rewrite a select statement into something better, you will recognize that same pattern in the future and rather than needing to start out with a select statement, you can jump immediately to the improved code, which you may then be able to further refine through more refactoring.
No matter what you are doing in life, practice and repetition allows you to improve upon your action. The Refactor step of TDD is the best way for developers to practice improving their code. You get to practice on Real code with Real logic. You are actively improving your code base and adding value to your project. And of course, because you have tests around everything, you know that you aren't causing any bugs.
Comments
Post a Comment