Archive for the ‘Programming’ Category

Reusing code

Wednesday, October 10th, 2007

A lot of modern programming focusses on making code reusable and modulated. This is clearly a good idea since there is no point writing code more than once since it is just a waste of time and gets pretty boring.

The tools which programmers have been able to use have become more and more sophisticated as languages have developed. Earlier languages had functions which you could use to put pieces of code into packets to be used elsewhere. Then object orientated programming was developed so you could easily package functions and variables together so that they could be reused. Files can be included so that you can just call in functions and objects from other files rather than having to copy them into your code.

Some programs that I have taken a look at recently are almost completely made up of functions and objects. This can be good but it is not good because they will be able to reuse the code, in reality most of the code is pretty specialised and couldn’t simply be put straight into another program. The main benefit that I see is keeping the code well organised. It is the idea that if you change one function that has a problem with it then the other functions don’t need to be changed, they should still work properly. This makes it a lot easier to debug code or make alterations clearly. And also as long as the structure is fairly clear then it makes it a lot easier for another coder to make some modification because they don’t have to worry about the whole structure, they just need to learn how the small section that they are trying to modify works.

One of the pitfalls that many programmers can fall into is creating code so that they can reuse it all in other projects. Some bits should be written so they can be reused but only some bits such as a database class. Writing too much of your program to be reusable will be very time consuming and will lead to a lot of unnecessary code being written. This will have a bad effect on performance since they is a lot of unnecessary code that has to be run. Also if you do want to reuse some code just because you wrote it specifically for one thing doesn’t mean you can’t reuse it. A small amount of modification will make it usable for another project.

Also a final thing which I have noticed with my own code is that if I look back at my old code it looks dreadful in many cases. I have only been programming for a few years so if I look back then at the time I wrote it I hadn’t been programming for very long so I didn’t know how to do things very well. So I wouldn’t even consider reusing the code I wrote then because I would rewrite it better now.

That is my opinion on these things anyway. It isn’t a simple right or wrong thing really as it does depend on what you like personally. I don’t really like spending too much time saving time later because I don’t seem to save time later in many cases.