Skip to content

Factorize similar code in Tcl source files - #752

Open
xdelaruelle wants to merge 28 commits into
envmodules:mainfrom
xdelaruelle:tcl-loc-reduction
Open

xdelaruelle wants to merge 28 commits into
envmodules:mainfrom
xdelaruelle:tcl-loc-reduction

Conversation

@xdelaruelle

Copy link
Copy Markdown
Collaborator

This series removes code that was written several times in the tcl/ source files, without changing behavior. Each commit handles one duplication, for a net reduction of 541 lines.

  • Shell code rendering: X resource set and unset, true and false values, and the bash/bash-eval setup of sh-to-mod each go through a single code path.
  • Sub-interpreter setup is shared between modulefile, modulerc and cache file evaluation.
  • Modulefile commands: module-forbid, module-hide and module-warn share one option parser, argument dispatch of getenv, getvariant and puts is simplified, and the whatis and scan mode procedures of environment variable commands share their variable declaration code.
  • Loaded environment: regular and No Particular Order dependency structures are handled by the same code, as are tags and extra tags when caching loaded modules. The variant report branches of getVariantList are merged.
  • Sub-commands: display, test and help share their evaluation loop, and collections are read the same way by restore and saveshow, initial environment included.
  • The five procedures selecting an alternative definition of a procedure (modEq, modVersCmp, ...) share their renaming code.
  • Identical switch arms are merged and redundant existence tests removed in a few other places.

The alternative definitions of the module comparison procedures themselves are left as they are, since they are duplicated on purpose for speed.

As helper procedures are introduced, the procedure call traces reported in -DD mode differ.

Report modes 1, 5 and 6 on one side and 7 on the other were handled
by two branches repeating the same boolean, shortcut and value variant
distinction and the same tracking of the kind of variant reported to
build output key. Only the shape of the element appended to the result
differs between these modes. Handle them in a single branch, which
determines variant name set and value once, then track variant kind
with a single statement.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Both kinds of rc file are evaluated and recorded the same way in
findModulesFromDirsAndFiles. The .version file is just additionally
skipped when found at a different depth level than search targets.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
cmdModuleDisplay and cmdModuleTest procedures were identical apart
from the evaluation mode set and the title reported prior each module
evaluation. Introduce displayModuleEvalList procedure to hold this
common code.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Number of arguments tells if a collection name has been provided and
default collection name is obtained by appending it to the argument
list.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Shell command and output parsing definitions were duplicated for bash
and bash-eval shells, whereas only the way script is evaluated differs
between them.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Dependency relations between loaded modules are recorded twice: once
taking loading order into account and once in No Particular Order.
Procedures setting and unsetting these relations repeated the same
code for each set of structures. Loop over both sets instead.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Evaluation of modulefile, modulerc and cache file each embedded a copy
of the code creating the sub-interpreter at first use, dumping its
initial state then resetting it prior each evaluation. Definition of
the variables to set in the interpreter, including those coming from
siteconfig, was also duplicated between modulefile and modulerc.

Move this code into two procedures working from the name prefix of
the global structures of each interpreter kind.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
These three modulefile commands parsed their specific options through
similar loops, each one reporting the same invalid option and missing
value errors. Introduce a procedure that parses arguments from a
description of the options supported by the calling command.

module-tag keeps its own loop as its first argument that is not an
option is the tag to set rather a module specification.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Help sub-command held a copy of the loop shared by display and test
sub-commands, with its own evaluation mode and title.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Procedures rendering a true and a false value were made of the same
switch over shell kinds, where only the rendered value differs. Define
both values in a single switch and pick the one to render.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
An X resource string is unset by merging it with an empty value, so
shell code rendered to unset a resource was a copy of the code setting
it, value excepted. Render both through a single procedure.

Resource string is still rendered without escaping when unset on sh,
csh and fish shells.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Rely on the unset command option that ignores undefined variables
rather testing variable existence beforehand.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Variant list is expanded into the designation whether it is empty or
not, so there is no need to test it first.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Procedures testing if a module is loaded or is loading with other
variants than those specified only differed by the loaded or loading
state they look at. Keep one procedure with an argument to select
this state.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
List of tags already known for a module was passed to the procedure
collecting the tags matching this module, which appended to this list
but never read it.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Tags and extra tags recorded in environment for loaded modules were
cached by two loops only differing by the property read and the
procedure called to record it.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Both callers of the procedure reading collection content handled the
specific case of the initial environment, read from an environment
variable rather a file, then checked collection read was valid. Move
these two steps into this procedure.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Arguments were dispatched through a switch on their number, with
nested tests on the first one. The only thing varying is the
--return-value option that may be set first: detect it then assign
remaining arguments.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Channel targeted by puts modulefile command was determined by two
switches, one per number of arguments, the second one being dedicated
to the -nonewline option. Detect this option first then use a single
switch to select channel.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
shell and shelltype actions only differ by the state queried, which is
named after the action. trace and tracepat return the same value.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Modulefile commands changing an environment variable do not alter
environment when evaluated in whatis or scan mode. Their procedures
dedicated to these modes all indicated variable name to configured
traces then defined variable if undefined. Do both in the procedure
defining the variable, renamed accordingly.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Loop testing that all modules depending on a module are part of the
unloading list is what the procedure testing if a list has elements
not found in another list does.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Lines of an error message and lines of a recorded message block were
padded by loops tracking first line to treat it differently. Joining
lines with a padded newline does the same.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Each arm of the switch on message severity set the same three
variables.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Both kinds of element were handled by the same code, the path of the
file to evaluate excepted.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
Unsetting an array that does not exist is not an error.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
The five procedures that select, depending on configuration, the
alternative definition of a procedure all renamed it into its generic
name the same way: previous alternative gets its name back, debug
trace is moved and selected alternative is recorded. Move this code
into one procedure.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
-a is a synonym of --all on search and listing sub-commands. Both arms
held a copy of this sub-command list.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Xavier Delaruelle <xavier.delaruelle@cea.fr>
@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant