I'm building really big modules but I want to break them up. I'm wondering if I can create a module and then go back into it to add more things. Ideally I want to make a function which can take Hold[Module[{x},{x=1}]] and Hold[Module[{y},{y=x+1}]] and produce Hold[Module[{x,y},{x=1,y=x+1}]]. The minimal example where this is failing is that Module[Join[{x},{y}],{x=1,y=x+1}] already throws an error, since Join[{x},{y}] is not a list of variablels. Something about the order of evaluation seems wrong, but I don't know much of the theory of Mathematica.
-
$\begingroup$ Module has the attribute HoldAll. Therefore, you need to write: Module[Evaluate@Join[{x},{y}],{x=1,y=x+1}] $\endgroup$Daniel Huber– Daniel Huber2025-12-18 17:08:15 +00:00Commented 23 hours ago
-
3$\begingroup$ I'm building really big modules but I want to break them up is there a reason why not make your modules small from the start? In old days, we used to have a rule that says if a function does not fit fully on VT 100 terminal screen, then it is too large. VT 100 terminal screen was also very small so If I were you, I will make sure your functions/modules are small to start with. What you are doing above is just code gymnastics which leads to unmaintainable code. do not know if there is tool that does module refactoring for Mma. May be workbench? $\endgroup$Nasser– Nasser2025-12-18 17:45:58 +00:00Commented 22 hours ago
1 Answer
This may be a bit pedantic, but I don't know a better way to approach your question. Your question was also vague, so this is speculative. But anyway...
Breaking big things into small things isn't the job of Module. I'm guessing that you want "functions" (not necessarily Function but all the various mechanisms that model functions in WL). A Module can be part of the implementation of a "function", of course.
It looks like what's going on is that one Module is implemented in such a way that it's dependent on some other Module. In your example specifically, the y in the second Module refers to the x in the first Module. As an aside, neither of your sample Modules does anything, so I'm assuming that's just a consequence of you giving us toy examples, and therefore I'll just ignore that problem for now. Okay, so how would you instead use functions do build up more complex structures?
One way is composition. For example:
funA[x_] := StringForm["y = `` + 1", x];
funB[] := 1;
funA[funB[]]
(* y = 1 + 1 *)
I just made up an effect since your examples don't really show us what you're doing with your symbols.
Or you might be parameterizing, which can be done with SubValues,
funC[x_][y_] := x + y
or by producing Functions
funD[x_] := Function[y, y + x]
Or maybe I'm reading too much into your question, and all that you really need to do is nest Modules:
Module[
{x = 1},
Module[
{y = x + 1},
y]]
Having said all of that, I'm assuming this doesn't actually answer your question, but hopefully it provides some "hooks" onto which you can attach follow up questions/examples to clarify what you're trying to do.