There is an interesting thread [thread=667585]How do you get faster and more efficient?[/thread] and some people mentioned code snippets as something that increases their efficiency. I wonder how exactly people go about managing them. I know once you have found a snippet you copy and paste it to your code, but how do you find the right snippet? Do you search for ready-made code online when the need arises? Or do you keep your own database of code snippets taken out from your projects in a way that is searchable easily? Do you use some special software for them or just plain txt or php files in folder structures?
I am thinking of creating a collection of my own snippets for future use and I am wondering if this would increase my efficiency and whether it’s worthwhile. So far when I need a piece of code I created in the past I try to remember in which project I used it and copy directly from the old project and adapt it to the new one. Now I am thinking of:
Building a separate collection of php code snippets in an organized way. That would include functions, methods and classes for performing various tasks, even as simple as truncating text or camelizing strings - so that when I need them they are available right away.
Using a software that will make it easy - is there anything you can recommend? Preferably freeware.
Do you think this makes sense? I have doubts because it will certainly require some effort on my part now and later when coding new stuff and I may never need some of the snippets in the future. On the other hand this could save me some time when I need to find a piece of code. So I am not sure… What is your experience?
Unfortunately, the chances of finding quality code snippets, for PHP, are generally quite rare. When finding a snippet, you should ensure that it’s of a high caliber. When finding snippets that I find impressive, I generally try to just understand the thinking that went behind it, so I can apply the underlying knowledge in my own way. Sometimes, nice snippets will be impressive hacks meant to solve a specific problem. Before saving it for reuse, you should consider rewriting it to solve the problem in a more general way. You’ll also want to make sure it fits into your existing code base in a way that makes sense to you.
I am thinking of creating a collection of my own snippets for future use and I am wondering if this would increase my efficiency and whether it’s worthwhile. So far when I need a piece of code I created in the past I try to remember in which project I used it and copy directly from the old project and adapt it to the new one. Now I am thinking of:
Generally, I find copy and pasting code a bad idea*. When making your code reusable, you should make them reusable as functions or classes, which you can then [require_once](http://php.net/require-once) into your code. The question, then, is how to organize things.
It’s helpful to look at quality libraries to see how things are organized (say, the Zend Framework), or even the standard library itself. What I’ve done is made a library above my document root, which serves as the root directory for my libs, which are divided into sub-directories. I have the root lib directory in PHP’s include path so that my PHP code that needs to use the libs can access it from anywhere. For example:
<?php
/* PHP will search the include_path setting (which is like
* how $PATH works) */
require_once 'nntp/client.php';
Building a separate collection of php code snippets in an organized way. That would include functions, methods and classes for performing various tasks, even as simple as truncating text or camelizing strings - so that when I need them they are available right away.
What you consider useful in your library is completely up to you. Generally, if you find yourself going back to something in an older project often, that’s a key sign that you should rewrite it to be more generally useful and allow it to be require_once’d whenever you need it (whether that be a function, a class, or a collection of these). Getting the organization down satisfactorily is something I find just takes time and experimenting. For the most part, it’s really a matter of taste as to how you organize things.
Using a software that will make it easy - is there anything you can recommend? Preferably freeware.
I’ve never used any for managing code snippets, but I imagine a quick Google search would help here.
Do you think this makes sense? I have doubts because it will certainly require some effort on my part now and later when coding new stuff and I may never need some of the snippets in the future. On the other hand this could save me some time when I need to find a piece of code. So I am not sure… What is your experience?
Again, how you use and value snippets of code is a personalized experience. If you find yourself needing to do something often, whether by copy and pasting old code or even reinventing the wheel, it’s a strong sign that you should modularize that code to be generally applicable.
Learning how to write modular code in PHP is a lot easier when you have a grasp on things like PHP5+ OOP. Of course, you can stick to procedural programming; there’s nothing at all wrong with it. Studying programming paradigms, in general, can be helpful in getting the most out of designing well-modularized code.
Although copy and pasting is often a bad idea, code generation is often a good idea.
I’m a big fan of code snippets, I use them in preference to libraries which IMO tend to bulk out your development environment (I seem to end up amassing loads of functions that all end up bleeding into each other!). In regards to my approach, this piece of freeware may be what you’re looking for (as long as you remember that copy pasting may not be the best option if you find yourself blending a lot of your code… http://snippets.gabehabe.com/ Hope it’s useful for you.
Thanks, this program might be something I am interested in. The bad news is that I have tried it and it crashes often and some things still don’t work. “Cloud” access is really nice but the program crashes every time I try to save a snipped to the cloud, I succeeded only once in a dosen attempts. If the developer keeps the project alive it can grow into a very useful program.
However, inspired by this I found another small program at http://www.snippetmanager.net/ - it looks like newly released software and also looks promising. It can upload and download snippets from ftp and it seems to work. I will need to take some time to play with it more. Do you save snippets every time you code something new or do you set aside special time at some intervals to gather all your new snippets in one place?
dyer85, thanks for your point of view. In this post I am more interested in finding out how to manage and use snippets but I agree with you that writing modularized code can save time. I use OOP, not yet very proficient but gradually I am grasping more and more concepts.