Scott Hackett at SlickEdit has blogged its time to update the time-honored practice of code scavenging. According to Hackett, taking a more formal approach could help to improve developer productivity. He argues that, while software re-use has historically concentrated on higher level frameworks and larger objects, there are …
code scavenging for bite-size tasks
shell, find, grep, sed, awk; God bless linux and cygwin.
Need I say more?
It sounds like one of those concepts that is a great idea until you try actually implementing it - then you find the simple sounding idea is almost impossible to implement efficiently because of the volumes of data
I would wonder about the time saved aspect - by the time you have downloaded a few similar code units, evaluated them for quality (there is a lot of dross out there) and fit then integrated them they will have to be pretty substantial units to make it worthwhile
scavenging is great for learning though, whether that is learning coding, learning a new language or just how to do a specific task - but usually does the code bear any resemblance to what you started with
Useful byte-size sources
Off the top of my head I can think of three very useful sources of code at the small reusable function level. All are books and all deserve shelf space in any working programmer's library. Despite their similarity there's not a lot of overlap:
The Practise of Programming: Kernighan & Pike
Software Tools in Pascal: Kernighan & Plauger
The emphasis in "Software Tools" is on designing code for reuse. Its examples are (surprise!) in Pascal but are very easy to translate into C or Java. If you work through the book you'll end up with a useful and varied source collection, which is easy to convert into a general support library.
"Practise" presents its examples in Java, C and C++ code with an emphasis on writing well-structured, maintainable and testable code.
"Algorithms" was written from the outset as a source book. If you think of it as a smaller version of Donald Knuth's "The Art of Computer Programming" but written in Pascal rather than pseudo assembler that's about right. It presents a large number of algorithms as Pascal procedures and functions. Again, this is very easy to convert: I've typed C into a terminal while reading the Pascal source and had it run once my typos were spotted and removed.
All three books do a good job of describing the code they present and in explaining the principles behind it.
Reading old code is always useful especially if you are new and you have someone explaining the code for you.
And code reuse is great in controlled environment but you have to be very careful with all the issues it brings, don't just copy some licensed code. And be careful of hidden macros, objects, special compiler, OS and hw related things. I can't count the cases where I had to fix for example code written for PPC going to x86 - did you remember the byte order or did you even know? Or going to 64bit? Or default alignments in different compilers and architectures? And so on. And these in code of very experienced developers who used too much cut and paste in large systems ( millions of lines of code. )
It also can be a small problem sometimes. I use several languages and my normal environment has snippets, macros, etc. My editors have templates and huge collection of snippets. Now, suddenly in a system that only has an editor and compiler it feels weird, how to start? Where are my templates, etc? So, code reuse may be a good way to start and very useful in every day work but everyone should be able to work without any when you have a situation where the only tool is vi or notepad ( I personally hate that! ) And often the man pages, help files are missing in such systems so..
First-class Copy & Paste for Code Reuse
Like most problems, textual code reuse has via copy & paste has already been studied, and a language based on first-class copy & paste is being developed ; it's called the Subtext language . See a more detailed discussion of it on lambda-the-ultimate . Academia is not so far remove from everyday programming as people seem to think!
Cookbook, CPAN anyone?
It seems to me that scripting languages such as Perl and Python have recognised the benefits of code reuse and their communities have spent time putting together code repositories such as http://www.cpan.org/ and http://aspn.activestate.com/ASPN/Python/Cookbook/ . Searching can still be a pain but people do explain their problem, together with their efforts so far, on newsgroups such as comp.lang.python and receive community help as either original code snippets, or pointers to code .
The process can always be improved, but at least with open-source scripting languages such as Python and Perl, you're not starting from scratch.
Essentially, I was taught to code by scavenging. My earliest teacher gave us a chunk of code, got us to understand what and how it worked, and then asked us to edit it to meet a new task. That was the good old days of BASIC. I took the approach on to Pascal, Modula2 and then C, and C++ seems to have been designed for this approach. But, the key was code comments and documentation - my teachers hammered it into me that good code has good comments so it is both evolvable and re-usable. I was always told the aim was that someone could follow on after me, take my code and expand, fix or re-use it in part of whole.
I used a number of snippets from that when I was new to programming, or new to a task.. The SWAG archives were quite good for source snippets in Pascal, Delphi and IIRC C as well (although I wasn't into C back then).
I started re-using code with basic on the Speccy.. It's not really a new concept, even if it does have a new name (how often is that done these days? Take something common, give it a new name, claim you invented it...?)
@Matt Byrant : You didn't have Michael whats-his-name at Whiterea in New Zealand did you? Or a tutor named Michael who was from NZ but moved to the UK in the mid 90's?
Copy and paste not as good as a good code library
The problem with a lot of code use is that it promotes BAD programming. I copy it from here, and paste it in this program and that program and that program and pretty soon I have lots of code that does the same thing being maintained in 20 different places.
Code Libraries are far better than this cut and paste mentality. The C Runtime Library made things easy to port, easy to use and in general help progress the art of computer programming way more than this arbitrary scrapbook of code you can paste from.
.NET and Java have almost gone too far the other direction with such a thick book of code that you don't have to invent much to do simple things (image manip, crypto, ....) but you need an encyclopedic brain to remember which package it is in.
We use a few core libraries in our development work and when we add functionality to one app that might be useful in the other, we put it in one of those core libraries:
WinExtensions - Thing Win32 should have had but didn't get
ImageCore - Things to do with Imaging
Generic - Things that STL didn't include
Boost - Nuff said
XMLib - XML stuff that sits on top of xerces (convenience mostly)
By collecting our code into groups like this we don't need so much to copy and paste and we maintain the code (with unit tests) in ONE spot.
I think it would be better to work on taming the Java and .NET API beast (although that might not be possible) than trying to make a cut and paste beast like this would become.
Design Patterns..... Yea, that's it!
- Updated Microsoft Azure goes TITSUP (Total Inability To Support Usual Performance)
- The Return of BSOD: Does ANYONE trust Microsoft patches?
- Review Apple takes blade to 13-inch MacBook Pro with Retina display
- Munich considers dumping Linux for ... GULP ... Windows!
- Pic iPhone 6 flip tip slips in Aussie's clip: Apple's 'reversible USB' leaks