It can be fun to think hard about something. But sometimes we waste a lot more thought than we could to achieve the same goal and even better. Let's say that efficient use of thought resources is to choose the path that requires you the minimum thinking and effort to achieve the same goal or better.
I will try to bring a metaphor, because I am afraid a math or code example would be less intuitive.
Let's say you need to carry rocks from one place to another. You can pick them one by one, by hand, and carry them. In some cases this will be the best solution, but what if you need to carry rocks every day for the whole year? Wouldn't it be better to build or buy a wheelbarrow? Though, if you needed to carry a few works than maybe getting a wheelbarrow is not worthwhile.
In programming this can mean planning ahead how you are going to solve a problem, or researching for the right existing technology to use.
However, things are not as simple as that. Planning consume thought resources or effort. You might end up planning too much ahead or investing long hours in planning something you don't really know if it will work. This means there need to be a balance between planning and just trying things out. It might sound funny, but I believe you don't want to sweat too much most of the time you code. A game requires large amounts of code and work, and if every minute of it would require you a lot of thought and effort, you will quickly burn out. A lot of the time coding should be easy or with very little effort. Of course, sometimes it's tough. And sometimes it gets really tough, but you want to minimize the times it gets really tough.
The thing that inspired me to write this article is some code I had to write to build a tree from a data structure. This data structure is the result of a text file I parsed with boost::spirit(more about that in future post). In my first attempt I simply used a tool I had available, of building a tree. I forgot I had another tool built upon it that would make my life much easier.
The basic tool would call two virtual methods, build child and build sibling, and with those you are suppose to build the tree. The code I wrote for this tool was long, ugly and didn't work. After this failed attempt I realized I have a tool I built upon the basic tool. This tool allows me to add the edges of the tree. I have an AddEdge method that gets parent index, child index and node data. The implementation make sure all those edges I add will create a tree after I call BuildTree. A lot simpler.
The first code snippet is of the advanced tree building tool, and the second code snippet is of the basic building tool. Notice the code is not spartanized much, and you don't really need to try and understand it. Just try to see which one looks prettier. You might not see the code if you are not at the original blog website(because it's a lot of code, and because of different plug-ins), so for the code you should visit my blog.