🌐 이 글의 한국어 원문이 있습니다.한국어 원문 보기 →

· 10 min read

How Does Google Say to Write Docstrings?

While writing docstrings for functions in Python, I thought it would be good to build the habit around a more widely used format, so I looked at Google's 'Python Style Guide'.

https://google.github.io/styleguide/pyguide.htmlgoogle.github.io

Why the Google Style Guide?

First, some background: at Google, 'Python' is the 'main language' among interpreted languages (Google uses the term 'dynamic language'). Many people at Google use Python, and they often have to read (review) and communicate about Python code together. And since Google is a very large organization, the 'style guide' it maintains also leans toward 'universality'.

But (Python) developers each have their own coding habits, and there are reasons they developed those habits (usually stemming from the characteristics of the language or their development environment). So when an instruction in the guide doesn't make sense to them, they may be inclined to reject the style guide.

Google seems well aware of this, because the style guide spells out the 'pros' and 'cons' of each item. The explanations are simple and concrete, and I found them easier to understand from a practical standpoint than the Python community's style guide, so I decided to use the Google style guide as my reference.

Going through the whole style guide line by line and making it my own would be very rewarding, but for now what I care about is comments, specifically docstrings (the description block for a function or class, written on the line after the function or class name, opened and closed with three single quotes ''' or three double quotes """), so I only looked at that section.

So first, let's look at function docstrings. The Google style guide groups functions, methods, and generators together and calls them all 'functions'.

When to Write a Function Docstring

A function must have a docstring, unless it meets all of the following criteria:
* not externally visible
* very short
* obvious

Google doesn't require a docstring for 'every function'. But if a function fails even one of these three conditions, it must have a docstring.

Not externally visible

For example, when writing a 'module', this refers to a method or function that isn't provided to the people (clients) who use that module. The condition is that the function is 'hidden' from users. In other words, once a function is exposed to 'users', it must have a docstring, no exceptions.

Very short

Some functions just wrap a few short but repeated lines. Put another way, if the code isn't short at a glance, you should write a docstring.

It would be nice if there were an explicit standard for 'short code', but since there isn't, I suspect it's somewhat relative from developer to developer. It would be good if another style guide mentioned a standard for 'short code'; on this point, analyzing the 'code' Google has written would probably give a clearer answer.

Obvious

There are probably 'two parts' to being obvious. One is that the 'function name' should be obvious, and the other is that the 'code as written' should be obvious. The word 'obvious' above should cover both.

Based on these three conditions, you can skip a docstring only for functions that are hidden from the users of the module or class, very short, and obvious. If not all three are met, you should write a docstring.

Just looking at the style guide's conditions for 'writing docstrings', a developer who finds docstrings tedious will try, whenever possible, to write 'short code' and make it 'obvious'. That alone nudges developers toward writing 'simple and obvious code'.

So what goes into a docstring? Let's look at the following description.

A docstring should give enough information to write a call to the function without reading the function’s code. The docstring should describe the function’s calling syntax and its semantics, but generally not its implementation details, unless those details are relevant to how the function is to be used.

It explains that a docstring should provide enough information to use the function without reading the code inside it. This includes the function's calling syntax (syntax and its semantics). And unless it's something you need to know to use the function, there's no need to describe the function's internal implementation in detail.

Coincidentally, this is also covered in MIT's Software Construction (6.005) course.

What a specification may talk about
A specification of a method can talk about the parameters and return value of the method, but it should never talk about local variables of the method or private fields of the method’s class. You should consider the implementation invisible to the reader of the spec.

The explanation of a specification in that material tells us what should be written in a docstring. It says not to put the internal implementation in the specification (docstring). MIT teaches how to write specifications starting from its introductory programming course (MIT 6.0001). In other words, writing a docstring is part of 'writing software'.

But there's a difference here between MIT's 'Software Construction' course and the 'Google docstring style guide'. MIT's Software Construction course is based on 'Java' and assumes a setting where the developer (the method's user) 'cannot see' the method's code. Google, on the other hand, asks for a 'docstring' on any code that isn't short or isn't obvious, even if the reader isn't someone who uses that method.

Why Does Google Want Docstrings Even for 'Internal Code'?

Someone else may modify the code I wrote later

It goes without saying, but if the author of the code leaves the company, a successor may have to come in and maintain it. The successor will naturally add what's needed to the predecessor's code and modify it, and in the process they'll end up using the 'functions' the predecessor wrote. Here, the 'user of the function' can be that successor.

However, I don't think the reason for writing docstrings stops at 'the people who use the function'. Let me add some thoughts I've come to have from studying the MIT courses and writing docstrings according to the Google style guide.

Writing a docstring is part of 'constructing a function'

What I learned from writing docstrings according to the Google style guide is that writing the docstring makes the function's 'purpose' more concrete. Sometimes when writing functions, there isn't enough time to thoroughly review the existing ones. In those cases you have no choice but to infer from the 'function name' alone without opening the code, and as you keep writing, you can end up duplicating functionality that already exists in another function.

On the other hand, if 'writing the docstring' is part of writing the function, you think through what the function's 'purpose' is and 'how it will be used', and write that down in the docstring. Because its purpose and usage have been 'made concrete' in your head once, it helps a lot in telling functions apart by 'use' and 'purpose'. Implementation aside, you can think ahead about the 'scope of code' that will go into this function, which makes it easier later to decide whether to add code to an 'existing function' or 'add a new function'. Naturally, the cohesion of each 'function' improves as well.

Judging from my own experience, 'writing docstrings' is 'part' of development. Writing a docstring means defining the 'purpose' and 'use' of the function I'm about to use, and through that, more precise and well-organized software can be built.

Yet I wonder whether programming education in Korea is still focused only on 'implementing features' in code. The development culture also seems to strongly hold that you don't need to bother writing docstrings for 'functions you'll use yourself'. I think good coding habits should be taught from the 'budding coder' stage, guiding people onto the fast track to becoming good developers.

Related posts

Home