When should I Modularize code in notebooks

Question:

I m aware that code can be modularized in Databtricks notebooks, (with the commant %run) but I m not sure when to do it.(at what point)

Currently I have a notebook that does a specific task(tha is to say, it does not make sense to divide the notebook into parts) but in turn this notebook use many funcions, so many that is a very loong notebook which makes it a bit confusin in my opinion, I donĀ“t know if I should modulaize it and put the funcions apart in another notebook that I later call.

When is this a good practice ans when not ?

currently notebook:

 def funct1():
    ...
 def funct2():
    ...
 .. ..
 # -- kind of main --
 # code where I run the functions.

At what point should I switch to another approach:

%run notebook_where_I_have_the_functions.py
 # code where I run the functions
 funct1()
 funct2() 

Answers:

I would define functions in the notebook when it is interesting or useful to see them there. For example, if I need to modify the function, or the notebook is quite focused on its functionality.

On the other side, if I have, lets say, a list of implemented algorithms and I’m comparing them in the notebook, it might make more sense to just import that code into the notebook instead of defining it inside, as my notebook is more oriented to the "comparing" part than to the "algorithmic".

It could happen that your notebook is focusing on some functionality that you implemented in a 1000 lines function directly there. In this case, it might be harder to choose, but in my opinion, notebooks are not the best place for 1000 lines functions.

Answered By: victorperezpiqueras