R: Naming Conventions

I haven’t been able to find a good agreed-upon naming convention for R therefore, I attempt to create one, for myself and anyone who is interested.

Consistency and readability, those are the main purposes for naming conventions.
I follow naming conventions to the best of my abilities because in the long run it pays off; other people can read your code and (hopefully) understand what you did, and more importantly, your future self will understand what your past self did.

I. Vectors: singular_lowercase_separated_by_underscore
Vectors are R’s variables, therefore, like in most programming languages, I define them with lowercase words separated by underscores. The singular form (grammatically) is preferred because when we add a vector to a data-frame it forms a column and you don’t want plural column names; I know I don’t.

II. Data-frames: plural CapitalizedWords
Data-frames, are usually the central part of an analysis. They have multiple columns (vectors), and many rows. They represent a set of objects. To give them the attention they deserve, I capitalize them but avoid underscores because as most people believe underscores make capitalized words ugly. Making them plural reminds us that they contain many objects in them.

III. Functions: verbal lowercase.separate.it.by.dots()
Functions are sometimes lowercase and sometimes camel-case in other languages; because camel-case is not very distinguishable from capitalized and because they are very ugly, I prefer lowercase letters. However, R allows us to use dots in naming anything, and dots are very distinguishable from underscores; by using dots we make functions distinguishable from vectors too.
I like the idea of putting one or two letters (e.g., initials like i.) at the beginning of function names to avoid any confusion with functions defined in R’s packages.

IV. Function Arguments: depends on the type
A. Arguments that accept vectors should follow the naming convention of vectors.
B. Arguments that accept data-frames should follow the naming convention of data-frames.
C. Arguments that accept functions should follow the naming convention of functions.
D. Arguments that accept two or more different objects types should follow the naming convention of the object type that is more likely to be used.

Leave a Reply

Your email address will not be published. Required fields are marked *