I like my coding like my women, quick and easy. Which explains why I’m alone. I was fascinated with the whole code reuse hype a while back.
“Yeah! Using the same code for multiple projects. Awesome fun time!”
Coding for reuse takes a lot of discipline, devotion, and time. It is a great idea overall, much like security, but what is the point?
The Code Reuse Purpose
There comes a point in every development cycle where you are coding the same thing over and over again. You ask the question of why. Why do you mindlessly code the same procedure, when the language gives you the power to create a function that you can use to simply the task?
You then create the function and use it until, oops, you need another function like the one before that either takes different input or outputs something differently. So you create another function and continue until you have multiple functions that preform the same task differently.
You’ll eventually take a step back and contemplate the horrible mesh of junk. How can I make this better? Well, taking what the language gives you, you’ll probably overcome the task multiple different ways. Perhaps an object would be an better choice.
More Time Spent On Reuse Than Problem Solving
Don’t get me wrong, code reuse should be practiced on every project as much as possible. However, I personally have spent more time on developing code reuse than actually finishing the project I was working. Once you lose sight of what it was that you were trying to do in the first place then it is time to take a breath and forget it.
For example, I’m have currently been working on the same project for the past 7 months that has knocked me on my ass. It should of been done five months ago, and in retrospect, some of the time has been spent trying to make it to where the code could be reused and easily expendable. Another friend of mine has been working on another project with no thought to code reuse or even security (but I have given some sense to him).
In the same time I have been working on my project making it to where it is wonderful work of coding art, he has started and almost completed his project. That feels to me like a kick to the stomach. He is a novice and I’ve been programming for years and doing PHP for 3 years. How could this have happened?
Don’t Mock Those Who Embed Queries In Their Code
PHP allows for quick development by those who choose to embed SQL queries in their code. They don’t have to create an object, maintain the object, and test the object. They just write the query, test the query, and use the output. It is ’safer’ and quicker coding than to test objects and the different way to use them.
In using objects you want to add functionality, so that you can add, change, and delete easily. Each method has to be planned, contemplated, debated, developed, tested, and deployed and by which time you could have spent half the time using two or three mysql functions to achieve the means to the ends.
Conclusion
I believe it is the measure of a developers skill as to how well they can plan out a project and see it through with the ability to reuse their code on multiple projects. It is always a good idea to develop with code reuse in mind, as well as security.
When you have tight deadlines, I think exceptions can be made for those who desire to just either end the project or end the client. Ending the project with any means is by far the best choice overall.
You can always go back and take the parts that you want to reuse and create that awesome object. You already have the proof-of-concept developed.
Possibly Related Posts:
- Paying Off Debt Revisited
- Thou Art God
- Saying Good Bye to Dollhouse
- Where the Wild Things Are Movie Review
- Why Choose 1st Financial Bank?
Tags: PHP
while i agree that initial development while using “dirty” code can be a lot quicker, maintaining a project after a few months with ever changing requirements as the site or buisiness behind it grows, will get difficult.
It’s even more difficult if the project spans a few major php releases (i’m still working with a website that was developped when php 4 was just a new kid on the block) and contains a lot of code.
The programming world also changes, in 1999, in the php world, not a lot of ppl were working with OO. Database good practices for mysql also changed since v3 etc etc.
I strongly believe, that a site build by complying to the latest trends (OO, patterns, database abstraction, unittesting etc) will be a lot easyer to maintain over the years. Butalas, it’s true, writing unittests, or phpdocs, etc takes a shitload of time, mabe better used adding features
A little clarity would be to say:
Try to code projects, so that you can reuse the code later, whether using functions or objects, but don’t lose sight of the original goal. If you don’t have a deadline, then it is all good.
I’m a hard advocate for code reuse and OOP. Sometimes, you just have to say to yourself, do I want to do this done right or do I want it finished quickly. It is a hard decision and you can’t have it both ways every time.
Embedded Mysql queries can be maintainable if you separate the content out. If you are looking at a huge blob of text with queries, then it does tend to hurt your eyes.
Reuse and Modularity on any project is always a hard call, be it a PHP program or designing a businesses network infrastructure. If you have time then it never hurts to add to the quality of your work. If pushed for time, get the job done and reap what you can. For me in most cases I try to spend a good amount of time making things modular to help keep things organized and aid in tracking bugs…
If it’s something simple I’ll make a function.
If there are many functions that start to relate to one another I’ll see if there is a set of them that would quickly and easily make a usefull class.
Only if it’s obvious the use of some part of code can be used on a majority of my projects, or if I find myself redoing something quite often that I didn’t foresee, then and only then will I bother with really fleshing something out, and only in free time… like at the coffee shop on paper or if I have maybe an hour left alloted for the day on a project I finished/got to a good stopping point early.
That way, your time is not wasted on details that may end up being irrelevant for a long time anyway, and the process is more "organic" by "evolving" in a manner that tends to be the path of least resistance.
I have seen the advantages of a large design stage, but unless the programmer is working on a huge project that may be split to multiple teams of coders, and they are the one overseeing most of that… or at least fleshing out the parts so management can split it up, it just doesn’t appear justifiable from a more financial/business sense. If that is the scenario however, reuse and modularity is almost a necessity for organization’s and sanity’s sake.
Well, there’s my two scents, hope it helps.