Reuse Code And Write Reusable Code
Before I set out to write a significant amount of code, I search high and low to make sure someone else hasn’t already written it.
There are a few ways to do this:
1. Google Code Search
2. Look in your other projects for similar code.
3. Ask around (co-workers, twitter, IRC etc..).
One main reason for code reuse is that it will usually have less bugs than writing it from scratch. It is also evident that reusing code will save time. For example, why write a user authentication system when you can just download one and install it as a plugin. A few hours of research can save hundreds of hours programming. Another advantage to using pre-existing code is that it is usually more abstract and can fit a wider variety of applications. As a bonus, it usually has more features.
When you do have to write code because the problem you are solving requires more customization than anything out of the box, it is best to make sure it is reusable. For example, avoid naming your variables too specific, such as the_yellow_square_at_the_left_of_the_page, instead, create a function similar to shape('yellow', 'square', 'left'). This will allow you to reuse it for many situations. If you wanted a purple triangle on the right you would just write shape('purple', 'triangle', 'right'). It’s much easier to refactor and reuse on future projects.