Friday, 9 March 2012

Write Less Code

I write too much code - I always have. Mathematicians have long understood the value of finding the smallest, most elegant solution to a problem. Unfortunately I think too many of us in my industry get carried away with the action of programming rather than the more difficult problem of truly understanding the problem.

For me, when faced with a new problem, it has always been easier to do some research with the debugger then sit down and start banging away on the keyboard with a rough mental plan of how to proceed. During the hacking phase I will often delete and rewrite code many times as new information comes to light but also as my brain works through the problem.

I don't really have a problem with this way of working in the games industry. The better you get the faster you converge on an acceptable solution. It is very difficult to solve a big problem in one go, I think mainly because it is difficult to understand the problem. Existing code generally makes the problem worse, how much worse depends on how inter-dependent the modules are. More dependencies increase the size of the problem space.

However the key to making this hacking work long term for the team is that there needs to be a culling phase before you submit. This is when you understand the problem, you have a working solution, now boil it down to only the bits that are needed. Ensure you are truly happy with everything you check into the code base. Badly named class? Now is the time to rename it! Big function? Break it up or simply examine if it's needed at all. Massive redundant confusing class hierarchy? Maybe you have bigger problems :)

It can sometimes be hard to see past the implementation you came up with. I don't have much advice for this other than imagine you are explaining your solution to someone else. We have code reviews before every check in at work which are great for this very purpose.

Sorry I don't have any examples to hand which means this post is pretty dry. Something juicer next time!

No comments:

Post a comment