<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Best practice for preventing namespace pollution/cross-talk when calling Functions in an included file in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/557398#M77111</link>
    <description>&lt;P&gt;There are several threads on this topic already, as well as a section in the Scripting Guide, but none really answer my question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two files, Main.jsl and Function.jsl, and I include the latter file in the former. There is a Function defined in Function.jsl that I wish to call inside of a For loop in Main.jsl. The problem I had is that the called Function also uses a For loop, and both the For loops in Main.jsl and the Function in Function.jsl use i as a counting variable, hence this resulted in namespace pollution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried a bunch of combinations with using Names Default Here(1) at different locations in either files, using the &amp;lt;&amp;lt; New Context argument in Include(), and even defining the Function name as a global variable using :: in order to try to solve this issue. However, the problem was always that either 1) I was able to call the Function in Function.jsl from Main.jsl, but there would be namespace pollution, or 2) I would not be able to call the Function in Function.jsl from Main.jsl because the Function was defined in a namespace or scope that I did not know how to access.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current workaround is to explicitly define i and the contents of the entire Function inside of a Local(), and defining the Function itself as a global variable as such:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Main.jsl&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Include("Function.jsl", &amp;lt;&amp;lt; New Context);

Names Default To Here(1);

for(i = 1, i &amp;lt;= 10, i++,
    returns = ::function(arguments);
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Function.jsl&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;::function = Function({arguments},
    Local({i},
        for(i = 1, i &amp;lt;= 20, i++,
            Do Something;
        );&lt;BR /&gt;    Return(returns);
    );
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My question is: Is this really the correct way to go about it? For example, I think it would be better if I could assign a namespace to Function.jsl, and then use this to call the Function instead of messing with global variables, like returns = ns_function:function(arguments);. And ideally this would then also prevent the namespace pollution because the i in Main.jsl is in the namespace Here, while the i in Function.jsl is in the namespace ns_function. But I do not know how/if this is possible.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using JMP Pro 16.2 (570548) on Windows 10 Enterprise 64-bit.&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 16:00:33 GMT</pubDate>
    <dc:creator>AutoSalamander3</dc:creator>
    <dc:date>2023-06-09T16:00:33Z</dc:date>
    <item>
      <title>Best practice for preventing namespace pollution/cross-talk when calling Functions in an included file</title>
      <link>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/557398#M77111</link>
      <description>&lt;P&gt;There are several threads on this topic already, as well as a section in the Scripting Guide, but none really answer my question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two files, Main.jsl and Function.jsl, and I include the latter file in the former. There is a Function defined in Function.jsl that I wish to call inside of a For loop in Main.jsl. The problem I had is that the called Function also uses a For loop, and both the For loops in Main.jsl and the Function in Function.jsl use i as a counting variable, hence this resulted in namespace pollution.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried a bunch of combinations with using Names Default Here(1) at different locations in either files, using the &amp;lt;&amp;lt; New Context argument in Include(), and even defining the Function name as a global variable using :: in order to try to solve this issue. However, the problem was always that either 1) I was able to call the Function in Function.jsl from Main.jsl, but there would be namespace pollution, or 2) I would not be able to call the Function in Function.jsl from Main.jsl because the Function was defined in a namespace or scope that I did not know how to access.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My current workaround is to explicitly define i and the contents of the entire Function inside of a Local(), and defining the Function itself as a global variable as such:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Main.jsl&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Include("Function.jsl", &amp;lt;&amp;lt; New Context);

Names Default To Here(1);

for(i = 1, i &amp;lt;= 10, i++,
    returns = ::function(arguments);
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Function.jsl&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;::function = Function({arguments},
    Local({i},
        for(i = 1, i &amp;lt;= 20, i++,
            Do Something;
        );&lt;BR /&gt;    Return(returns);
    );
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My question is: Is this really the correct way to go about it? For example, I think it would be better if I could assign a namespace to Function.jsl, and then use this to call the Function instead of messing with global variables, like returns = ns_function:function(arguments);. And ideally this would then also prevent the namespace pollution because the i in Main.jsl is in the namespace Here, while the i in Function.jsl is in the namespace ns_function. But I do not know how/if this is possible.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using JMP Pro 16.2 (570548) on Windows 10 Enterprise 64-bit.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:00:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/557398#M77111</guid>
      <dc:creator>AutoSalamander3</dc:creator>
      <dc:date>2023-06-09T16:00:33Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for preventing namespace pollution/cross-talk when calling Functions in an included file</title>
      <link>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/557418#M77113</link>
      <description>&lt;P&gt;In this case you could also function({arguments},&lt;STRONG&gt; {Default Local}&lt;/STRONG&gt;) to declare that all unscoped variables used in the function are local to the function (this is what I do 99% of the time).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//main.jsl
Names Default To Here(1);

Include("test.jsl");

For(i = 1, i &amp;lt;= 5, i++,
testparam = i;
	return = test();
	show(i);
	show(return);
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;//test.jsl
Names Default To Here(1);

test = function({}, {Default Local},
	For(i = 1, i &amp;lt;= 20, i++,
		show(i);
	);
	show(testparam);
	return("done");
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://www.jmp.com/support/help/en/16.2/#page/jmp/programming-functions.shtml?os=win&amp;amp;source=application#ww5013741" target="_blank" rel="noopener"&gt;Function({arguments}, &amp;lt;{local variables}&amp;gt;, &amp;lt;Return(&amp;lt;expr&amp;gt;)&amp;gt;, script)&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2022 16:26:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/557418#M77113</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-10-18T16:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Best practice for preventing namespace pollution/cross-talk when calling Functions in an included file</title>
      <link>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/560005#M77348</link>
      <description>&lt;P&gt;Thank you! With the {Default Local} flag this works exactly as I wanted it to.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Oct 2022 15:25:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Best-practice-for-preventing-namespace-pollution-cross-talk-when/m-p/560005#M77348</guid>
      <dc:creator>AutoSalamander3</dc:creator>
      <dc:date>2022-10-25T15:25:32Z</dc:date>
    </item>
  </channel>
</rss>

