Naming conventions

From Rosetta Code
Revision as of 13:57, 30 August 2022 by Razetime (talk | contribs) (add bqn)

BQN

BQN uses a context-free grammar, a result of which it has explicit naming conventions defined in its interpreter. The basic types of values can be distinguished by their first and last characters as follows:

# Subjects (arrays, numbers, characters, etc) start with a lowercase letter:
var3
arr1,2
# Functions start with an uppercase letter:
Fun{𝕨+𝕩}
Avg+´÷≠
# 1-modifiers start with an underscore and do not end with an underscore:
_mod{𝔽𝕩}
# 2-modifiers must start and end with an underscore:
_2mod_{𝔾𝕨𝔽𝕩}

C

Base language
  • All reserved words and operators are lower-case. e.g. while, for, if, sizeof and return etc.
Libraries

Constants that appear in C "header" files are typically in upper-case:

O_RDONLY, O_WRONLY, or O_RDWR. O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC

Note also that there are remnants of some historic naming conventions in C where constants were required to be 8 characters or less. The "O_CREAT" constant is an example.

Types are often suffixed with a "_t", e.g. size_t, and "private" types and arguments are prefixed with "__":

extern size_t fwrite (__const void *__restrict __ptr, size_t __size,
                      size_t __n, FILE *__restrict __s) __wur;

However there are some instances where types use all upper-case. The classic is the type FILE.

In C, the standard library for floating point is focused on double precision, hence the function "cos" is for double precision, and a suffix of "f" and "l" indicate single precision and quad precision respectively.

#include <math.h>
double cos(double x);
float cosf(float x);
long double cosl(long double x);

Whereas for complex variable a prefix of "c" is added.

#include <complex.h>
double complex ccos(double complex z);
float complex ccosf(float complex z);
long double complex ccosl(long double complex z);

This prefix/suffix convention extends to other standard c library function, for example in the following the "f" suffix indicates that an argument is a format string, the prefixes of "s", "v" and "n" hint at other argument types:

#include <stdio.h>

int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

#include <stdarg.h>

int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
Function names

Originally names of C function names were short and in lower case, such as qsort or strcpy. Longer names, perhaps originating from user code or 3rd party libraries would be spelled with snake_case, for example, btree_insert

As CamelCase became popular with the introduction of object-oriented style (namely C++ adopting CamelCase from Smalltalk) CamelCase made its way into C, and became somewhat normalized with the introduction of the Windows API, e.g. MessageBox and LoadLibrary. It's now just as likely that user code may opt to use CamelCase, such as, for example, BTreeInsert.

Quirks

The Unix C standard library uses a prefix of "_" to "hide" raw Operating System calls from the non systems-programmer