6

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?

Kvothe
  • 4,419
  • 9
  • 28
  • 1
    As of version 12.3, you can use the interactive analyzer, in the Menu bar, Evaluation > Analyze Cell or Analyze Notebook. – Ben Izd Jun 10 '21 at 16:57

1 Answers1

6

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

Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • Nice. +1. I think it should say a is uninitialized, but it what the OP asked. – Michael E2 Jun 10 '21 at 16:51
  • 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.) – Kvothe Jun 10 '21 at 17:04
  • @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: https://mathematica.stackexchange.com/a/225038/29 – Rolf Mertig Jun 10 '21 at 17:46
  • This is in product v12.3 – user5601 Jun 10 '21 at 21:40
  • @RolfMertig, would you mind adding an answer using the Workbench in that case. I already installed it (Eclipse + Plugin) following https://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.) – Kvothe Jun 11 '21 at 08:25
  • So I found that by putting the .m in an open mathematica project I could find the unused variables at the "Problems" tab. I also found I could cycle through them using Ctrl + .. Is there an even more efficient way to let the Workbench remove all of these at once? – Kvothe Jun 11 '21 at 08:33
  • @Kvothe I don't know, but you could just call Wolfram support? – Rolf Mertig Jun 12 '21 at 22:04