Skip to content

Instantly share code, notes, and snippets.

@davidzchen
Last active March 21, 2020 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidzchen/9188090 to your computer and use it in GitHub Desktop.
Save davidzchen/9188090 to your computer and use it in GitHub Desktop.
Sample C code using a hybrid coding style
// Sample file using a hybrid coding style.
//
// This file is written in a style derived from the Google C++ coding style but
// with some modifications to make it more C-like.
//
// General rules:
// - Indents are two spaces. No tabs should be used anywhere.
// - Each line must be at most 80 characters long.
// - Prefer using C99-style comments
// - File names should be lower_case.c
#include <stdlib.h>
#include <stdbool.h>
// For macros, use ALL_CAPS separated by underscore:
#define FLAG_FOO 0x0
// If a macro's replacement is not just a literal, enclose it in parentheses:
#define FLAG_BAZ (0x1 << 3)
// For constants, use k followed by PascalCase:
const int kStateFoo = 0;
// Type names should be PascalCase. Non-typedeffed struct and enum names should
// be lower_case.
typedef struct linked_list LinkedList;
// Enum values can either look like macros:
typedef enum {
MODE_FOO,
MODE_BAR,
MODE_BAZ,
MODE_QUX
} Mode;
// or they can look like contants:
typedef enum {
kStateFoo,
kStateBar,
kStateBaz,
kStateQux
} State;
// Names of members of structs are lower_case and separated by underscores:
typedef struct sample {
int first_field;
bool second_field;
Mode mode;
State state;
struct sample *next;
} Sample;
// Function names are lower_case. Opening braces come at the next line. This
// only applies to function declarations. For other control structures, the
// opening brace comes at the end of the line.
bool sample_equal(Sample *self, Sample *other)
{
// Local variables are lower_case and separated by underscores.
if (self == NULL && other == NULL) {
return true;
}
if (self == NULL || other == NULL) {
return false;
}
// For statements that span multiple lines, break after the logical operator
// and align each line with the start of the first line.
if (self->first_field == other->first_field &&
self->second_field == other->second_field &&
self->state == other->state &&
self->mode == other->mode &&
self->next == other->next) {
return true;
}
// If the previous block ends with areturn (or break or continue), do not
// follow it with an else.
return false;
}
// For function declarations that span multiple lines, then align subsequent
// lines with the first parameter.
Sample *sample_new(int first_field,
bool second_field,
Mode mode,
State state,
Sample *next)
{
Sample *sample = (Sample *) malloc(sizeof(*sample));
if (sample == NULL) {
return NULL;
}
memset(sample, 0, sizeof(sample));
sample->first_field = first_field;
sample->second_field = second_field;
sample->mode = mode;
sample->state = state;
sample->next = next;
return sample;
}
Sample *sample_clone(Sample *sample)
{
if (sample == NULL) {
return NULL;
}
// For function calls that span multiple lines, align each subsequent line.
return sample_new(sample->first_field,
sample->second_field,
sample->mode,
sample->state,
sample->next);
}
// For function declarations (and function calls) where you cannot fit
// the parameters with the first after the opening parentheses, then align
// the parameters indented four spaces on the next line:
static void sample_do_something_with_a_long_name(
Sample *sample,
int parameter_with_a_long_name,
bool another_parameter,
int another_parameter)
{
if (sample == NULL) {
return;
}
// else and else if comes after the previous closing brace and not on the
// next line.
bool local_variable;
if (parameter_with_a_long_name == kStateFoo) {
local_variable = true;
} else {
local_variable = false;
}
sample->first_parameter += another_parameter;
sample->second_parameter |= local_variable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment