Friday, January 4, 2013

One small step

Photo of Euclid
This week I started on the first two algorithms used as examples in Knuth's book. These algorithms are Euclid's methods for finding a greatest common denominator. The first algorithm does just that, solves for the greatest common denominator. The second algorithm takes it a step further by accepting two inputs M and N and solving for A*M+B*N=D where D is the greatest common denominator. Many would say these algorithms are trivial and that implementing them would be a waste of time. I would have to agree.

That being said I do not regret spending a few hours writing these two algorithms in 3 languages and creating unit tests for each. Why? It offered me an opportunity to practice something I rarely get the opportunity to practice: starting a project. One of the things I have a hard time doing is starting a project in an organized fashion, and the main reason is I rarely start large coding projects (after all they are large, more time is spent working on them then is spent starting them). Often times as the project grows in size and complexity I find that I end up having to scrap everything and start over as my code has become to unmanageable as a direct result of poor base code.

I jumped at the opportunity offered by the small project and thought about end-game when starting these. Unit tests were created prior to writing the functions they were testing (as opposed to my normal approach which is testing functionality I have already implemented, bad habit), code was commented nicely, makefiles were made, documentation was generated, and good variable names were used. Even though these projects were small, I approached them each like they were large projects and I feel I walked away with a lot because of it. If you want to see the beautiful code created Euclids Algorithm is here, and Euclids Extended Algorithm is here. They are implemented in Java, Go, and Javascript.

For unit tests in Go I tried using a table driven approach as recommended by Dave Anderson in his Wikipost on Table Driven Tests. It worked quite well, so well in fact that I decided to use it for my tests in Java and Javascript.

When it came to writing unit tests in Java, I came to the conclusion that I had no idea how to run junit from terminal! Up until this point all of my unit tests have been done using Eclipse. After a while I realized my problem was not with running junit from terminal, it was compiling java! I had never done that either! My problem was solved with a little help from stackoverflow and this article from IBM about Managing the Java classpath.

Then it came time to implement the algorithm in Javascript and I needed to decide on a way to do this from terminal... I considered using Rhino but settled on a Makefile that started Firfox in SafeMode with the Debugger Console. I used an HTML file for standard output and wrote my own unit test framework again using table driven tests from Anderson's post.

At the end of the day it was a bloody fine experience and I did enjoy it.

Wednesday, January 2, 2013

New Years Resolution

In the spirit of New Years, I decided that I too would conform to social norms and come up with some sort of "resolution" in the vain attempt to make myself a better person this year than I was last. My resolution is to program a minimum of 2 hours per day. To support this goal -and to keep myself accountable- I sat down last night and created a nice little Android App that keeps track of how many hours a day I have coded. It uses a Chronometer to keep track of how many hours I have programmed on the current day and logs it to a data structure that is displayed using a CalendarView front end. To keep the data persistant between closing the app and returning, I used Google's gson library to export the data as a JSON object and saved it to the local file system. The app is a little rough around the edges but it should get the job done.

Why am I talking about this app on my blog? Well two hours of programming a day will certainly help me progress through my research project and The Art of Computer Programming a little faster. If you want to see the source code for the app so that you can better ridicule my poor work, it can be found in my GitHub repository here: https://github.com/Crackerz/NewYearsResolution

Cheers and Happy New Years!