Thursday, January 18, 2018

Civil: Refactoring, refactoring, refactoring

While upgrading the code base from Python 2, PyGame 1.5 and PyQt3/4 (state of the art in 2004) to Python 3, PyGame 1.9 and PyQt5 you need to approximately change only 20-30% of the code base and often enough it can be done by some refactoring.

However, with Python being a dynamically typed language, refactoring is not so simple. Also the Civil project misses unit tests. What worked in the end was:

  • Heavy usage of regular expressions to modify code that follows a certain naming structure to, for example, exchange arguments. I learned quite a lot there too.
  • Starting the program again and again, doing the same stuff over and over and fixing runtime errors one by one, so next time it runs a bit further.
  • Using a source versioning system and saving intermediate steps often (with Git I often amend commits to be able to revert uncommitted changes if needed) so one can go back if nothing else helps.
By far the most typical runtime error I encountered was
AttributeError: 'module' x has no attribute 'y'
which mostly indicates that the variable has a different type of what people expected, which can have many different causes. Still, fixing things can be very satisfying. :)
 

No comments:

Post a Comment