|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
| View Poll Results: How often do you recycle your code? | |||
| Always | | 2 | 16.67% |
| Often | | 10 | 83.33% |
| Rarely | | 0 | 0% |
| Never | | 0 | 0% |
| Voters: 12. You may not vote on this poll | |||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
Recycling Ideas and Snippets
Often you see people asking questions specific to some operation or method. Like how to extract certain data from a MySQL table. I find this pretty odd since extracting data is basicly always a somewhat rutine. Maybe names of the tables change but otherwise things stay the same. There have been other examples as well.
So I ask now, how much do you recycle your ideas and snippets?
__________________
-- Tomi Kaistila -- Developer's Journal The more you learn, the more you know. The more you know, the more you forget. The more you forget, the less you know. |
|
#2
|
|||
|
|||
|
Whenever I do something new, I try to write a general solution and then apply this; a good example is when I categories for a webshop, after some badly written code I realized that it could be turned into a general solution, so I spent some extra time and wrote a general solution which can easily be reused.
I've also created myself a small library which is global to all the sites we have, and which eventually should contain lots of code for simplifying things, I've added a small auto-prepended function called import() which I use to load stuff from this library. On the other hand, it often happens that I'm about to do something I've done before, and then realize that my previous take on it was not perfect (and I want things to be perfect). In those cases I rewrite it, possibly refactoring it.
__________________
-- Regards André Nĉss Puritanism: The haunting fear that someone, somewhere may be having fun |
|
#3
|
||||
|
||||
|
I too try to reuse as much as possible, as long as I think it's worth it and the code ain't all bad...
And I also have a kind of a library which I extend and change when necessary. This is also (mostly) transferrable between sites, thus often reused. But I think everybody does this way, I mean what's the point in writing the same (or similar) code over and over again? If you've done something similar before, it's in the human nature to reuse your experiences... But I'm not saying that those experiences can't be improved or re-evaluated... //NoXcuz
__________________
UN*X is sexy! who | grep -i blonde | date; cd ~; unzip; touch; strip; finger; mount; gasp; yes; uptime; umount; sleep |
|
#4
|
|||
|
|||
|
Yeah I think most peopel "reuse" their code, but the question is do they do it properly? Many people probably use the "copypaste" approach to reuse, which IMO isn't really reuse, just laziness. If you centralize the code, you can easily update and improve it, without having to do so in tons of files.
|
|
#5
|
||||
|
||||
|
I think that this is a problem that is actually bigger than it sounds. Take Macromedia for example, their code library is pretty much what you said about having a library of your own to refer to. Use dreamweaver and you'll use the Macromedia code library, no bad thing until you realise how out of date some of the coding is. The fact that it's there though means people use it without thinking. Online code bases are the same, people take and don't think. The company that does our web sites uses macromedia dreamweaver, when I joined the company they had a drop down menu script that was 38k in size. With a little updating it could be reduced to 7k.
As far as helping people goes I try my best to push people in the right direction when answering queries by giving them snippets of code and not the full blown answer. This way peopl will learn, and hopefully come back to me and say 'heres a better way of doing it'. I don't like the idea of people just copying my code then pasting it in without knowing why it works. Also, I don't like it when some people obviously have copied the code, pasted it without adapting it and immediately posted in the forum to say that the code doesn't work. The gripe out of the way, I reuse code. Only small amounts though, and use them as building blocks. It's the question of design. On big bit of code is not reusable, whereas small component pieces are. Sorry about the ramble, it's time to eat and I seem to be losing with DHTML. |
|
#6
|
||||
|
||||
|
When you have little to no experience, I think one often use the copy'n'paste method. Wheter it's from your own source, or if it's from someone else's code. It's simply the easiest way out (not saying this is entirely bad though)...
In that case I don't think one reflects (or even understands) if the code is well written, efficient, reusable etc. It's more of the attitude 'this worked in my previous script, so it should work here as well'... But when the page is checked with one's browser and the only thing you see is parse errors or fatal errors, the question arises: Why doesn't this work? And so one posts a question here... So I guess you have a point there André, if it's properly or not... And I think this topic may also easily divert into a question of separating logic from presentation, which is a must if you wish to write truly reusable code... BTW André, in a recent thread you provided an example of fetching data from mysql which is to be stored in an array for later processing/presentation. I've used stuff like that before, though not extensively, so I decided to take a shot at it and 'go all the way'. And found out that it really helped me on a current project. So I guess I just wanna say thanks for the idea... ![]() //NoXcuz |
|
#7
|
||||
|
||||
|
I'm not sure I understood correctly what NoXcuz and andnaess meant by a code library. I mean I do have a place where I keep my best work and all sorts of small snippets that I've collected from various sources. But a library from which you "import" functions goes way beyond me. It sounds good when dealing with software development, but for web programming it sounds somewhat odd. I'd love to know more...
|
|
#8
|
||||
|
||||
|
I do a lot of VB work with SQL Server and I found myself writing tons of similar code (with a bunch of typos that had to be fixed). So I wrote a code generator to write the bulk of the base code for a form. This is a different form of reuse since I actually get "fresh" code every time I generate a form, but its always the same pre-tested code.
When I start my form generator a form pops up. I connect to a database and pick a table from a combo box. All the table fields are listed in a one grid. All of the related child tables (by foreign key) are listed in another. In the grid I get to choose the type of control that will be bound to each field (Textbox, Checkbox, Date, Time, Combobox from database, Combobox from literals, etc.). If I want any of the related child tables to appear at the bottom of the form as a grid, I put a check next to them. When I generate the form, it puts all the controls on it with a label. It writes all the code for sorting, filtering, finding, enabling / disabling toolbar buttons, forward, backward, saving, adding, deleting, closing, error handling, etc. Of course, using a code generator forces you to have consistent naming conventions since it bases the names on the data model. All of the generated code is stored locally and then sorted alphabetically by subroutine name before being written to a file. I can go from a blank table to a working form with base functionality in less than 10 minutes. Most forms have ~8,000 to ~22,000 lines of code and comments generated. It is then up to me to just add the extra code to make the form interact with other parts of the program. I have found this process to be a huge time saver. It eliminates a lot of drudgery from my job and makes my code easier to maintain. It took a long time to write the code generator but it was worth the effort. |
|
#9
|
|||
|
|||
|
Quote:
Well, ehh... glad to be of help... I guess ![]() |
|
#10
|
|||
|
|||
|
Quote:
Well the most important thing is that a code library is reused by way of includes. Say you have a DB abstraction layer coded, which uses the files: DBA.inc DBA_mysql.inc DBA_postgresql.inc DBA_oracle.inc DBA_config.inc Now, by including DBA.inc you get all the functionality you need, and you can start using it. However, if you copy these files to where your project is, and then include this copy, you're on the wrong track. So the point is keeping your library one place and always "referencing" it rather than copying it. What I have done is simple; I have a folder called phplib which resides in /home/www/, and under this is my library, organized to suit my needs. I wrote an import function because I might extend this library idea in the future, and add support for packages and stuff like that, it's nice to already have a interface defined so that I don't have to change lots of includes etc. It also let's me easily move the library because all I have to worry about is making sure import() works as specified. Which reminds me; I also use small snippets of javascript code once in a while (hide/show thingies for administration forms, clocks, floating layers, value checks, whatever.) And I found a neat way of organizing this into a reusable library; I simply have this folder called /home/www/jslib/, then from every website there is a symbolic link to this folder, so that whenever I write <script src="jslib/foo.js"> I include from this library. It's a nice way to avoid having multiple copies of the same javascript code ![]() |
|
#11
|
||||
|
||||
|
I reuse code all the time, thanks to CPAN:
Code:
use CGI; use DBI; use HTML::Template; use CGI::Application; use DBIx::FullTextSearch; use Mail::Sendmail; use Storable; use LWP::Simple; use LWP::UserAgent; use HTML::TagFilter; use HTML::Clean; use HTML::TokeParser; use XML::RSS; are the few that come to mind immediatly. I find the more I code the more I'm stitching together pieces of work that others already bug-fix and maintain via CPAN. I also do most of my web application development with CGI::Application, and have created a custom SuperClass with it that I use as a base for most of my projects. I chop out what I don't need and have a good start on what I'll need to do the things I want. Very, very powerful! |
|
#12
|
|||
|
|||
|
I do reuse my coding. I also use them as reference also.
I've never done a library though... too lazy ![]() Just copy/paste/modify from other scripts.
__________________
K1 |
|
#13
|
||||
|
||||
|
I would say that 'and' caught me here on my code reuse.
Yeah I do reuse but the way like I copy past my code. Though these days I have one functions.php which contains a lot of functions which would help me out in my any new web app development. Now I shall soon spend time on gathering my reusable code in one place. JD
__________________
_____________________________ d.k.jariwala (JD) ~ simple thought, simple act ~ I blog @ http://jdk.phpkid.org |
|
#14
|
||||
|
||||
|
i believe that most people who've spent some time programming tend to re-use their/other's codes that have been tested before even if its not the best solution to the problem, as long as its working.
i dont have a "library" of codes but i re-use and modify my old codes or use them for reference. |
|
#15
|