7
$\begingroup$

I have some code (that I did not write) that has many unused local variables in Modules. I would like to clean this up. Is there any automated way to do this or to make it more clear that a local variable is unused (it would be great if it could be highlighted in a different color for example)?

I am using the standard notebook environment. Is it possible within that? Or should I be using something else like Workbench for this?

$\endgroup$
1
  • 1
    $\begingroup$ As of version 12.3, you can use the interactive analyzer, in the Menu bar, Evaluation > Analyze Cell or Analyze Notebook. $\endgroup$
    – Ben Izd
    Commented Jun 10, 2021 at 16:57

1 Answer 1

7
$\begingroup$

Wolfram Workbench is nice for this, but you could also try the "CodeInspector`" package: enter image description here

$\endgroup$
7
  • $\begingroup$ Nice. +1. I think it should say a is uninitialized, but it what the OP asked. $\endgroup$
    – Michael E2
    Commented Jun 10, 2021 at 16:51
  • $\begingroup$ Thanks! This looks great! Is there an easy way to refer to a certain input cell instead of having to convert it to a string by hand? Say if In[2] in the above had contained Module[{a,b,c},a+1]. Or alternatively if we want to check if CodeInspectSummarize[#, CodeInspect[#]] &@"Module[{a,b,c},a+1]" contains any unreferenced variables by applying the CodeInspector to the (unevaluated) In[3]. (In the latter case I would expect the answer to be no since the Module is within a string.) $\endgroup$
    – Kvothe
    Commented Jun 10, 2021 at 17:04
  • $\begingroup$ @Kvothe you could save your notebook as a package or use NotebookRead, but I would really use Workbench. I once wrote a installation script, not sure if it still works on all operating systems: mathematica.stackexchange.com/a/225038/29 $\endgroup$ Commented Jun 10, 2021 at 17:46
  • $\begingroup$ This is in product v12.3 $\endgroup$
    – user5601
    Commented Jun 10, 2021 at 21:40
  • $\begingroup$ @RolfMertig, would you mind adding an answer using the Workbench in that case. I already installed it (Eclipse + Plugin) following support.wolfram.com/27221. I simply dragged and dropped a .m file into it where I would like to find the unreferenced local variables. I don't know what to do next. (Edit: Sorry I think I figured it out.) $\endgroup$
    – Kvothe
    Commented Jun 11, 2021 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.