2
$\begingroup$

It is often the case sometimes we write some functions that turns out to be handy and we would like to reuse them later. My current solution is to keep them in a single notebook and copy them to every project. Clearly this is bad for version control. I also thought about using package but every time you want to make some change you have to dig into the package folder and reload everything. I'd appreciate any idea on this, thanks.

$\endgroup$
7
  • 1
    $\begingroup$ You can keep them in a notebook and use NotebookEvaluate[...path...] it's almost like using a package with a little less overhead, and slightly easier than copying the code. I use that for a few odds and ends I don't feel the need to write into a package. Though this is a practice, I think calling it "best practice" would be a stretch :) $\endgroup$
    – N.J.Evans
    May 9, 2019 at 17:42
  • 3
    $\begingroup$ What's any different between updating a package and updating a notebook? Having all your code in a centralized place is, as you say, better for VCS but you can also now use the code more easily by just calling Get. Best practice is definitely just making a package out of it. $\endgroup$
    – b3m2a1
    May 9, 2019 at 17:55
  • 1
    $\begingroup$ You can use DefineResourceFunction and LocalObject to do this, although the exact incantation escapes me at the moment. Then you can do ResourceFunction["myfunc"] and use your function anywhere locally. $\endgroup$
    – Carl Lange
    May 9, 2019 at 17:55
  • $\begingroup$ @b3m2a1 Indeed they are similar but when using package we are dealing with context scopes. A real life scenario is when we write some functions and realize it'll be useful in the future. At this stage the definition is already in the Global context so adding it to the package would result in conflicts. This is not a huge problem that can be easily resolved by clear the definition in Global context. $\endgroup$
    – Louis Yu
    May 9, 2019 at 19:37
  • 2
    $\begingroup$ @L.Yu You can certainly do that. You don't actually need ResourceFunction for that - you can just use CloudPut and CloudGet. $\endgroup$
    – Carl Lange
    May 9, 2019 at 19:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.