Prime Factors Code Kata
(Practicing TDD in PHP)
ViennaPHP, Sept. 2014
Peter Kofler, ‘Code Cop’
@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional Software
Developer for 15 years
• “fanatic about code quality”
• I help development teams
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Deliberate
Practice
How do musicians practice?
Then how do coders practice?
Code Kata
Code Kata Definition
●
A kata as a detailed choreographed
pattern of movement.
●
A code kata is an exercise in
programming which helps a
programmer hone their skills through
practice and repetition.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Code Kata
●
simple problem (max. 20 minutes)
●
solve every day
●
memorization
●
create reflexes
●
experiment with solutions
●
stretch yourself
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Why repeat the same kata?
●
de-emphasise the generation of code
●
concentrate on
●
the process of writing the code
●
naming test cases
●
the Red/Green cycle
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Don't Focus on
Getting it Done.
F0cus on Doing
It Perfectly.
The Prime Factors
Kata
What Exactly Will We Do?
●
write code together
●
using TDD
●
see techniques
●
discuss
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
The Requirements.
• Write a class named “PrimeFactors”
that has one static method: generate.
●
The generate method takes an integer
argument and returns a List<Integer>.
●
That list contains the prime factors in
numerical sequence.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata
First Some Math.
●
Prime Number: number > 1 that has
no divisors other than 1 and itself.
●
e.g. 2, 3, 5, 61, 67, ..., 997, ..., 243112609
-1
●
Prime Factors: prime numbers that
divide an integer without remainder.
●
e.g. 2 = 2,
4 = 2 * 2,
24 = 2 * 2 * 2 * 3
288 = 25
* 32
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Don’t
Hurt
Me!
Demo
See step by step code: https://bitbucket.org/pkofler/primefactors.php
Unit Testing
• test individual units
• isolate each part
• show that individual parts are correct
• regression testing
• sort of living documentation
• executed within a framework
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Test-Driven Development
●
add a test
●
run all tests and see if the new one fails
●
write some code
●
run all tests and see them succeed
●
refactor code mercilessly
●
„Red Green Refactor“
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
“I write unit tests for one
reason: so my coworkers
don't f*** up my code.”
(David Angry)
Coding Dojo Vienna
●
Biweekly Coding Dojo in Vienna
●
Every other Wednesday 6PM
●
Changing locations
●
Follow
#CodingDojoVie
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
http://codingdojovie.github.io/
Global Day of Code Retreat
●
A world-wide event celebrating passion
and software craftsmanship.
●
GDCR 2014:
15th November
●
Follow #GDCR14
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
http://globalday.coderetreat.org/
Peter Kofler
@codecopkofler
www.code-cop.org
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
CC Images
●
karate
http://www.flickr.com/photos/the-consortium/5301698212/
●
music
https://www.flickr.com/photos/jeremyhiebert/11474303315
●
code https://www.flickr.com/photos/slworking/7726767220/
●
questions http://www.flickr.com/photos/oberazzi/318947873/
●
puppy http://www.flickr.com/photos/davdunc/3706558237/
●
dojo
http://www.flickr.com/photos/49715404@N00/3267627038/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Prime Factors Code Kata - Practicing TDD (2014)

  • 1.
    Prime Factors CodeKata (Practicing TDD in PHP) ViennaPHP, Sept. 2014 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2.
    Peter Kofler • Ph.D.(Appl. Math.) • Professional Software Developer for 15 years • “fanatic about code quality” • I help development teams PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 3.
  • 4.
  • 5.
    Then how docoders practice?
  • 6.
  • 7.
    Code Kata Definition ● Akata as a detailed choreographed pattern of movement. ● A code kata is an exercise in programming which helps a programmer hone their skills through practice and repetition. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 8.
    Code Kata ● simple problem(max. 20 minutes) ● solve every day ● memorization ● create reflexes ● experiment with solutions ● stretch yourself PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 9.
    Why repeat thesame kata? ● de-emphasise the generation of code ● concentrate on ● the process of writing the code ● naming test cases ● the Red/Green cycle PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 10.
    Don't Focus on Gettingit Done. F0cus on Doing It Perfectly.
  • 11.
  • 12.
    What Exactly WillWe Do? ● write code together ● using TDD ● see techniques ● discuss PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 13.
    The Requirements. • Writea class named “PrimeFactors” that has one static method: generate. ● The generate method takes an integer argument and returns a List<Integer>. ● That list contains the prime factors in numerical sequence. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata
  • 14.
    First Some Math. ● PrimeNumber: number > 1 that has no divisors other than 1 and itself. ● e.g. 2, 3, 5, 61, 67, ..., 997, ..., 243112609 -1 ● Prime Factors: prime numbers that divide an integer without remainder. ● e.g. 2 = 2, 4 = 2 * 2, 24 = 2 * 2 * 2 * 3 288 = 25 * 32 PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 15.
  • 16.
    Demo See step bystep code: https://bitbucket.org/pkofler/primefactors.php
  • 17.
    Unit Testing • testindividual units • isolate each part • show that individual parts are correct • regression testing • sort of living documentation • executed within a framework PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 18.
    Test-Driven Development ● add atest ● run all tests and see if the new one fails ● write some code ● run all tests and see them succeed ● refactor code mercilessly ● „Red Green Refactor“ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 19.
    “I write unittests for one reason: so my coworkers don't f*** up my code.” (David Angry)
  • 20.
    Coding Dojo Vienna ● BiweeklyCoding Dojo in Vienna ● Every other Wednesday 6PM ● Changing locations ● Follow #CodingDojoVie PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY http://codingdojovie.github.io/
  • 21.
    Global Day ofCode Retreat ● A world-wide event celebrating passion and software craftsmanship. ● GDCR 2014: 15th November ● Follow #GDCR14 PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY http://globalday.coderetreat.org/
  • 22.
    Peter Kofler @codecopkofler www.code-cop.org PETER KOFLER,CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 23.
    CC Images ● karate http://www.flickr.com/photos/the-consortium/5301698212/ ● music https://www.flickr.com/photos/jeremyhiebert/11474303315 ● code https://www.flickr.com/photos/slworking/7726767220/ ● questionshttp://www.flickr.com/photos/oberazzi/318947873/ ● puppy http://www.flickr.com/photos/davdunc/3706558237/ ● dojo http://www.flickr.com/photos/49715404@N00/3267627038/ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY