Turning a messy code into simple recursive code

Arthur Rodrigues
3 min readJan 21, 2017

--

When I made programming a big deal in my life in January 2016, one of the big stones that I could not pass through was certainly recursive programs, that concept just could not enter on my mind in any way. So I had the opportunity to join @TsuruPaas team and learn Golang on July 2016, there I had lots of time to experience the new language in small projects before getting my hands dirty into big codes and I did many small recursive projects but they never felt real for me.

In Tsuru my team coordinator once told me “There are many concepts that you don’t understand and yet you can make things work”, for me that was a good feedback but I felt like I was lacking things that I should not and making a ‘real’ recursive program was in that time a big step for me (since I never had the experience in a real system). So I decided to make a tool named XML-Comp that I did on November recursive.

Let me tell you more about XML-Comp: that’s a CLI tool to compare two folders that contain .xml files and output which tags/files/folders the first given folder has that the second does not (like a diff). This was made to help the translators of the game RimWorld to find the missing tags to translate them, but to follow the MVP concept my only concern was to make It functional to contribute to other languages translations and because of It If you wanted to scan multiple folders you had to change the given path to the wanted folder before running the program again.

There’s not many people using my program but I wanted to make their experience the most enjoyable possible and to do so I had to make It recursive and It was time to make all that training worth. My first step was mapping all the functions to have a big picture on how I’ve structured It and how I would make It better, and this was my creepy result:

My messy program

It was not only ugly but a big mess. Every form here is a different function, I had many shared functions and It was not a very linear program. I noticed that It was so messy that I had to separate those functions in two different files, the reader and comparer functions. It was such a big mess in my mind that I even did a Issue about how I should improve It (that had the wrong approach).

My first thought when I decided to make It recursive was that I would put everything that I did on the trash can and start over. I had to make that mess linear and better, after 1 hour of hard work I had the same program running structured as It’s bellow and finally recursive! Turned out that my code was much more helpful than before, I could reuse some of the functions that I had (that made my life much easier), removed lots of nonsense code and reduced the amount of files of my repository.

My super cute recursive code functions!

That was a small victory for me but made me feel very happy about how I’m improving in Golang and programming in general. With this I was able to make many pull requests faster and help other people. If I got your attention until now and you feel like contributing to this small project, I do everything on Github in this repository and I like to have a history of issues to follow my development.

If you have a look into XML-Comp right now (Jan 2017), the master branch is not yet recursive (don’t panic!) but all the changes that I named here you can visit here on my ‘recursive’ branch (that was not merged yet due to testing coverage). If you wish to see a diff of what has changed you can check my PR here.

--

--