CodeNewbie Community 🌱

Cover image for Write better functions in Python!!
Vishnubhotla Bharadwaj
Vishnubhotla Bharadwaj

Posted on • Originally published at bharadwaj.hashnode.dev on

Write better functions in Python!!

By seeing the title you may get an idea of what we are going to discuss. So, let's waste no time and dive into the topic. After writing functions for nearly three years, I have figured out some Six points to write them effectively. Here I am going to discuss them as crisp as I can.

Keys to a Good Function.!

  • Sensible naming
  • Has a single responsibility
  • Includes a docstring
  • Returns a value
  • Is no longer than 50 lines
  • Is idempotent and, if possible, pure.

Let's go deep into each concept.

Naming!

Prefer full English words to abbreviations and non-universally known acronyms. The only reason one might abbreviate words is to save typing, but every modern editor has autocomplete. So, you'll be typing that full name once.

Single Responsibility

A function should have a single responsibility. That is, it should do one thing and only one thing. One great reason is that if every function only does one thing. There is only one reason ever to change it.

Docstrings

I have recently come to know about this. And from then onwards for every function, I have written, I have included them.

  • Every function requires a docstring.
  • Use proper grammar and punctuation. Write in complete sentences.
  • Begins with a one-sentence summary of what the function does.
  • Uses prescriptive rather than descriptive language.
  • Can be used as comments also(added advantage).

Return Values

Functions can(and should) be thought of as little self-contained programs. They take some input in the form of parameters and return some results. Parameters are of course optional. Return values, however, are not optional from a Python internals perspective. Even if you try to create a function that doesn't return a value, you can't. If a function would not return a value, the Python interpreter forces it to return none.

Function Length

The length of the function directly affects readability and thus maintainability. So, keep your functions short. 50 lines is a totally arbitrary number that seemed reasonable to me. Most functions you write will(hopefully) be quite a bit shorter.

Idempotency and Functional Purity

An idempotent function always returns the same value given the same set of arguments, regardless of how many times it is called. The result does not depend on non-local variables, the mutability of arguments, or data from any I/O streams.

Pure functions do not have logging statements or print() calls. They do not make use of databases or internet connections. They don't access or modify non-local variables. And they don't call any other non-pure functions.

That's the end friends. These are the things I learned about functions in my coding journey. Hope you enjoyed it. Connect with me on Twitter for more awesome content.

Top comments (0)