Code refactoring for better test automation code

I took a break from test automation at the beginning of 2012 due to changing jobs.

Recently, I convinced my employer to buy the Ranorex test automation tool (http://www.ranorex.com) after using the tool's trial version to find a bad memory leak in a production application.

So, I resumed working on test automation with Ranorex and C#.

Initially, I created a few scripts through record and play so that I understand the objects used by the application and their attributes.

After the record and play scripts worked well, the real job started.

I cleaned the scripts up by removing duplicated code, making use of looping, variables, classes, using the repository as little as possible.

The scripts started to look better soon but I was still not very pleased with them.

They were clear enough to me but difficult to understand for another person with less Ranorex and application-under-test knowledge.

Some serious changes were needed but I had no clue how to do them.

Then, I found this amazing book at the local library about code re-factoring:
Refactoring: improving the design of written code.

The book presents many ways of improving the design of existing code such as:

- bad smells in code
- composing methods
- moving features between objects
- organizing data
- simplifying conditional expressions
- making method calls simpler
- dealing with generalization

It is written for people that know an object oriented language like C++, C# or Java (all examples are written in Java).

This book helped a lot improving my code by explaining how to

- replace temporary variables with queries
- remove flag control from conditional expressions
- replace nested conditional with guard clauses
- rename methods
- replace error codes with exceptions
- replace a group of parameters with an object
- move features between classes
and many more ....

A benefit of reading the book is that I have learned a few C# features like custom operators, get and set methods, assertions and exceptions.

I am so happy with this book that I am going to buy it to have it handy for future test automation projects.

It is a very good resource for any tester who aims at improving his coding skills for test automation.

Because, as the author of the book puts it, any fool can write code that a computer can understand, Good programmers write code that humans can understand. 

And for this, code re-factoring is essential.

Share this