2008-11-06

CODING: Modular coding techniques

Totally unnecessary disclaimer: I do not claim to be any kind of authority when it comes to programming. The following are just observations.

For more modular coding, I would like to mention two techniques.

First: code for unit testing. This means to write your methods in such a way that it is easy to test each method in an "offline" setting to make sure the method produces expected values given certain input. In this way, if you make changes to the method, you can test the method before and after to make sure the output is the same for both instances.

The trick in this case is to develop methods so that they do indeed work offline. For example. I was just editing a method that creates a SQL query using arguments passed to the method, executes the query, then returns the results. For unit testing, the part where the query is build should be in its own method (separated from the database part), so that you can test it with input when you change the method of building the query.

Second: modularize everything (sorry for making a verb by adding "ize"). Every part of an application should be modularized so that changing one part does not require changing other parts. For example, I have an application that accepts input from an excel spreadsheet and produces PHP code. However, many methods in the application contribute to the final output. What I should do is have all of those methods call a common output method, and have that write to the final output. In this way, if I decide to output in different mediums, I only have to change the output method, rather than the multiple methods.

No comments: