I have a pretty robust workflow for developing in Notebooks. One element of that workflow is the reflexive habit of saying ClearAll
just before a definition so that experimental definitions don't linger and fool me during iterative, interactive development. For example, suppose I define
foo[x_] := bar[x];
and then later, I decide to change the definition. I overwrite the above with
foo[xs___] := baz[xs];
naively expecting foo
to forget about bar
. But I get a surprise. While foo[1, 2]
produces baz[1, 2]
, foo[1]
produces the old bar[1]
. If I had
written
ClearAll[foo];
foo[x_] := bar[x];
and
ClearAll[foo];
foo[xs___] := baz[xs];
I wouldn't have had the surprise. Obviously, this can be really important in big, complex definitions with many cases and branches all intertwined and infeasible to track in your head, so it's a routine habit and I don't even think about it any more.
The question is whether the same habit should pertain to Package development. I never see ClearAll
before definitions in the Packages I've read, but wouldn't it make sense for the same reason as it does in Notebooks? Or is there some other development fu technique that I don't know about for Packages? Or do people only publish completely finished, formally certified & debugged Packages, and they never need to do iterative development on them ever again (tiny sarcasm)? Or, is there special magic in the Package system that clears symbols on a package reload? Perhaps the subcontexts are automatically discarded?
In short, why don't I see ubiquitous ClearAll
in Packages?
ClearAll
in my packages, even in production code. I can't see how this can possibly hurt, while this leaves me an option to not clear certain symbols, for example because they receive their definitions in more than one file (which doesn't often happen, but sometimes does, and can not always be easily avoided). $\endgroup$