From: Albert Retey on
Am 27.03.2010 11:11, schrieb Nasser M. Abbasi:
> Hello;
>
> Many times when I write some module, I add local symbols, then later I might
> change the code and those symbols will no longer be used.
>
> For example
>
> f[x_]:=Module[{a,b},... a=...; ....]
>
> Suppose that 'a' is still used, but 'b' is not used inside the module. Both
> will be colored green as things are now.
>
> It would be nice, if a new color is made for symbols that are local, but NOT
> used, i.e not referenced.
>
> This way, after some long editing session, and I need to clean the code, I
> can simply remove those local symbols declaration more easily, by remove the
> symbols with the new color.

the workbench will report unused local variables as warnings. I don't
think that switching to the workbench would be justified for just this
feature, though...


hth,

albert

From: dh on
Hi Nasser,
here is a function that may help you. I would like to pay tribute to the
one who deserves it, but its too long ago that I got this and I can no
more remember who it was.
The code classifies variables according to use:

(*shows used variable sorted according to type*)

Clear[ClassifyVariables];
ClassifyVariables[func_Symbol] := Module[{const, dv, sym, param, locVar,
locVar2, gloVar,hsVar, pat, temp,exc},
(*exc = HoldForm /@ Join[Union[Head /@ ToExpression[Names[]]],
{Head, Constant, Pattern}];*) (*heads*)
const = HoldForm /@ ToExpression[Select[Names[],
MemberQ[Attributes[#], Constant] &]]; (*system constants*)
dv=ToExpression["DownValues["<>ToString[func]<>"]"]; (*DownValues*)
sym = Complement[Cases[dv, x_Symbol :> HoldForm[x], Infinity],
const]; (*Symbols - constants*)
param = Union[Cases[ (*parameters*)
DownValues[func][[All, 1]], x_Pattern :> ToExpression["HoldForm[
" <> StringTake[temp = ToString[x], StringPosition[temp,
"_"][[1, 1]] - 1] <> "]"], Infinity]];
locVar = Union[Cases[dv, (*local variables*)
HoldPattern[Module[vl_List, _]] :> (Map[HoldForm,
HoldForm[vl], {2}][[1]] /. HoldPattern[Set[v_, _]] :> v), Infinity]
//Flatten];
temp = Cases[dv, HoldPattern[Module[_List, b_]] :> HoldForm[b],
Infinity] //Flatten; (*body*)
locVar2 = Complement[locVar, Union[Cases[temp, x_Symbol :>
HoldForm[x], Infinity]]]; (*all local variables not in body*)
(*pat = Complement[Cases[Cases[temp,
x_Pattern :> HoldForm[x], Infinity], x_Symbol :>
HoldForm[x], Infinity], exc]; (*pattern in body*)
*)
hsVar = {}; (*Union[Join[pat, Cases[dv, {s_Symbol, _ ,
_?NumericQ} :> HoldForm[s], Infinity]]];*) (*HiddenScopeVariables????*)
gloVar = Complement[sym, param, locVar, hsVar, const (*, exc*)];
(*global variables*)
{Parameters -> param, LocalVariables -> locVar,
GlobalVariablesUsed -> gloVar,
LocalVariablesNotUsed -> locVar2(*, HiddenScopeVariables ->
hsVar*)} // TableForm
];
Daniel

On 27.03.2010 11:11, Nasser M. Abbasi wrote:
> Hello;
>
> Many times when I write some module, I add local symbols, then later I might
> change the code and those symbols will no longer be used.
>
> For example
>
> f[x_]:=Module[{a,b},... a=...; ....]
>
> Suppose that 'a' is still used, but 'b' is not used inside the module. Both
> will be colored green as things are now.
>
> It would be nice, if a new color is made for symbols that are local, but NOT
> used, i.e not referenced.
>
> This way, after some long editing session, and I need to clean the code, I
> can simply remove those local symbols declaration more easily, by remove the
> symbols with the new color.
>
> Right now, I have to check for each symbol, one by one, by doing Find, to
> see if I am still using it or not.
>
> In practice, I normally like to write small functions, so this is not a
> problem, as I can easily see which symbol is used or not, but sometimes, I
> have to write a large function, and I notice the need for this feature.
>
> May be this feature can be added to version 8? (Unless one can do this now
> in version 7, which I am not aware of).
>
> --Nasser
>
>
>


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh(a)metrohm.com>
Internet:<http://www.metrohm.com>


From: Nasser M. Abbasi on

"dh" <dh(a)metrohm.com> wrote in message news:hoq4gn$16a$1(a)smc.vnet.net...
> Hi Nasser,
> here is a function that may help you. I would like to pay tribute to the
> one who deserves it, but its too long ago that I got this and I can no
> more remember who it was.
> The code classifies variables according to use:
>....

Thanks Daniel ! This is really a very useful function.

I just used it on a large function that I thought I cleaned up, and it found
one local variable not being used, and found 2 which I did not have declared
as local which should have been and were left global.

Too bad we do not know who the original author, they deserve to be thanked
for this function as well.
--Nasser