quan m. nguyen

(WordPress) and naming things

In the course of my C programming (the pinnacle of which has been work on the Linux kernel), I’ve grown accustomed to the Linux kernel coding style.

It’s great. Now every bit of C I’ve written follows, more or less, this standard. It feels clean and crisp. It addresses naming conventions (i over loop_counter, for instance), but it doesn’t address a more subtle issue—the naming of functions that perform an action.

There are two obvious sides to this issue: either verb-object (VO) or object-verb (OV). With the verb as “set” and the object as “foo”, would the corresponding C function be set_foo() or foo_set()?

Examining English, a subject-verb-object (SVO) language, it’s pretty clear that the preferred form would be set_foo(). Of course, there are other languages that are principally SOV, but the vast majority of C is written in English, so it is probably bound to follow English’s conventions.

In other programming languages, there can be explicit conventions. Java’s setter and getter methods are the first examples to come to mind. We would have setFoo() and getFoo(). Of course, because Java is object-oriented, we understand that there is a usually subject (i.e. the instantiated object) that comes before it, so it is appropriate to retain the SVO form: bar.setFoo().

But there are examples where it would be clearer to use the object in question first. The Linux kernel provides a atomic.h file, which specifies function prototypes for some atomic operations. All function names begin with “atomic”: atomic_read(), atomic_set(), etc. Something similar happens, albeit with less consistency, with x86’s pgtable.c file.

It’s painfully clear that people establish conventions and maybe even flip-flop between one and the other. I personally can’t decide, and I still throw my hands up in resignation when it comes to naming things other than functions.