Luc <
luc@sep.invalid> wrote:
I already do a little bit of that. I find myself copying blocks of
code from one old project to a new one quite often (and my IDE has a very
useful "snippets" feature),
That's one form of 'reuse', but:
but there is always the problem of adjusting all the variable names
and perhaps even proc arguments and parameters.
You've discovered the problem with "copy/paste reuse". Adapting the
copied code to fit into its place in a new batch of code can often be
more work than rewriting it all over again.
But I often crave something richer than that. I fancy much better
building blocks that will help me produce stuff with a lot less
effort, on use and reuse.
You are beginning to recognize what are termed "code libraries".
Usually (but not always) small code chunks for one specific useful
aspect, that can then be reused in multiple other projects without the
effort of "rewriting" or of "adjusting variable names and proc
arguments".
I.e., Tcllib is a code library. It has tons of useful modules inside.
To pick a couple out, the 'sha256' module impliments the SHA256 hash
function [1]. Lets just say you regularly write applications that
make use of SHA256 hashes. You /could/ copy paste in the long drawn
out code to perform a SHA256 hash into each one, laboriously changing
variable names that conflict. Or, you could write a "SHA256 hash
function module" (i.e., what is now in Tcllib) and then, every time you
are writing some application that makes use of a sha256 hash, you just
do:
package require sha256
And then use the ready made hash functions that are part of the
"sha2::*" namespace thaat the module adds to your application. And:
1) not have to copy/past anything from anywhere;
2) not have to laboriously edit variable names to avoid name conflicts
3) not have to adjust proc arguments
4) etc.
Maybe I should pause all the projects (even more so) and spend some
time focused on writing such blocks?
That's your choice, it is your code and your time to do with as you
please.
Is that a good idea?
In general, if you find you have "chunks" you keep reusing, then
exerting the effort to make a package/module from the "chunk", with a
generic enough interface to cover all your needs from the
package/module, will turn out to return the effort/time ten or a
hundred fold later. So yes, generally a good idea.
At the same time, don't go "package/module" happy either (i.e. NPM).
Exert the effort to make packages or modules for the chunks you *know*
you are often reusing, as you will more likely reuse them later. But
"building a module" from a chunk you are not sure you will reuse
(unless you know you *really* will reuse it) can become effort wasted,
because you may not in fact reuse it.
Should that wheel be reinvented?
Well, you don't need to 'reinvent'. Tcl already has packges and
modules, you just need to start package-izing or module-izing your
blocks you currently reuse.
Elders of the Code Church, what is your wisdom?
Reusable code libraries are a good thing. The effort spent to build
(and define) a generic interface usable for multiple apps. to a block
of 'reusable code' will pay for itself over the longer term.
Take a look through the Tcllib source for insights on building reusable
packages/modules.
[1] Don't worry if you don't know what a 'hash function' is right now,
that's not terribly important for this discussion here, and you can
look it up on Wikipedia if you don't know but really want to know.