Write better functions in Python!!
Tags: python, 100daysofcode, watercooler
Published: 2021-06-15 00:14:53

---
title: Write better functions in Python!!
published: true
date: 2021-06-15 00:11:16 UTC
tags: Python,100DaysOfCode,watercooler
canonical_url: https://bharadwaj.hashnode.dev/write-better-functions-in-python
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/al0qbxiy56dblf6n026f.png
---

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](https://twitter.com/Bharadwaj6262) for more awesome content.