Prime Factors Code Kata - Practicing TDD (2014)

23
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.

description

PrimeFactors Code Kata as performed during my presentation on katas at ViennaPHP.

Transcript of Prime Factors Code Kata - Practicing TDD (2014)

Page 1: Prime Factors Code Kata - Practicing TDD (2014)

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.

Page 2: Prime Factors Code Kata - Practicing TDD (2014)

Peter Kofler

• Ph.D. (Appl. Math.)

• Professional SoftwareDeveloper for 15 years

• “fanatic about code quality”

• I help development teams

PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Page 3: Prime Factors Code Kata - Practicing TDD (2014)

DeliberatePractice

Page 4: Prime Factors Code Kata - Practicing TDD (2014)

How do musicians practice?

Page 5: Prime Factors Code Kata - Practicing TDD (2014)

Then how do coders practice?

Page 6: Prime Factors Code Kata - Practicing TDD (2014)

Code Kata

Page 7: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 8: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 9: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 10: Prime Factors Code Kata - Practicing TDD (2014)

Don't Focus on Getting it Done.F0cus on Doing

It Perfectly.

Page 11: Prime Factors Code Kata - Practicing TDD (2014)

The Prime FactorsKata

Page 12: Prime Factors Code Kata - Practicing TDD (2014)

What Exactly Will We Do?

● write code together● using TDD● see techniques● discuss

PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Page 13: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 14: Prime Factors Code Kata - Practicing TDD (2014)

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 * 3288 = 25 * 32

PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Page 15: Prime Factors Code Kata - Practicing TDD (2014)

Don’t Hurt Me!

Page 16: Prime Factors Code Kata - Practicing TDD (2014)

Demo

See step by step code: http://hg.code-cop.org/primefactors.php

Page 17: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 18: Prime Factors Code Kata - Practicing TDD (2014)

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

Page 19: Prime Factors Code Kata - Practicing TDD (2014)

“I write unit tests for one reason: so my coworkers don't f*** up my code.”

(David Angry)

Page 20: Prime Factors Code Kata - Practicing TDD (2014)

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/

Page 21: Prime Factors Code Kata - Practicing TDD (2014)

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/

Page 22: Prime Factors Code Kata - Practicing TDD (2014)

Peter Kofler

@codecopkofler

www.code-cop.org

PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Page 23: Prime Factors Code Kata - Practicing TDD (2014)

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