Go to: Articles List

Code Smarter Part 1 : Encapsulate Code

If you have been coding for long you have done it. You have written a group of lines of code that perform a specific task. Then a week or two later, you write the same code again. Frustrating isn't it?

The answer to this problem is to encapsulate your code! What this means is to put reuseable bits of code into functions or sub routines so that they can be reused at a later date.

However, that is not all. When you encapsulate code you need to write it in a way the makes it versatile and easily reused. A simple example of this is the database connection routine. Instead of writing this code over and over, or even cut and pasting it in, put it in to a sub routine.

When you encapsulate your code try to make it as easy to reuse as possible. We have all gone back through our old code and had to sit there for a second and figure out what we were trying to do. Make your code readable and comment it out for your own sake!