Theory vs. Practice in coding


When working on a project, I sometimes find myself stuck – thinking about some implementation detail, such as how to tell my admin-module which other modules are installed in my CMS (but that’s another story) – and being stuck, unable to get anything done, just thinking of the problem at hand, because I want to find the best possible way to solve it…

A theoretical problem

The problems that get me stuck like this are always basically just some theoretical problems… How to arrange some directories and files in a reasonable manner, how to make some classes talk with each other, how to implement some complex feature.

Most of them don’t have a definite answer. You could arrange dirs and files in many ways that could make sense, and in the coding problems there’s always many ways to approach it.

While it helps to plan ahead, I just try to find the most optimal solution, and it isn’t always possible right away, and I end up getting stuck.

A practical solution

So what’s the best way to get out of an endless loop? Do something about it!

First, it may help to write down some notes. What does the feature you’re thinking about need to do? How to do it (the alternatives)? Then, it may also be helpful to draw some simple diagrams. Maybe a flowchart of the pieces and how they interact.

While these may seem pointless, they will help you organize your thoughts.

When you have some kind of an idea about what you need to do, do it. Even if the solution is not perfect, or doesn’t do all the things you want it to do, do it.

After you have some working code, you can then look at it and start implementing the things it doesn’t do yet. It’s much easier to add into existing code than just think about the whole thing.

You may have to modify the original code to account for the new features, but so what? You should have unit tests that you can use to be sure your new code doesn’t break the interface! You also should have your code in source control – then you can always go back to the old code if the new one doesn’t work out.

I think this is where it boils down: I try to anticipate what I want the code to do, and make room for all of it at once, which just sometimes doesn’t work out when I can’t come up with a clear picture of it. That’s when you have to just go in and start writing something, and change it in the progress. Don’t forget to have some kind of a goal in mind, though – otherwise you can easily get feature creep.