The NetBSD coding style
guide,
also known as Kernel Normal Form (KNF), suggests to prefix a struct’s
members with a string that represents the structure they belong to. For
example: all struct tmpfs_node members are prefixed by tn_ and all
struct wsdisplay_softc members start with sc_. But why there is such
a rule? After all, the style guide does not mention the reasons behind
this.
The first reason is clarity. When accessing a structure instance, whose
name may be anything, seeing a known prefix in the attribute helps in
determining the variable’s type. For example, if I see foo->sc_flags,
I know that foo is an instance of some softc structure. As happens
with all clarity guidelines, this is subjective.
But there is another reason, which is not more technical. Using unprefixed names pollutes the global namespace, a specially dangerous situation if the structure belongs to a public header. Why? Because of the preprocessor — that thing that should have never existed — or more specifically, the macros provided by it.
Let’s see an example: consider a foo.h file that does this:
#ifndef _FOO_H_
#define _FOO_H_
struct foo {
int locked;
};
#endif
And now take the following innocent piece of code:
#define locked 1
#include <foo.h>
Any attempt to access struct foo’s locked member will fail later on
because of the macro definition. Prefixing the variable mitigates this
situation.
(Content also available at: gemini://capsule.jmmv.dev/post/2006/04/netbsds-knf-prefixes-for-struct.gmi)