Red... Green... Refactor!

When I first set out to do Test-Driven Development (TDD) in my Major Project, I had anticipated doing a lot of my development in Java. I am familiar with JUnit, and the thought of writing my tests first, then the code, did not phase me.

Once my project got underway it became increasingly clear that I would not be needing Java at all. MATLAB contained all the functionality I could possibly need, including a nice GUI creator - GUIDE.

As a novice in MATLAB, I then explored to what extent does MATLAB allow and encourage Unit testing? It turns out MATLAB has a nice Unit Testing Framework.

So as of today I have implemented my first function-test and corresponding function of my Major Project! Below are some screenshots of the process I took.

RED RED

Here's my test, it compiles as it's corresponding function is present, however it fails as currently there is no code supporting it!

GREEN GREEN

Implemented the code in membership.m function and the actual output equals that of the expected!


Note: before I got to green, I had an interesting error which I thought I would share here. It turns out comparing doubles is especially tricky:

double error

To overcome this I had to include a small tolerance level to 'verifyEqual' . (initially 0.1, refactored below to 0.0001)


REFACTOR

After the test passed I made some small refactors in the code such as:

  • Made the tolerance on the doubles smaller (see below)
  • Renaming variables to something more transparent
  • Added a title to the graph
  • Added plot titles so the user knows which group each trapezium corresponds with
  • Outlined the different membership degrees visually

Initial threshold Refactored threshold

Final output

KISS - KEEP IT SIMPLE STUPID

So, there is more refactoring to come. However for now I did just the items needed and I will come back at a later date to assess these extra changes, such as:

  • Tweaking parameters of the trapeziums to better represent grey-levels - this is likely to change the output from the test too, so this will need editing.
  • As the grey level will be computed for every pixel, it would be unsuitable to create a graph for thousands of pixels - this will eventually be removed (however could be added as an extra to the final GUI).
  • memberships.m function will need to be tweaked to take an input of .pbm files and iterate through these.