Apr 1, 2009

How to get optimized code in Agile process.

These days I'm fascinated by the idea of Agile process. I have read the 500 page book, "Agile software development" 2002, in 4 days, and I borrowed several more books about agile and eXtreme Programming.
This morning when I was reading a book, "Clean Code", I was thinking what if clients want my program to be faster. The book mainly focuses on Maintainability and Readability of source code rather than Optimization. Generally optimization harms maintainability and increases complexity.

However the performance is very important for game programming, so that optimization is inevitable.
The book, "Clean Code", said, "Make your program work first and then make it right." I want to add here: "Make it work, make it right and make it fast".

How do we make a program work? It assumes that we uses "Test cases first", and we can make a program working for the test cases. And then we need to clean the code, making right for short.

What I want to suggest here is once we got the right code, we keep the right code as the "base code" and we build "Temporary Fast Codes". Since we have the right code, the behavior of the right code and the fast code must be identical. Thus we can use the right code as an automatic test case generator, for example:
GameComponent rightCode = new RightImpl();
GameComponent fastCode = new FastImpl();
for ( int i = 0; i < 1000; ++i )
assertEquals( rightCode.do(i), fastCode.do(i) );
The point is that we don't expect we can maintain the optimized fast code. It should be treated as "Temporary Code". When the requirements change, the right code will grow up while the temporary code doesn't. We throw the fast code away and we will use the right code until we get the temporary fast code again.

Following this idea, we keep the basic principle of Agile process and we can also get the optimized (temporary) code. In game development process, we may develop the optimized code only at the last phase of the process, so that we can focus solely on right code during most of iterations without worrying about optimization.

No comments: