I’m no stranger to code refactoring. It is a well adopted practice within our team. It has many benefits. Read this blog post if you want to know why.
Some benefits not listed in the article above:
- Get a better understanding of your code-base
- Learn about the coding practices your colleagues use
- Get to know about used frameworks, modules and packages
And that is what happened today; I learned more about a framework we use.
In our Python test automation framework we use Behave, a Behaviour Driven Development implementation in Python. It has a concept of context as a way to pass around data. We do use context in our framework but not to its fullest potential.
While working on an area of our code-base today, I noticed that the way we deal with (global) parameters is quite inconsistent and convoluted. Knowing about behave context I took the opportunity to read a bit more about the workings and see if this could be used as a replacement of global variables.
One hour later, I pushed a PR which gets rid of the messy variables implementation and replaces it with the more elegant context concept. See the following code snippet as a small example:
user_data = (get_user_data(context.config.userdata)) print(“************************ context data ************************”) context.allow_fail = get_user_data_value(user_data, “ALLOW_FAIL”, ALLOW_FAIL) context.app_name = get_user_data_value(user_data, “APP_NAME”, APP_NAME) context.response_times = get_user_data_value(user_data, “RESPONSE_TIMES”, RESPONSE_TIMES) context.configuration_file = get_user_data_value(user_data, “CONFIGURATION_FILE”, CONFIGURATION_FILE) context.dev_mode = get_user_data_value(user_data, “DEV_MODE”, DEV_MODE)
This post is part of a series of articles about “What I Have Learned Today”; acknowledging every day the thing I have learned. Feel free to read the other posts. all opinions in this article are my own and not necessarily represent the views of my employer.