Tag Archives: kata - Page 2

The Refactoring only Constraint

Doing code kata is more fun with constraints. Once I heard about a very impressive one, but I was unable to find anything about it, so I decided to investigate and write about it on my own.

The constraint is simple: only refactoring can be done in the production code, in other words: any kind of new functionality shall be tested and implemented in test code and refactored to the production code.

Until now, I was unable to find any reasonable argument that favours this methodology in a production environment, however, doing code kata with this constraint really improves…

  • …knowledge about the refactoring capabilities of the tools being used
  • …the refactoring experience
  • …communication skills – if done in pairs
  • …craftsmanship and patience

My recommended steps for the first try:

  1. Find an easy code kata
  2. Write a small piece of functionality with TDD, but keep it in the test case
  3. Move that small piece of functionality to production code
  4. Write another small piece of functionality with TDD, but still keep the real and the test code in the test case
  5. If necessary, refactor the production code so that it can accept the new functionality
  6. Write an integration test case which tells how the production code shall behave after moving the new piece of code to the production
  7. Move that small piece of functionality to the production code
  8. Refactor the test cases, remove redundancy – for example, the test cases of steps 2 and 4
  9. Refactor the code and keep encapsulation in mind
  10. Continue with step 4, until everything is implemented

Of course, you can choose different ways, but pay attention to the importance of the integration test case. It makes sure that nothing is broken after the move operations.

While doing the refactoring, try to…

  • …use only the refactoring tools and assistance features of your IDE – this will help you learn the capabilities of your tool(s)
  • …use only documented refactoring techniques (Fowler – Refactoring, Kerievsky – Refactoring to Patterns) – this will help you gain lexical knowledge
  • …avoid adding new [helper] lines to the code, even if you know that you will remove them later – there is a good chance that these lines will remain in the code after all

As an example, I did Roy Osherove’s String Sum exercise, with some small changes:

  • The input is always one line and always valid
  • The separator is ‘,’

I’m using eclipse, and focusing on the most important parts of work, meaning that I’m covering only one angle in the following example: return the summary of an input such as “2,3,5″

The first functionality:

    @Test
    public void shouldSumAnArrayOfIntegers() {
        assertEquals(10, sumNumbers(new int[]{2, 3, 5}));
    }
 
    int sumNumbers(int[] numbers) {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        return sum;
    }

Now I’m creating a stringSum private field with the type StringSum, and moving the sumNumbers there with right click on the method -> Refactor… -> Move…

Now comes the parsing:

    @Test
    public void shouldConvertStringToIntArray() {
        assertArrayEquals(new int[]{2, 3, 5}, convert("2,3,5"));
    }
 
    int[] convert(String string) {
        String[] items = string.split(",");
        int[] numbers = new int[items.length];
        for (int i = 0; i < items.length; i++) {
            numbers[i] = Integer.parseInt(items[i]);
        }
        return numbers;
    }

So far everything is green, now comes the integration test case:

    @Test
    public void shouldPerformTheSumOnTheInputString() {
        assertEquals(10, stringSum.sum("2,3,5"));
    }

The sum() method shows up, makes the test code red (compilation failure), but with the quick fix (CTRL + 1) on the method, I can create it, and now it’s just the test assertion itself that fails. In order to finish, the convert() method is required, but it is still in the test code, and until I have a green bar, I’m not really allowed to change the code base. I am ignoring the integration test case for a minute to have a green bar, and moving the convert() method to the production code. After having the test case on board again, the bar is red, but using the content assist (CTRL + space) I’m inserting the convert() and sumNumber() calls:

    public int sum(String string) {
    	return sumNumbers(convert(string));
    }

Now everything is green, but the code is a bit ugly. The test code does not need the shouldSumAnArrayOfIntegers() and shouldConvertStringToIntArray() test cases, so I’m removing them, and making every method in the StringSum class private except the sum().

After using Refactor -> Inline…, and moving around some lines (ALT + up/down arrow) the StringSum has only one method, which looks like this:

    public int sum(String string) {
        int sum = 0;
        String[] items = string.split(",");
        for (int i = 0; i < items.length; i++) {
            sum += Integer.parseInt(items[i]);
        }
        return sum;
    }

The example above is very simple. After adding more functionality – like supporting more separators and having proper error handling -, the task became harder. It took me hours to finish it, but on the bright side, I’ve learnt new refactoring techniques.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)

Testing the Stack Implementation

I’ve created my own version of the exercise used during my first agile job interview in order to compare it to the result of the pair programming session of the job interview. The exercise was quite simple: implement a stack, and store the values in an array. The implementation is pretty straightforward, the code is available on github. One can ask: why an array, why not using the collection framework? The answer is simple: just for fun, and just for the exercise :-)

In order to store the necessary amount of values, the internal array needs to be enlarged from time to time. There is nothing wrong with this so far. But in order to avoid unnecessary memory loss, that internal array needs to be shrunk as well. The first version of my Stack implementation lacked this functionality. The user stories of the functionality request can be:

  • As a developer, I don’t want to reserve more memory for my stack implementation than necessary
  • As a customer, I don’t want to lose values after shrinking the internal container

The second user story is very straightforward, so let’s focus on the first one. User stories and previous test cases are available, so far so good, but how to test the user story? An even better question: how to write a test case first? I consider my Stack implementation as an end product, and as such, I believe that it should follow the encapsulation object oriented programming principle, and it shouldn’t be difficult to use. An obvious choice would be a reflection-based solution, but since I’m not a great fan of reflection, I started to test the code from different angles.

For better understanding, here comes the code being tested:

    private void shrinkContainerCapacity() {
        int[] extentedContainer = new int[container.length - 10];
        System.arraycopy(container, 0, extentedContainer, 0, container.length - 10);
        container = extentedContainer;
    }

The first user story suggests memory handling, so first let’s try out this angle. Memory usage can be checked with the following methods:

Using those methods I wrote this test case sketch:

    @Test
    public void shouldUseLessMemoryAfterShrinkingInternalContainer() {
 
        for (int attempts = 0; attempts < 15; attempts++) {
            for (int i = 0; i < 10000; i++) {
                stack.push(i);
            }
 
            long availableMemoryBefore = getAvailableMemory();
            for (int i = 0; i < 10000; i++) {
                stack.pop();
            }
 
            long availableMemoryAfter = getAvailableMemory();
            assertTrue(availableMemoryBefore < availableMemoryAfter);
        }
    }
 
    private long getAvailableMemory() {
        System.gc();
        System.runFinalization();
        System.gc();
        System.runFinalization();
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();
        return totalMemory - freeMemory;
    }

I’ve executed the test case several times and it never turned red. I was curious why, because it supposed to be work, isn’t it?

The javaDoc of System.gc() and System.runFinalization() states that: “…the Java Virtual Machine has made a best effort…“. Additionally Java Virtual Machine implementations may differ on operation system level: “…The aim is binary compatibility. Each particular host operating system needs its own implementation of the JVM and runtime. These JVMs interpret the bytecode semantically the same way, but the actual implementation may be different.” (source: Wikipedia).

Adding these statements up makes me nervous. The whole situation – using memory measurements for testing – starts looking black for me, so I did some measurements using the test code from above with different Java Virtual Machine implementations on different operating systems:

  • Running with eclipse 1.6 jre on Ubuntu Linux
  • Running with oracle’s 1.6 jre on Ubuntu Linux
  • Running with jrockit 1.6 jre on Ubuntu Linux
  • Running with oracle’s 1.6 jre on Windows XP

I’m interested how the assertTrue(availableMemoryBefore < availableMemoryAfter) will turn out in the different scenarios, so I’m measuring the availableMemoryBefore and availableMemoryAfter values, and calculate the SIGN of their difference: if the availableMemoryAfter is greater than availableMemoryBefore the SIGN will return 1, which is equal to a true test case and a green bar. In other words, after shrinking the array, the Stack uses less memory. Mission accomplished sort of say.

Before evaluating the results I have to note two things.  I’ve executed the test cases several times and I didn’t find any difference in their outputs, that’s the reason why I have only one measurement sheet for each case. Additionally I sometimes have the feeling that there is a slight difference when I’m running a java application/test case from eclipse and from command line using oracle’s jre, therefore I did the measurements with both of them separately. The detailed measurement values are available in this spreadsheet. Now the result:

Now my red bar is understandable. I’m using eclipse under Ubuntu Linux (and oracle’s jre), and according to the diagram above, there is less available memory after the first execution of the shrinking (again, I executed this particular measurement more than twenty times, and I always got the same result). If I had used jrockit, the problem above never would have turned out (the orange bars are always at 1, meaning that there is more available memory after shrinking in every case).

I would have stopped with the measurements here, unless my friend had asked me this: “Why did you have to call the System.gc() and System.runFinalization() twice?” It is a very good question. The more I call the System.gc() the more feasible is that the Java Virtual Machine will do garbage collection, which means that my application will use less memory, at least on paper.

Unfortunately experience shows otherwise. Have a look again at the javaDoc of System.gc(). In layman’s terms the System.gc() call is just a request to the Java Virtual Machine, and there is no guarantee that it will consider the call at all. According to my measurements below, the Java Virtual Machine considered it, but I had participated in a project several years ago, in which we had memory issues and no matter how often we called the System.gc() nothing really happened. The Java Virtual Machine felt that the time for garbage collection had not come yet, so it ignored our request.

So SIGN (before – after) looks this, when I’m calling the System.gc() and System.runFinalization() only once:

 

In this case I would still see a red bar with my setup, in several cases (7, 9, 11, 13 and 15) the SIGN is 0, meaning that the amount of available memory is the same before and after the shrinking. After having a close look there isn’t any option, which would make my bar green. I feel like I’m getting somewhere: checking memory usage as validation is not that promising as it was before I started measuring it. Let’s see the result without any System.gc() and System.runFinalization() calls in order to finish my measurements and provide more data for evaluation:

 

The result is not deterministic at all. Now I’m 100% sure that checking the memory is a dead end for me. I cannot garantee that my shouldUseLessMemoryAfterShrinkingInternalContainer() test case will produce the same output every time. Someone can use a different kind of machine for test case execution – for example Solaris, on which I didn’t do any measurement at all -, or can change my getAvailableMemory() helper method and execute less or more System.gc() calls. Unfortunately I need a different approach.

The next thing worth having a look at is injecting the array copy functionality. Unfortunately, the System.arraycopy() method is static, so I have to wrap it up, and inject the wrapper class into my Stack. The Stack is considered an end product, I cannot let the user take care of the dependency injection by herself, so I need a factory to do that:

public class StackFactory {
	public Stack create() {
		return new Stack(new ArrayCopyWrapper());
	}
}
// ...
public class ArrayCopyWrapper {
    public void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) {
        System.arraycopy(src, srcPos, dest, destPos, length);
    }
}

First let’s see the effect of this modification on the test code:

    @Test
    public void shouldShrinkInternalCapacity() {
        FakeArrayCopyWrapper arrayCopyWrapper = new FakeArrayCopyWrapper();
        stack = new Stack(arrayCopyWrapper);
 
        arrayCopyWrapper.setExpectations(0, 0, 10);
        for (int i = 0; i &lt; 20; i++) {
             stack.push(i);
        } 
 
        // Had to cut the for() loop in half
        // In order to be able to set two expectations
        arrayCopyWrapper.setExpectations(0, 0, 20);
        stack.push(20);
 
        // The same reason here
        arrayCopyWrapper.setExpectations(20, 10, 1);
        stack.pop();
 
        arrayCopyWrapper.setExpectations(20, 10, 15);
        for (int i = 20; i &gt;= 5; i--) {
           stack.pop();
        }
    }

I didn’t want to spend too much time on setting up and using mock libraries. I had the strange feeling that I didn’t have to put too much effort into this angle. A simple fake object will be just fine:

public class FakeArrayCopyWrapper extends ArrayCopyWrapper {
    private int srcPos;
    private int destPos;
    private int length;
 
    public void setExpectations(int srcPos, int destPos, int length) {
        this.srcPos = srcPos;
        this.destPos = destPos;
        this.length = length;
    }
 
    public void arraycopy(int srcPos, Object dest, int destPos, int length) {
        assertEquals(this.srcPos, srcPos);
        assertEquals(this.destPos, destPos);
        assertEquals(this.length, length);
    }
}

Although I wrote the shouldShrinkInternalCapacity() and FakeArrayCopyWrapper class, I don’t like them too much. The test case is complicated and the Fake is strange.

Additionally, when someone wants to instantiate my Stack she must do this:

Stack stack = new StackFactory().create()

But, this version seems to be more user friendly:

Stack stack = new Stack()

The first version is good for internal classes, but personally I don’t want my users to do complicated things like using wrapper factories for creating an object, when there is an easier way.

However there is one more thing. The ArrayCopyWrapper wraps only the System.arraycopy() call. If I executed a code coverage measurement using the shouldShrinkInternalCapacity() test case, the measurement would indicate that the shrinkContainerCapacity() method is 100% covered with shouldShrinkInternalCapacity() test case. This is not true. The current version of shouldShrinkInternalCapacity() does not test the command below, because it focuses only on the arraycopy:

    private void shrinkContainerCapacity() {
        //...
        //...
        container = extentedContainer;
    }

One can argue that the line above is indirectly tested by the other test cases.  This is true, but I would like to have focused test cases and correct coverage data.

This solution is not user friendly and I had to do things in testing which I don’t really like. This approach is another dead end for me, at least.

The last thing I can think of is checking the size of the internal container. I have the following options:

  • make it protected
  • create a protected method for getting its size
  • use reflection

The first two options harm encapsulation, which I have no intention to do, so another dead end.

I had no other option but to do the testing with reflection. I don’t like the current state of the test cases, but I have no other ideas. So I’m going to leave this post open, feel free to submit other ideas on how this feature can be tested. The code is available on github, and any suggestions and comments are welcome.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)

File Lottery Kata

When I do some coding, I use test driven approach, have lots of fake and mock objects, and I use dependency injection for putting everything together. I usually don’t work alone, and from time to time I have to introduce these methods to someone with less experience in this particular area. Unfortunately, it takes me a lot of time to find the right example for them, because, frankly, the existing solutions are not that lightweight: spring context with configuration, annotations, different mock libraries, or even fake objects where mocking is impossible.

So I came up with a simple code kata exercise, which allows one to

  • practice the usage of dependency injection
  • try out different dependency injection frameworks
  • practice faking and mocking
  • try out different mocking libraries

The kata is quite simple (15 minutes):

Implement an application which follows the iterator design pattern. This particular application receives a directory as argument, and at each consecutive call, it returns a file name from the directory that hasn’t been returned before, in a random order. When the application reaches the end of the content of the directory, it shall start over, again returning the directory contents in a random order. If the directory is empty, then every call shall return an empty string. If the argument is not a directory but a file, then its name shall be returned on each consecutive call.

The random order makes testing the application quite complicated. In order to do proper testing – have an expected random number in each test case – the random generator shall be injected and mocked (or faked). If you find the right way to do it, then this exercise is pretty straightforward.

A very simple solution with fake test objects is available on my github page.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)

Using Kata for Improvements

I used to keep coding dojos for my colleagues, and it was sometimes very hard to find the right topic. I recently rediscovered the code kata – I did it earlier, but stopped after a short period -, and got an idea. I’m going to use it for discovering areas where our teams can improve. The setup is very easy: I propose a certain kata to the others with a time constraint. While they are working – the participation is voluntary -, I’m hanging around and looking for certain clues. These clues and the discussions after the session will help me find the areas where we need to exercise more.

After they get used to the code kata style and are able to finish the exercise within the time frame, we go forward with some constraints. In the far future, they will be able to pick their own code kata and constraint, but first there are things which have to be re-learnt and practised more. Here are some of the usual problems:

  • Too much time spent in red
  • Too much thinking before actually doing something
  • Not following the ideas from the clean code book
  • Too large steps

My plan is to come out with good practices in order to show a way to improve these areas, but for now, let’s have a final look at the code kata setup:

  1. Present a coding exercise for the participants
  2. Give them a time frame and keep them informed about the elapsed time
  3. Walk around and watch out for things the guys are doing right and wrong
  4. When the time has elapsed, shortly discuss the experience, share ideas with each other (sharing is very important here)
  5. If there is time left, redo the session from the start

We are going to keep these sessions twice a week before the daily stand-up meetings. Usually this time of day is less productive, because not everybody is in the office, people are warming up, having their starting tea or coffee – the list goes on. It is also a good time for doing improvements, but I’ll talk about that in a different post.

One can ask why this style is different from others, and how it is going to help. I see it this way: if programmers have to perform a simple but challenging task under time pressure, they are focusing on it very much. They won’t bother that you are watching what they are doing, or that you are asking them short but focused questions. On the other hand, during pair programming, they may show a picture of themselves as they want to look like, or simply become shy. Time will tell how this is going to work. I’ll keep you posted.

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)