this post was submitted on 12 Feb 2025
16 points (78.6% liked)
Python
6697 readers
23 users here now
Welcome to the Python community on the programming.dev Lemmy instance!
π Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django π¬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
π Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
π Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
β¨ Python Ecosystem:
π Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- PythΓΆrhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Meh.
I'll agree, docstrings are better for documenting a function than just a comment.
However, the author seems to jump through hoops in the next example to break one function into four, just to avoid some single line comments. Unless those code blocks make sense as functions (they're used/duplicated elsewhere), you're just making work for yourself. Why not turn it into 12 functions? One for each line of code?
I'm reminded of the admonition that there are only two hard problems^*^ in computer science -- cache invalidation, and naming things. The more functions you have, the more things you have to name.
The rest of it -- name your magic numbers, use tuple unpacking, comment "why" instead of "what" -- is good practice. I'm just not a fan of making functions just to avoid writing a comment.
^*^ And off by one errors.
single use functions are fine; I often break 20+ line functions apart and it makes it easier to test and reason about, it's not just to avoid comments: block comments are just a sign that the function might be getting too complex.
On the other hand, I often have wished that the author of the code I am reading had just kept their original 20 line function around instead of splitting it up into a zillion little functions that force me to constantly jump around to figure out what is actually going on.
it's turtles all the way regardless; but it's much easier to handle side effects if you have more numerous but smaller functions.
I prefer that because fully reading a module or component is not the most common scenario. The most common use case of reading code is (or should be) not caring about most of the implementation details until you need to; only then you have to go down the rabbit hole.
Longer functions force the reader to understand most of their context every time they get there, a problem especially when the function has a bunch of local vars.
I agree completely that, when done well, smaller functions can make code easier to work with for all of the reasons that you have mentioned. When not done well, however, I still have to read through all of the same code to figure out which part has the implementation detail that I need to care about, but now it has been scattered about instead of collected into one convenience place.