Save all mutants – and rule your code!

Save all mutants. I repeat: Save all mutants. Because mutant testing is a topic I was thinking of recently. Josemi (@Twitter), a good friend of mine and a awesome software and computer expert was sending me a article: "This will be interesting for you René" he said to me. And ...tuuuching... "You have a new message" thank you Skype! Before we start you can find the concepts for mutant testing here: Wikipedia.   I was trying it with my rspec/RoR tests I have by using the super easy to install mutant gem from mbj. I saw for some tests only a few mutants that I "should kill". For some I was thinking of one of my loved cartoon series that is almost always starting with a big X followed by Men, Factor, Force,Calibur etc. : What a mess. The funny thing is that I was knowing that my functional coverage was not higher than 75%. But to see your computer telling you where is unknown territory is funny. And then always the message "Kill all mutants".  


Why should I kill all mutants? To have full coverage? Yeah this is a good reason, but I am not willing to do it now. This is because I got interrupted by the interpretation of what this tool was telling me about my code. It is saying to me nothing else than: Hey Sir, there is code that contains of a lot operators that can be exchanged without breaking the code (mutant is changing operators but the code is valid code). If you follow me here then the next idea is that this means it is also most likely that this code parts are having a higher possibility to contain errors. See a code as a function with a lot of input parameters. You might remember from your last algebra lessons that with a higher number of variables the functions depend on more input values, can have complexer graphical representations and strange unexpected behavior.     Your code is only a very complex function and every mutant in the mutant testing tool is describing a new function. By this mutant is kind of a code generator where the code is given but the task that is needing this code is not...are you now smiling a little bit? I do.  

Befriend your mutants

  There is certainly no way to get rid of code that can be altered by changing simply the operators in it, but you can make friends with them by selecting the mutants you want to have. If you are having mutants on places you do not want to meet them, just change the complexity at the position in the code and you will see less. When there is a huge amount of mutants presents in one part of your code than you know this part will most probably contain logic errors and needs refactoring! The solution with this mutant testing can also be adding tests of course, but this is only the step if there is nothing to refactor.

Have a smoke with your mutants

  You might know terms like smoke testing, smoke tests etc. and this is a thing I am having some experience in. I now have a new way of estimate issues. I can use mutant testing as smoke tester. If there are a lot of now mutants in a new release version than before, I know that this is a sure thing to have hidden, ugly bugs in the system. In average a developer is producing 1 mistake per ten lines of code. Some of this mistakes are resulting in wrong functionality, performance issues or design issues. That is why you should always prefer simple code with less operators over quick and dirty hacks. Good code is clever and is helping you solve your tasks as developer. Enjoy