fornax-navo / fornax-demo-notebooks

Demo notebooks for the Fornax project
https://fornax-navo.github.io/fornax-demo-notebooks/
BSD 3-Clause "New" or "Revised" License
7 stars 19 forks source link

Preference for importing whole modules rather than individual functions #261

Closed troyraen closed 1 month ago

troyraen commented 3 months ago

Our notebooks and code files tend to import individual functions rather than entire modules. In my opinion, there are good reasons to prefer importing modules. I'm opening this issue to explain with an example.

Note: The example is constructed specifically to illustrate my struggles and the details are a bit cherry-picked. This has not caused problems programmatically, and others may have different preferences. I don't mean to discount that. It just occurred to me today that I don't think I've ever elaborated. Happy to close this without action if that's generally preferred.

One reason I prefer importing entire modules is that it makes the import blocks/cells much more concise, which is good for readability. More importantly, it makes later code easier to understand.

For example:

# Importing individual functions

from sktime.registry import all_tags, <... other sktime.registry functions ... \
    ... that may even span ... ... several lines ... ...  \
    making ... ... ... it difficult ... ... to spot the specific one I care about ... ... ...>
# ... lots of other imports of individual functions ...

# ... hundreds of lines of code/text ...

all_tags(estimator_types = 'classifier')
# Now, if I'm reviewing code, I'm thinking:
#     "What is `all_tags`? What does this do? Did we write it?".
# And as a user I might also think things like:
#     "What are the parameter options for this function?"
# I have to scroll all the way back to the top of the notebook and search around just
# to learn where I actually need to go for the answers.
# Then scroll more trying to find my way back to this spot so I can continue.

# Repeat for every other function I have questions about.

versus

# Importing the whole module

import sktime.registry
# ... much smaller set of simple imports ...

# ... same hundreds of lines of code/text ...

sktime.registry.all_tags(estimator_types = 'classifier')
# Now it's obvious who wrote the function, and I immediately know where to go looking for
# its documentation to get the rest of my questions answered without scrolling the notebook.