Deutsch   English   Français   Italiano  
<vn1nit$2l472$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Rich <rich@example.invalid>
Newsgroups: comp.lang.tcl
Subject: Re: Should I write building blocks?
Date: Sat, 25 Jan 2025 03:59:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 86
Message-ID: <vn1nit$2l472$1@dont-email.me>
References: <20250124203441.7a1b1c34@lud1.home>
Injection-Date: Sat, 25 Jan 2025 04:59:26 +0100 (CET)
Injection-Info: dont-email.me; posting-host="c193d54e1ec2d9f9d63f4858590cfbf6";
	logging-data="2789602"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19UU0/anxN1eOJq19nbLMrI"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:aV+lPe2ILYvHJz4TtFuK9ibCokQ=
Bytes: 4514

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.