Strategies to re-engineer someone else's notebook?
- To: mathgroup at smc.vnet.net
 - Subject: [mg116693] Strategies to re-engineer someone else's notebook?
 - From: "W. Craig Carter" <ccarter at mit.edu>
 - Date: Thu, 24 Feb 2011 06:22:19 -0500 (EST)
 
Mathgroup,
This is perhaps an odd but practical query, and I am guessing that some 
of you have tackled similar.  Sorry for the lengthy question below.
What is a plausible strategy to "improve" a working notebook, where the 
notebook is written in the way many beginners-to-Mathematica write 
notebooks?
The user that I am helping runs her notebook by changing one variable, 
evaluating the notebook, killing the kernel, changing another variable, 
evaluating the notebook....
For a concrete and artificial (hopefully amusing) example that is 
representative of about 1000 lines of code that probably started its 
life as a giant spreadsheet: the target notebook to be fixed might look 
like this
(**********)
hamburgerMass = proteins + veggies + starch;
hamburgerCalories = proteins proteinCalorieDensity + veggies veggieCalorieDensity + starch starchCalorieDensity;
meat = 12;
lettuce = 8;
cheese = 13;
pickles = 1;
bun = 0.5;
proteins = If[CheeseBurger,cheese + meat,meat];
veggies = lettuce + pickles;
starch = bun;
hamburgerCosts = meat meatCosts + veggies veggieCosts + starch starchCosts
(*etc etc. Output from expressions at line 1000 become very long*)
(******************)
Sometimes the user wishes to know only the number of calories, sometimes a pie chart of calorie contributions, and sometimes things like:
=
ParametricPlot[{hamburgerMass/.meat->x,hamburgerCalories/.meat->x},{x,0,1}] (*with all other variables assigned*).
My current strategy is to leave out any assignments, evaluate the 
notebook from a fresh kernel, and then find the dependencies of 
"derived" expressions using  a function like this:
vars[expr_] :=  Union[Cases[expr, a_ /; (! NumberQ[a] && AtomQ[a]), 
Infinity]]
which gives all the symbols on which expr depends,
and then use these independent variables to construct functions by 
cutting and pasting.  Tedious, but a path towards doing something useful 
with Manipulate.
Another approach may be to make everything a delayed assignment; another 
would be to make everything Dynamic.
Any ideas how I might do this with less tedium and error?  Also, if the 
naive-notebook generator wishes to change a single line, I'd like to be 
able to re-engineer her notebook without duplicate labor.
Thanks, Craig