My Coding Style I stole from people

GrapeJuiceSoda | Sat May 15 01:28:17 PM PDT 2021

Inspirations:

cat-v_coding_style

openbsd_coding_style

suckless_coing_style

pedantic_software_coding_style

aiju_coding_style

Comments:


/*
 * Very important single-line comments look like this.
 */


/* single-line comments are all lowercase */


/*
 * Multi-line comments look like this.  Make them real sentences.
 * Fill them so they look like real paragraphs.
 */


/*
 * Avoid obvious comments such as
 * "Exit 0 on success."
 */

Variable Names:

Local variables


int lowerCamelCase;

Function names


void*
snake_case(int a)
{


}

Pointers


char* snake_case_p /* always add a '_p' at the end */

Struct/Union/Enum


typedef struct 
{
    int a;
    int b;
}UpperCamelCase;


Enum
{
    UPPERCASE,
    UPPERCASE,
}

Variable Declarations


int f(void) {
    int a;


    for (a = 0; a < 3; a++) {
        int validInForBlock;


        validInForBlock = a;
    }


    {
       int scopedVariable;


       scopedVariable = 42;
    }
}

Tests and boolean values


if (!(p = malloc(sizeof(*p))))
    hcf();

Structs:


typedef struct Foo Foo;


struct Foo
{
    int a;
    int b;
};

Enums:


Enum Direction 
{
	DIRECTION_X = 52,
	DIRECTION_Y = 23,
	DIRECTION_Z = 7,
};

Pointers:


int *int_p;
short *short_p;
Rect *rect_p;
void(*func_p)(int i);


void *target = (char *)s->elems + (s->logicalLen * s->elemSize);


int a = int_arr[4];
/* NOT */
int a = *(int_p + 4)

Function prototypes:


void function(int);

Functions definitions:


static void
func(void)
{
    puts("....");
}


void*
func(void)
{
    puts("....");
}

Keywords:


if (usage)
{
    puts("....");
}


while (usage)
{
    puts("....");
}

Print statements


int a = 9;


printf("%d\n", a);


{
    printf("Hello ");
    printf("World");
    printf("\n");
}

Return statements


free(random_p);


return 0;

Exiting

Casting

Operators


    int two = 1 + 1;


    int fd = -1;
    (int)42;

Switch case:


    switch (val) 
    {
        case 1:
            break;
        case 2:
            break;
        default:
    }

Sample Code




typedef struct child Child;
typedef struct parent Parent;


void create_parent(Parent *);
void create_child(Child *);
void free_parent(Parent *);
void free_child(Child *);


struct child
{
	int age;
	char *name;
};


struct parent
{
	Child *child;
	int age;
	char *name;
};


int
main(void)
{
    Parent Mom;
    Parent *parent_p;


    *parent_p = &Mom;
    create_parent(parent_p);
    parent_p->name = strdup("Masayo");
    parent_p->child->name = strdup("Kendall");
    {
	    printf("%d\n", parent_p->age);
	    printf("%s\n", parent_p->name);
	    printf("%s\n", parent_p->child->name);
    }
	free_parent(parent_p);


	return 0;
}


void
create_parent(Parent *parent_p)
{
	if (!(parent_p->child = malloc(sizeof(Child))))
	{
		printf("%s:%d: malloc failed\n", __FILE__, __LINE__);


		exit(1);
	}
	parent_p->age = 0;
}


void
create_child(Child *child_p)
{
	child_p->age = 0;
}


void
free_parent(Parent *parent_p)
{
	free_child(parent_p->child);
	free(parent_p->child);
	free(parent_p->name);
}


void
free_child(Child *child_p)
{
	free(child_p->name);
}

Top