Decompostion and Refactorization
Never Settle For Less
Today, I got to pair program with Gina again! Looking back, it is funny how the timing of everything worked out – it seems like Gina & Micah have decided to take a temporary break on the trello project (integrating trello with the poker app) and, as you have read from my prior blog post, I was not meeting the expectations that were set from Week 2’s iteration meeting. Fortuitously, due to these two events coninciding, Gina and I have been able to pair program today and yesterday (or I might just be tripping and this was bound to happen regardless…who knows?)
Today’s Tasks
- Pair programming w/Gina!
- Refactor Euler #9
- Solve Euler #7 using TDD approach!
- Catch up on blogs!
- if time permits
- TTT in Clojure
Challenges
- Diagnosing which function was taking the longest time - goal was to optimize the time for the
solve euler #9
test case. - Refactoring the function in such a way that we created a sieve such that the triplets being generated by our
find-all-triplets-below-n
were pythagorean triplets instead of merely being a triplet that fulfills the initial constraint ofa < b < c
for a given tuple element of[a b c]
TIL
Euler TDD
- Utilizing the
time
function to analyze the performance of each function call & in a way trace the stack calls to see where it takes the program the longest amount of time to complete a task - Gina and I agreed to not only refactor the
find-all-triplets-below-n
function but also redefine how this function would behave - this was accomplished by first refactoring our test cases to the new expected behavior- Consequently, the new test case proved to be too big to have the function handle all of it
- We decided to decompose the function’s logic into smaller helper functions that would assist us in ultimately passing the test cases
- The end result:
- Two helper functions were created to successfully pass the new test case for
find-all-triplets-below-n
:maybe-p-triplet
perfect-square?
- the function was renamed to:
find-pythagorean-triplets-below-n
- Two helper functions were created to successfully pass the new test case for
- In the end, it was awesome to experience first-hand the power of TDD and how through judicious test writing the tests dictate the flow and direction of the production code
- When we finally got all of our tests passing we had a production code that was concise and elegant. Of course, it helps that we were able to use the amazingly powerful language of Clojure to solve this euler problem with TDD.
Business Term
- Greenfield project: In many disciplines a greenfield project is one that lacks constraints imposed by prior work.
- In other words, it’s a fresh/brand new project
- Interesting tidbit: In construction a greenfield land refers to an underdeveloped land – there is no need to work within the constraints of existing buildings or infrastructure.
Starting to slowly grasp & appreciate the beauty of TDD
Micah has been drilling the importance of TDD every time we have had our iteration meeting and he has taken a look at the code I have written for the euler problems. Again and again, I kept on missing the mark and went straight to writing production code instead of focusing on following the three tenets of TDD. My personal goal is to solve every coding problem and project with TDD and to take my time practicing the art of TDD and to hone my skills in this fundamental skill that is a part of the repertoire of being a software craftsman at Clean Coders.