Code Kata - Pager

A simple code kata - "Pager" - which could be used by those who try to learn programming, and especially for TDD beginners.

My colleague (hello Tomasz!) asked me recently whether I know about any "testing kata". Few minutes with Google gave me a number of websites (including http://codekata.pragprog.com/ and http://codingdojo.org/) which include some coding and TDD kata. In this blog post I would like to suggest a kata of my own device - "Pager".

What Is Pager?

This is a pager (from allegro.pl).

And this is also a pager (from brainyqoute.com).

And this? Yes, this is also a pager (from bitcomputer.pl).

And this one? This is an ugly pager, but still a pager.

< 5 / 24 >

The Kata

The kata is about the writing of the underlying data model, and NOT about the presentation. What you should create is a class (bunch of classes) which is smart enough to provide all the data required to actually render the links to pages.

So, the pager gets the following data as input:

  • total number of items - e.g. there were 239843279 websites about Justin Bieber found,
  • number of items per page - e.g. user would like to see 20 links on each search page
  • offset - e.g. 4680, which means that the user has already flipped through 234 pages (20 items each) in search for everlasting wisdom (bad news for you pal, you won't find any there!)
    • or page number - e.g. 234 (which in fact is the same information)

Given this data pager should be able to answer the following questions:

  • how many pages are there? - e.g. 239843279/20 ... eeee... well, you do the math, please,
  • should prev/next link be visible? - e.g. prev button should not be visible when user is on the first page,
  • which links to print? - e.g. when user is on 25th page, then links to 24, 25 and 26 should be printed,
  • which page is current? - so we could bold/highlight it's number (and remove the link).

And so on.

Piece of cake, right?

Of course the aim of this kata is not really to just code the solution but to code it in the right way. So, whatever the "right way" means to you (TDD?, BDD?, CDD?, DDD?, EDD?, FDD?, eh... whatever), please be true to it when doing the kata.

The Glory of Pager Code Kata

What I like about it:

  • it's language-agnostic,
  • everyone understands the purpose and mechanics behind,
  • it's collaborators-free (no mocking required),
  • it's real life,
  • it's short,
  • you can tailor it to your liking because there are many variants which provide different data and differ in complexity,
  • a lot of places for off-by-one errors,
  • many corner-cases,
  • there are many possible implementations

Ok people, keep on paging ... eh-um, I mean keep on coding! (and testing, don't forget about tests!)

Show Me Your Pager

...and I'll show you mine? Well, no.

But if you would like to share the results of your Pager-kata with me, that would be nice. I can't promise anything, but I might give you some hints on improving your tests.

The risk is that your code can end up in the Bad Tests, Good Test book. You have been warned! :)

BTW. I do not care about the language you use ...with the exception of brainfuck! ;)

 
 
 
This used to be my blog. I moved to http://tomek.kaczanowscy.pl long time ago.

 
 
 

Please comment using