PingCAP Style Guide

Guide to writing idiomatic code at PingCAP and in the TiKV project.


Project maintained by pingcap Hosted on GitHub Pages — Theme by mattgraham

Functions, methods, and traits

There is often a choice between using a type’s concrete name and using Self, e.g., in function signatures or when qualifying names. In general, use Self when the meaning is ‘the current type’, and the explicit type name when the meaning is ‘that specific type’. Some examples:

If in doubt, use the explicit type name.

Functions

Methods

Generics

Flexible function arguments

For each of these flavours of flexible function arguments, there is a trade-off. The generic form is more flexible. However, the generic form makes the function signature more difficult to read, can cause code bloat, adds boilerplate to the signature and body, and in some rare cases generates less performant code.

Some things to consider:

Some examples of this theme:

Traits

Associated types

Generic type parameters and associated types are not the same. Generic type parameters are chosen by the user of a trait. Associated types are chosen by the implementer of a trait. For more details see the book.

It is often useful to supply a default type for associated types. If you add an associated type to a trait, using a default type makes the change backwards compatible.

<< Implementing traits | Macros >>