I'm trying to compile a list of what makes a function good. So far, I've gathered the following principles: a good function should have a single purpose, have a sensible name, accept inputs through parameters instead of relying on external state, clearly define its inputs and outputs, maintain a clear relationship between them, avoid unnecessary side effects, be deterministic, always terminate, and effectively communicate its intent. I'd love to hear everyone's thoughts on this and what else might be important when crafting functions!
5 Answers
You’ve got some solid points! But I think avoiding unnecessary side effects can be subjective; sometimes, logging and printing are crucial for debugging, especially in larger projects. Just be mindful of the trade-offs.
Totally! But what’s most important is understanding when and why you’re using those side effects.
I’d add that good functions are well documented. Also, don't be too strict about the rules; sometimes flexibility can enhance readability and maintainability.
Good comment! Functions are a part of a larger system, and thinking about how they fit into that is really key to keeping everything maintainable over time. Always aim for clarity!
Exactly! Plus, a well-structured test suite can make all the difference when changes are made.
A function should keep its job as simple as possible, taking only the most constrained parameters necessary. This helps in making them easier to reason about, though sometimes you may need to break that rule cautiously for performance reasons. It's key to have team discussions about what 'easy to reason about' means for your projects.
For sure! Aligning on these terms really helps in maintaining code quality as a team.
I had a similar experience where we were passing around huge objects when only small bits were needed, making the code a nightmare to manage. Refactoring made a huge difference!
I’d say don’t just focus on functions—consider how they interact with the overall data structures and maintainability. The tests should actually drive how functions are written!
Agreed! Logs can be lifesavers in identifying issues that might not be obvious just from reading the code.