“Marty, you’re just not thinking 4th dimensionally”
I’ve worked on a few different types of applications at this point in my career and one sure sign of poor programming I’ve noticed is the inability to program 4th dimensionally. You notice this when the application may be working but implementing features begins to take longer and longer. The performance starts to become an issue when more users are added to the system. These problems are a result of the programmer not thinking about the future.
PHP code is notorious for this problem because it is so easy and tempting to write code that just focuses on the immediate problem. A programmer gets the task done quickly and he is happy and the client is happy. It’s also the reason why on outsourcing sites there are many job postings looking for PHP programmers at $5/hour. I don’t see many postings looking for Ruby on Rails developers at that rate. Ruby on Rails forces developers to use a well-established framework that adheres to good design practices. With PHP, especially without using a framework, developers have the ability to just throw everything together if they so choose. This leads to a big mess. If the application grows, this mess gets bigger and bigger until it needs to be cleaned up.
Cleaning up a code mess involves a lot of work and it shouldn’t be done all at once. It’s like switching bank accounts. I’ve switched my main banking account before and it is a hassle that takes months to complete. Any direct deposits and automated bill payments need to be changed to the new bank account. Money needs to be transferred. When you think you’ve got everything switched over, it’s also good to leave the old bank account active for a little while longer until you are reasonably confident that you haven’t forgotten anything.
Cleaning up a code mess is similar. First you want to make sure that the logic will not break. This involves writing tests for the current system. These will most likely have to be done from scratch because if the codebase is a mess, chances are there is no testing being done.
Secondly, start with simple modules that are easy to understand and are loosely coupled to other parts of the application. These parts of the application are the easiest to switch over to cleaner code because you can be relatively confident that they will still work without breaking the application. Move on to more complicated modules as you understand the system better and all the connections.
Finally, when the modules have been re-coded into a more fluid design, remove the old code. Old code on a system just confuses new developers who don’t know if it’s important or not. Throw it out to keep things simple. You aren’t going to use it.
As a developer, how can you better program for the future? There are a few things you can do:
Every time you write code think about how it will look in the future. How easily can someone understand this code when they read it 6 or 12 months from now? What if the code needs to be used somewhere else? Is it easy to call from another part of the application?
In addition to thinking long-term while performing short-term tasks, start noticing code messes. Anytime you see the same code repeated more than once in an application, alarm bells should start going off in your head alerting you to a possible mess. Make a note of it.
Take some time regularly to clean up messy code you notice. This is probably the most difficult as it requires a developer to think independently. In my experience, very few managers are going to come to you and say “You should take the next week to work on cleaning up the code while no features are getting implemented.” As a developer, you need to just clean up the code as you go along without being asked to do so. You do it because even though the immediate task may take a little bit longer, you are saving more time down the road.
If all the effort is placed on the immediate short-term you will be like a public company that just focuses all its attention on the next quarter’s results. It may work for a while but after cutting their Research and Development budget to save costs and boost short-term profits, the company then realises that they have nothing in the pipeline for the upcoming years and begins to lose market share.
Your manager/client is always going to be providing the short-term, immediate tasks you need to work on but to become a professional developer, you need to carve out a part of your day to think 4th dimensionally.