Dev Shed Lounge
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherDev Shed Lounge
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


Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old January 23rd, 2002, 05:16 AM
Datamike's Avatar
Datamike Datamike is offline
Web Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2001
Location: Finland
Posts: 719 Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 27 m 33 sec
Reputation Power: 9
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.

Reply With Quote
  #2  
Old January 23rd, 2002, 05:55 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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

Reply With Quote
  #3  
Old January 23rd, 2002, 06:25 AM
NoXcuz's Avatar
NoXcuz NoXcuz is offline
Wiking
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Sep 2000
Location: Sweden
Posts: 3,608 NoXcuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 27 sec
Reputation Power: 11
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

Reply With Quote
  #4  
Old January 23rd, 2002, 06:29 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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.

Reply With Quote
  #5  
Old January 23rd, 2002, 07:06 AM
binky's Avatar
binky binky is offline
Gerbil
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: In a Rotastak
Posts: 1,763 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 22 h 12 m 52 sec
Reputation Power: 18
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.

Reply With Quote
  #6  
Old January 23rd, 2002, 07:09 AM
NoXcuz's Avatar
NoXcuz NoXcuz is offline
Wiking
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Sep 2000
Location: Sweden
Posts: 3,608 NoXcuz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 27 sec
Reputation Power: 11
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

Reply With Quote
  #7  
Old January 23rd, 2002, 07:40 AM
Datamike's Avatar
Datamike Datamike is offline
Web Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2001
Location: Finland
Posts: 719 Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level)Datamike User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 27 m 33 sec
Reputation Power: 9
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...

Reply With Quote
  #8  
Old January 23rd, 2002, 07:52 AM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
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.

Reply With Quote
  #9  
Old January 23rd, 2002, 09:53 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Quote:
Originally posted by NoXcuz
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


Well, ehh... glad to be of help... I guess

Reply With Quote
  #10  
Old January 23rd, 2002, 10:06 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Quote:
Originally posted by Datamike
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...


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

Reply With Quote
  #11  
Old January 23rd, 2002, 04:54 PM
Hero Zzyzzx's Avatar
Hero Zzyzzx Hero Zzyzzx is offline
11
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Jul 2001
Location: Lynn, MA
Posts: 4,632 Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hero Zzyzzx User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 23 h 12 m 33 sec
Reputation Power: 76
Send a message via AIM to Hero Zzyzzx
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!

Reply With Quote
  #12  
Old January 23rd, 2002, 05:28 PM
Keiichi Keiichi is offline
aHVoPw==
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2001
Posts: 1,058 Keiichi User rank is Lance Corporal (50 - 100 Reputation Level)Keiichi User rank is Lance Corporal (50 - 100 Reputation Level)Keiichi User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 15 h 24 m 31 sec
Reputation Power: 9
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

Reply With Quote
  #13  
Old January 23rd, 2002, 10:05 PM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
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

Reply With Quote
  #14  
Old January 24th, 2002, 12:07 AM
roninblade's Avatar
roninblade roninblade is offline
// no comment
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2001
Posts: 1,639 roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level)roninblade User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 2 h 36 m 14 sec
Reputation Power: 33
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.

Reply With Quote
  #15  
Old Januar