SlideShare a Scribd company logo
TDD Walkthrough
The Time in Words
Peter Kha: peter.kha@greatersum.com
TDD Walkthrough - The Time in Words
Hello and welcome to my TDD Walkthrough!
Today’s exercise is The Time in Words, taken
from HackerRank.com at the following URL:
https://www.hackerrank.com/challenges/the-
time-in-words/problem
TDD Walkthrough - The Time in Words
In these TDD Walkthroughs, I assume that my
readers have basic knowledge of test driven
development. I highly suggest having an
understanding of TDD before going through this
walkthrough. The following blog post explains
the cycles of TDD.
http://blog.cleancoder.com/uncle-
bob/2014/12/17/TheCyclesOfTDD.html
I also assume that my readers have knowledge
of refactoring tools and terminology. If you need
more information, this blog post on the six core
refactorings will provide enough information.
http://arlobelshee.com/the-core-6-refactorings/
TDD Walkthrough - The Time in Words
For this exercise, I have the following already set up:
Chrome browser
Visual Studio Code
Jasmine testing framework
(see my other blog on setting up: https://www.greatersum.com/tdd-setup-jasmine/)
With the above already set up, let’s get started!
TDD Walkthrough - The Time in Words
I would normally brainstorm a test list of all the functionality I expect out of my
code. Since the exercise provides good examples of that functionality, I will use
those examples as my test list.
My first step will then be to implement the simplest test case, and for that I choose
5:00, which translates to “five o’ clock”.
Before that though, we must set up our project. Creating files, pointing to file
locations, and running for the first time.
TDD Walkthrough - The Time in Words
Starting with a basic Jasmine standalone
project, I will create the file that contains my
tests.
TDD Walkthrough - The Time in Words
There are various conventions for file names out
there. I tend to use the affectionately dubbed
kabob-case: the-time-in-words.spec.js
TDD Walkthrough - The Time in Words
This is a spec file. This file needs to be included
in the Jasmine spec runner, so open up the
SpecRunner.html file next.
Look for the comment that says “include spec
files here”, and include the spec file you just
created.
TDD Walkthrough - The Time in Words
Let’s write our first test. First, we create the test
suite:
And then we write the test function:
In our test function, we write what we are
expecting:
TDD Walkthrough - The Time in Words
At this point, you should be able to run your test runner HTML file by opening it in
the browser, and see a failing test.
Failing the test here is good! You haven’t written any production code yet, and
that’s ok. What you did write is a test that will tell you when your code fulfilled a
purpose, and that is to translate 5:00 to five o’ clock. It’s not much, but we take
one step at a time.
TDD Walkthrough - The Time in Words
Ok, so what does the test runner say?
theTimeInWords is not defined. That makes
sense, we haven’t written anything called
theTimeInWords yet.
Let’s go make that function!
TDD Walkthrough - The Time in Words
To keep the code organized well, the tests are
contained in the spec file, in the spec folder.
So then our source code should go in the source
folder!
TDD Walkthrough - The Time in Words
Our new source file will need to be included in
the Spec Runner though, so let’s not forget to do
that.
TDD Walkthrough - The Time in Words
Now back to our source file. The first issue is
that theTimeInWords is not defined, so let’s go
define it.
TDD Walkthrough - The Time in Words
You might be tempted to go implement all of the function right now. But bear with
me and take one small step at a time. Let’s focus on the error at hand, and we’ll
tackle each issue as they come up. That way we don’t write extra code for no
reason.
This is, after all, development that is driven by testing. So run the tests again. You
can rerun the tests by refreshing the spec runner page.
TDD Walkthrough - The Time in Words
Ok, different error message. Expected
undefined to be ‘five o’ clock’.
Well, the solution sounds simple. Make our
function return five o’ clock!
TDD Walkthrough - The Time in Words
This solution will make you cringe. It makes me cringe.
But you never write more code than you need. That prevents you from wasting
time on extra code, and it prevents you from writing code that can break other
things in other ways.
Focus on minimal design, and drive your code towards the functionality you want
with tests.
TDD Walkthrough - The Time in Words
Ok, that being said, our current test should pass.
Side note: We could implement all of the o’
clocks from 1 to 12. But in the interest of
keeping the blog shorter, I will focus on the
examples on the test list I decided on in the
beginning. I will still keep the code in a flexible
manner to allow for implementation of hours 1 to
12.
TDD Walkthrough - The Time in Words
So, with the five o’ clock case done, let’s pick
the next case. Preferably an easy one, like 5:30.
Our new test will fail, while our previous test is
still passing as shown in the top left of the test
runner. This is also important! Whatever you do
to your code, don’t break the old tests while
getting the new one to work!
TDD Walkthrough - The Time in Words
So, that being said, how do we make our
function do both of these? Looks like an if
statement will solve that problem.
And run our tests again, they should be passing
now.
TDD Walkthrough - The Time in Words
We have an opportunity to make our code cleaner! Whenever your tests are
passing and you are in a good state, you can look back at your code and look for
ways to improve it.
Removing duplication is one of those ways to clean up code. Duplication here
means using the same thing or doing the same thing in more than one place.
TDD Walkthrough - The Time in Words
Where is the duplication?
We use the word ‘five’ in both our strings. That
word will change when we do other hours in the
day, so this looks like a good opportunity for
refactoring.
TDD Walkthrough - The Time in Words
Extract the ‘five’ in the strings to a local variable.
Rerun those tests. They had better still be
passing! As long as they are passing, we can
continue.
What next? Let’s translate 5:15 to quarter past
five. Write the test first. Make sure it is failing.
TDD Walkthrough - The Time in Words
Much like last time, we can write an if statement
for the quarter past five case. Thanks to our
refactoring from before, we can reuse the hour
variable for our return.
Tests are passing again! Is there an opportunity
to clean the code?
TDD Walkthrough - The Time in Words
The return from the ‘half past’ and the ‘quarter
past’ look awfully similar. The only difference is
the ‘quarter’ vs the ‘half’. Let’s extract the ‘past’
out to local variable, as a step towards cleaning
the code.
We have changed the code, so we must run the
tests! They are still passing, so we may
continue.
TDD Walkthrough - The Time in Words
There is a type of duplication here as well! The
‘quarter past’ scenario and the ‘half past’
scenario look rather similar, the difference is in
the minutes past the hour.
The way to cleaner code may not be obvious
here. There is a duplication of intent in terms of
the minutes past the hour. Let’s turn the ‘quarter’
and the ‘hour’ into variables, and maybe the way
to cleaner code will become more obvious.
TDD Walkthrough - The Time in Words
How about that? They return just about the
same thing. Let’s move the returns down, to
remove the duplication.
Ah, but there is a problem. The return for the
hour o’ clock scenario conflicts if we move the
minutes past hour return down. Let’s find a way
to move the hour o’ clock return up so it doesn’t
conflict. Will an if statement change anything?
TDD Walkthrough - The Time in Words
Make the change. Run the tests. Everything is
green. The hour o’ clock if block can be moved
up now, with less fear of breaking things.
TDD Walkthrough - The Time in Words
That move didn’t break anything. Running our
tests will tell us that.
Now we should be able to move the minutes
past hour returns down without conflict.
TDD Walkthrough - The Time in Words
Quite a few changes were made. Run the tests,
make sure they pass, and continue on.
What should our next test be? Since we are
returning minutes past hour, we can probably
implement one minute past and ten minute past.
Let’s start with one minute past.
TDD Walkthrough - The Time in Words
Run the test to see it failing. Now, how to solve
this? An if statement would solve this problem
easily.
I’ll put the one minute past scenario between the
hour o’ clock and the quarter past scenario, so
that the scenarios are in ascending order by
minutes.
Organization does make code easier to read.
TDD Walkthrough - The Time in Words
Ok, tests should be passing.
But now our code doesn’t look so good with so
many if statements. We should clean those up.
What can we do about them? For one thing,
they only seem to care about the minutes. Let’s
change the if conditional to reflect that. The
javascript slice function will work here.
There is a pattern here. If the minutes of the
input is this, minutes is set to that. There are 3
pairs of this pattern.
TDD Walkthrough - The Time in Words
This pattern can be refactored into a key/value
type data structure. Some people call it
dictionaries, some people call it look ups. In
javascript, we can just use an object. Write up
the object first with its pairs.
Then replace the minutes strings with the
appropriate key from the minutes conversion
object.
TDD Walkthrough - The Time in Words
What I am doing here falls under a technique where you first create the
duplication, migrate your code from one version to the other version, then remove
the duplication.
In doing so, you migrate your code from one structure to another structure in a
step-by-step process. This method lets you catch errors while they are small. Feel
free to run tests continuously while making the changes.
TDD Walkthrough - The Time in Words
Now all these if statements look about the same!
Who needs the if statements anymore. Just
remove them.
TDD Walkthrough - The Time in Words
Pretty impressive, how so much code collapsed
into the one line. And all of this was done in an
incremental manner using certain techniques.
But wait, there’s more! What happens when we
try to add the next test case, ten minutes past
five?
TDD Walkthrough - The Time in Words
How much code would it take to pass this test?
Just a key value pair. Our code is very clean
and easy to change. That’s good news,
considering that our next few test cases will be
more interesting. Let’s do quarter to six first.
TDD Walkthrough - The Time in Words
Write the test and see it fail.
Quarter to six doesn’t seem to fit well into our
current code. Looks like we’ll have to write an if
statement for this. We’ll figure out how to make
it cleaner later.
TDD Walkthrough - The Time in Words
Tests should be passing now, but our code isn’t
as clean as before. We can take some small
steps to making it cleaner. We can change the
conditional to check minutes. And we can add
45 minutes as a key value pair.
It’s a small improvement. Having something
better than before is fine. Our upcoming test
cases will make the cleaner code more obvious
too.
TDD Walkthrough - The Time in Words
Tests should be passing now, but our code isn’t
as clean as before. We can take some small
steps to making it cleaner. We can change the
conditional to check minutes. And we can add
45 minutes as a key value pair.
It’s a small improvement. Having something
better than before is fine. Our upcoming test
cases will make the cleaner code more obvious
too.
TDD Walkthrough - The Time in Words
Let’s do 5:40, and run the tests to see it fail.
We can use some of our existing structure for
this scenario. We can add 40 to the minutes
conversion, and we can use our 45 minute
scenario to implement the 40 minute scenario
too. Not bad, but still a little bulky looking. Let’s
clean up a bit. When do we use the minutes to
six logic? When minutes is past 30. So let’s
replace the if conditional with that.
TDD Walkthrough - The Time in Words
Make the change. Run the tests and make sure
you don’t break anything. Now that’s better.
What other patterns are there? When minutes is
past 30, you say ‘to’ and ‘six’. Otherwise you
say ‘past’ and ‘five’. That sounds like something
we can implement with variables and logic.
TDD Walkthrough - The Time in Words
At this point, the return inside the if block looks
quite similar to the return at the end. Let’s make
them exactly the same. Then remove the return
from the if block. Theoretically everything should
still pass!
And they do pass! Our code doesn’t look so
bad, and we’ve hit all the major test cases. The
remaining test cases fit into our code very well.
Just add some key value pairs to the minutes
conversion object.
TDD Walkthrough - The Time in Words
Now, there is more duplication to remove and other refactorings to do to improve
the code quality, but I will leave that to you guys. This walkthrough illustrates how I
solve this problem in somewhat small steps. What I didn't dive into was why, or
how I knew that step was the next small step. I believe that the whys and hows
are best acquired through personal experience. So I encourage you guys to get
out there and try it! Good Luck!
TDD Walkthrough - The Time in Words
For those who want to look at the source code, I have this walkthrough posted on
github here:
https://github.com/khapeterk/TDDWalkthroughTheTimeInWords

More Related Content

What's hot

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
Tung Nguyen Thanh
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
Jeremy Kendall
 
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael KuehneTDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
Michael Kuehne-Schlinkert
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
Lars Thorup
 
TDD and Simple Design Workshop - Session 1 - November 2018
TDD and Simple Design Workshop - Session 1 - November 2018TDD and Simple Design Workshop - Session 1 - November 2018
TDD and Simple Design Workshop - Session 1 - November 2018
Paulo Clavijo
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
Nguyễn Đào Thiên Thư
 
TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
David Rodenas
 
8th Alg--Dec14
8th Alg--Dec148th Alg--Dec14
8th Alg--Dec14
jdurst65
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Zohirul Alam Tiemoon
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
Donny Wals
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
Vijay Kumbhar
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
Dionatan default
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guy_davis
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 

What's hot (15)

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
 
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael KuehneTDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
TDD for APIs @ Europython 2015, Bilbao by Michael Kuehne
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
TDD and Simple Design Workshop - Session 1 - November 2018
TDD and Simple Design Workshop - Session 1 - November 2018TDD and Simple Design Workshop - Session 1 - November 2018
TDD and Simple Design Workshop - Session 1 - November 2018
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
8th Alg--Dec14
8th Alg--Dec148th Alg--Dec14
8th Alg--Dec14
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 

Similar to TDD Walkthrough - The Time in Words

TDD Walkthrough - Library Fine
TDD Walkthrough - Library FineTDD Walkthrough - Library Fine
TDD Walkthrough - Library Fine
PeterKha2
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
FalafelSoftware
 
TDD & Refactoring
TDD & RefactoringTDD & Refactoring
TDD & Refactoring
Hernan Wilkinson
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Abdelkrim Boujraf
 
Intro to TDD
Intro to TDDIntro to TDD
Intro to TDD
Jason Nocks
 
Pairing w developers_stpconpics
Pairing w developers_stpconpicsPairing w developers_stpconpics
Pairing w developers_stpconpics
Lanette Creamer
 
Lập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven developmentLập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven development
Anh Lê
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
Tdd - Test Driven Development
Tdd - Test Driven DevelopmentTdd - Test Driven Development
Tdd - Test Driven Development
David Paluy
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
Wen-Tien Chang
 
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
Brian Brazil
 
Testacular
TestacularTestacular
Testacular
James Ford
 
Wait A Moment? How High Workload Kills Efficiency! - Roman Pickl
Wait A Moment? How High Workload Kills Efficiency! - Roman PicklWait A Moment? How High Workload Kills Efficiency! - Roman Pickl
Wait A Moment? How High Workload Kills Efficiency! - Roman Pickl
PROIDEA
 
Data Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdData Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ Scribd
Formulatedby
 
wtst3_pettichord3
wtst3_pettichord3wtst3_pettichord3
wtst3_pettichord3
tutorialsruby
 
wtst3_pettichord3
wtst3_pettichord3wtst3_pettichord3
wtst3_pettichord3
tutorialsruby
 
Understanding Why Testing is Importaint
Understanding Why Testing is ImportaintUnderstanding Why Testing is Importaint
Understanding Why Testing is Importaint
Sana Nasar
 
TDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happinessTDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happiness
Erez Cohen
 
Testing smells
Testing smellsTesting smells
Testing smells
Sidu Ponnappa
 
A Tale of Two Workflows - ChefConf 2014
A Tale of Two Workflows - ChefConf 2014A Tale of Two Workflows - ChefConf 2014
A Tale of Two Workflows - ChefConf 2014
Pete Cheslock
 

Similar to TDD Walkthrough - The Time in Words (20)

TDD Walkthrough - Library Fine
TDD Walkthrough - Library FineTDD Walkthrough - Library Fine
TDD Walkthrough - Library Fine
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
 
TDD & Refactoring
TDD & RefactoringTDD & Refactoring
TDD & Refactoring
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
 
Intro to TDD
Intro to TDDIntro to TDD
Intro to TDD
 
Pairing w developers_stpconpics
Pairing w developers_stpconpicsPairing w developers_stpconpics
Pairing w developers_stpconpics
 
Lập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven developmentLập trình hướng kiểm thử - Test Driven development
Lập trình hướng kiểm thử - Test Driven development
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Tdd - Test Driven Development
Tdd - Test Driven DevelopmentTdd - Test Driven Development
Tdd - Test Driven Development
 
BDD style Unit Testing
BDD style Unit TestingBDD style Unit Testing
BDD style Unit Testing
 
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
Staleness and Isolation in Prometheus 2.0 (PromCon 2017)
 
Testacular
TestacularTestacular
Testacular
 
Wait A Moment? How High Workload Kills Efficiency! - Roman Pickl
Wait A Moment? How High Workload Kills Efficiency! - Roman PicklWait A Moment? How High Workload Kills Efficiency! - Roman Pickl
Wait A Moment? How High Workload Kills Efficiency! - Roman Pickl
 
Data Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdData Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ Scribd
 
wtst3_pettichord3
wtst3_pettichord3wtst3_pettichord3
wtst3_pettichord3
 
wtst3_pettichord3
wtst3_pettichord3wtst3_pettichord3
wtst3_pettichord3
 
Understanding Why Testing is Importaint
Understanding Why Testing is ImportaintUnderstanding Why Testing is Importaint
Understanding Why Testing is Importaint
 
TDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happinessTDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happiness
 
Testing smells
Testing smellsTesting smells
Testing smells
 
A Tale of Two Workflows - ChefConf 2014
A Tale of Two Workflows - ChefConf 2014A Tale of Two Workflows - ChefConf 2014
A Tale of Two Workflows - ChefConf 2014
 

Recently uploaded

原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Envertis Software Solutions
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 

Recently uploaded (20)

原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 

TDD Walkthrough - The Time in Words

  • 1. TDD Walkthrough The Time in Words Peter Kha: peter.kha@greatersum.com
  • 2. TDD Walkthrough - The Time in Words Hello and welcome to my TDD Walkthrough! Today’s exercise is The Time in Words, taken from HackerRank.com at the following URL: https://www.hackerrank.com/challenges/the- time-in-words/problem
  • 3. TDD Walkthrough - The Time in Words In these TDD Walkthroughs, I assume that my readers have basic knowledge of test driven development. I highly suggest having an understanding of TDD before going through this walkthrough. The following blog post explains the cycles of TDD. http://blog.cleancoder.com/uncle- bob/2014/12/17/TheCyclesOfTDD.html I also assume that my readers have knowledge of refactoring tools and terminology. If you need more information, this blog post on the six core refactorings will provide enough information. http://arlobelshee.com/the-core-6-refactorings/
  • 4. TDD Walkthrough - The Time in Words For this exercise, I have the following already set up: Chrome browser Visual Studio Code Jasmine testing framework (see my other blog on setting up: https://www.greatersum.com/tdd-setup-jasmine/) With the above already set up, let’s get started!
  • 5. TDD Walkthrough - The Time in Words I would normally brainstorm a test list of all the functionality I expect out of my code. Since the exercise provides good examples of that functionality, I will use those examples as my test list. My first step will then be to implement the simplest test case, and for that I choose 5:00, which translates to “five o’ clock”. Before that though, we must set up our project. Creating files, pointing to file locations, and running for the first time.
  • 6. TDD Walkthrough - The Time in Words Starting with a basic Jasmine standalone project, I will create the file that contains my tests.
  • 7. TDD Walkthrough - The Time in Words There are various conventions for file names out there. I tend to use the affectionately dubbed kabob-case: the-time-in-words.spec.js
  • 8. TDD Walkthrough - The Time in Words This is a spec file. This file needs to be included in the Jasmine spec runner, so open up the SpecRunner.html file next. Look for the comment that says “include spec files here”, and include the spec file you just created.
  • 9. TDD Walkthrough - The Time in Words Let’s write our first test. First, we create the test suite: And then we write the test function: In our test function, we write what we are expecting:
  • 10. TDD Walkthrough - The Time in Words At this point, you should be able to run your test runner HTML file by opening it in the browser, and see a failing test. Failing the test here is good! You haven’t written any production code yet, and that’s ok. What you did write is a test that will tell you when your code fulfilled a purpose, and that is to translate 5:00 to five o’ clock. It’s not much, but we take one step at a time.
  • 11. TDD Walkthrough - The Time in Words Ok, so what does the test runner say? theTimeInWords is not defined. That makes sense, we haven’t written anything called theTimeInWords yet. Let’s go make that function!
  • 12. TDD Walkthrough - The Time in Words To keep the code organized well, the tests are contained in the spec file, in the spec folder. So then our source code should go in the source folder!
  • 13. TDD Walkthrough - The Time in Words Our new source file will need to be included in the Spec Runner though, so let’s not forget to do that.
  • 14. TDD Walkthrough - The Time in Words Now back to our source file. The first issue is that theTimeInWords is not defined, so let’s go define it.
  • 15. TDD Walkthrough - The Time in Words You might be tempted to go implement all of the function right now. But bear with me and take one small step at a time. Let’s focus on the error at hand, and we’ll tackle each issue as they come up. That way we don’t write extra code for no reason. This is, after all, development that is driven by testing. So run the tests again. You can rerun the tests by refreshing the spec runner page.
  • 16. TDD Walkthrough - The Time in Words Ok, different error message. Expected undefined to be ‘five o’ clock’. Well, the solution sounds simple. Make our function return five o’ clock!
  • 17. TDD Walkthrough - The Time in Words This solution will make you cringe. It makes me cringe. But you never write more code than you need. That prevents you from wasting time on extra code, and it prevents you from writing code that can break other things in other ways. Focus on minimal design, and drive your code towards the functionality you want with tests.
  • 18. TDD Walkthrough - The Time in Words Ok, that being said, our current test should pass. Side note: We could implement all of the o’ clocks from 1 to 12. But in the interest of keeping the blog shorter, I will focus on the examples on the test list I decided on in the beginning. I will still keep the code in a flexible manner to allow for implementation of hours 1 to 12.
  • 19. TDD Walkthrough - The Time in Words So, with the five o’ clock case done, let’s pick the next case. Preferably an easy one, like 5:30. Our new test will fail, while our previous test is still passing as shown in the top left of the test runner. This is also important! Whatever you do to your code, don’t break the old tests while getting the new one to work!
  • 20. TDD Walkthrough - The Time in Words So, that being said, how do we make our function do both of these? Looks like an if statement will solve that problem. And run our tests again, they should be passing now.
  • 21. TDD Walkthrough - The Time in Words We have an opportunity to make our code cleaner! Whenever your tests are passing and you are in a good state, you can look back at your code and look for ways to improve it. Removing duplication is one of those ways to clean up code. Duplication here means using the same thing or doing the same thing in more than one place.
  • 22. TDD Walkthrough - The Time in Words Where is the duplication? We use the word ‘five’ in both our strings. That word will change when we do other hours in the day, so this looks like a good opportunity for refactoring.
  • 23. TDD Walkthrough - The Time in Words Extract the ‘five’ in the strings to a local variable. Rerun those tests. They had better still be passing! As long as they are passing, we can continue. What next? Let’s translate 5:15 to quarter past five. Write the test first. Make sure it is failing.
  • 24. TDD Walkthrough - The Time in Words Much like last time, we can write an if statement for the quarter past five case. Thanks to our refactoring from before, we can reuse the hour variable for our return. Tests are passing again! Is there an opportunity to clean the code?
  • 25. TDD Walkthrough - The Time in Words The return from the ‘half past’ and the ‘quarter past’ look awfully similar. The only difference is the ‘quarter’ vs the ‘half’. Let’s extract the ‘past’ out to local variable, as a step towards cleaning the code. We have changed the code, so we must run the tests! They are still passing, so we may continue.
  • 26. TDD Walkthrough - The Time in Words There is a type of duplication here as well! The ‘quarter past’ scenario and the ‘half past’ scenario look rather similar, the difference is in the minutes past the hour. The way to cleaner code may not be obvious here. There is a duplication of intent in terms of the minutes past the hour. Let’s turn the ‘quarter’ and the ‘hour’ into variables, and maybe the way to cleaner code will become more obvious.
  • 27. TDD Walkthrough - The Time in Words How about that? They return just about the same thing. Let’s move the returns down, to remove the duplication. Ah, but there is a problem. The return for the hour o’ clock scenario conflicts if we move the minutes past hour return down. Let’s find a way to move the hour o’ clock return up so it doesn’t conflict. Will an if statement change anything?
  • 28. TDD Walkthrough - The Time in Words Make the change. Run the tests. Everything is green. The hour o’ clock if block can be moved up now, with less fear of breaking things.
  • 29. TDD Walkthrough - The Time in Words That move didn’t break anything. Running our tests will tell us that. Now we should be able to move the minutes past hour returns down without conflict.
  • 30. TDD Walkthrough - The Time in Words Quite a few changes were made. Run the tests, make sure they pass, and continue on. What should our next test be? Since we are returning minutes past hour, we can probably implement one minute past and ten minute past. Let’s start with one minute past.
  • 31. TDD Walkthrough - The Time in Words Run the test to see it failing. Now, how to solve this? An if statement would solve this problem easily. I’ll put the one minute past scenario between the hour o’ clock and the quarter past scenario, so that the scenarios are in ascending order by minutes. Organization does make code easier to read.
  • 32. TDD Walkthrough - The Time in Words Ok, tests should be passing. But now our code doesn’t look so good with so many if statements. We should clean those up. What can we do about them? For one thing, they only seem to care about the minutes. Let’s change the if conditional to reflect that. The javascript slice function will work here. There is a pattern here. If the minutes of the input is this, minutes is set to that. There are 3 pairs of this pattern.
  • 33. TDD Walkthrough - The Time in Words This pattern can be refactored into a key/value type data structure. Some people call it dictionaries, some people call it look ups. In javascript, we can just use an object. Write up the object first with its pairs. Then replace the minutes strings with the appropriate key from the minutes conversion object.
  • 34. TDD Walkthrough - The Time in Words What I am doing here falls under a technique where you first create the duplication, migrate your code from one version to the other version, then remove the duplication. In doing so, you migrate your code from one structure to another structure in a step-by-step process. This method lets you catch errors while they are small. Feel free to run tests continuously while making the changes.
  • 35. TDD Walkthrough - The Time in Words Now all these if statements look about the same! Who needs the if statements anymore. Just remove them.
  • 36. TDD Walkthrough - The Time in Words Pretty impressive, how so much code collapsed into the one line. And all of this was done in an incremental manner using certain techniques. But wait, there’s more! What happens when we try to add the next test case, ten minutes past five?
  • 37. TDD Walkthrough - The Time in Words How much code would it take to pass this test? Just a key value pair. Our code is very clean and easy to change. That’s good news, considering that our next few test cases will be more interesting. Let’s do quarter to six first.
  • 38. TDD Walkthrough - The Time in Words Write the test and see it fail. Quarter to six doesn’t seem to fit well into our current code. Looks like we’ll have to write an if statement for this. We’ll figure out how to make it cleaner later.
  • 39. TDD Walkthrough - The Time in Words Tests should be passing now, but our code isn’t as clean as before. We can take some small steps to making it cleaner. We can change the conditional to check minutes. And we can add 45 minutes as a key value pair. It’s a small improvement. Having something better than before is fine. Our upcoming test cases will make the cleaner code more obvious too.
  • 40. TDD Walkthrough - The Time in Words Tests should be passing now, but our code isn’t as clean as before. We can take some small steps to making it cleaner. We can change the conditional to check minutes. And we can add 45 minutes as a key value pair. It’s a small improvement. Having something better than before is fine. Our upcoming test cases will make the cleaner code more obvious too.
  • 41. TDD Walkthrough - The Time in Words Let’s do 5:40, and run the tests to see it fail. We can use some of our existing structure for this scenario. We can add 40 to the minutes conversion, and we can use our 45 minute scenario to implement the 40 minute scenario too. Not bad, but still a little bulky looking. Let’s clean up a bit. When do we use the minutes to six logic? When minutes is past 30. So let’s replace the if conditional with that.
  • 42. TDD Walkthrough - The Time in Words Make the change. Run the tests and make sure you don’t break anything. Now that’s better. What other patterns are there? When minutes is past 30, you say ‘to’ and ‘six’. Otherwise you say ‘past’ and ‘five’. That sounds like something we can implement with variables and logic.
  • 43. TDD Walkthrough - The Time in Words At this point, the return inside the if block looks quite similar to the return at the end. Let’s make them exactly the same. Then remove the return from the if block. Theoretically everything should still pass! And they do pass! Our code doesn’t look so bad, and we’ve hit all the major test cases. The remaining test cases fit into our code very well. Just add some key value pairs to the minutes conversion object.
  • 44. TDD Walkthrough - The Time in Words Now, there is more duplication to remove and other refactorings to do to improve the code quality, but I will leave that to you guys. This walkthrough illustrates how I solve this problem in somewhat small steps. What I didn't dive into was why, or how I knew that step was the next small step. I believe that the whys and hows are best acquired through personal experience. So I encourage you guys to get out there and try it! Good Luck!
  • 45. TDD Walkthrough - The Time in Words For those who want to look at the source code, I have this walkthrough posted on github here: https://github.com/khapeterk/TDDWalkthroughTheTimeInWords