this post was submitted on 12 Feb 2025
16 points (78.6% liked)

Python

6889 readers
7 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 1 month ago (3 children)

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.

[–] [email protected] 5 points 1 month ago (2 children)

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.

[–] [email protected] 2 points 1 month ago (1 children)

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.

[–] [email protected] 1 points 1 month ago

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.