GrapeJuiceSoda | Sat May 15 01:28:17 PM PDT 2021
pedantic_software_coding_style
/*
* 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."
*/
int lowerCamelCase;
void*
snake_case(int a)
{
}
char* snake_case_p /* always add a '_p' at the end */
typedef struct
{
int a;
int b;
}UpperCamelCase;
Enum
{
UPPERCASE,
UPPERCASE,
}
int f(void) {
int a;
for (a = 0; a < 3; a++) {
int validInForBlock;
validInForBlock = a;
}
{
int scopedVariable;
scopedVariable = 42;
}
}
if (!(p = malloc(sizeof(*p))))
hcf();
typedef struct Foo Foo;
struct Foo
{
int a;
int b;
};
Enum Direction
{
DIRECTION_X = 52,
DIRECTION_Y = 23,
DIRECTION_Z = 7,
};
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)
void function(int);
static void
func(void)
{
puts("....");
}
void*
func(void)
{
puts("....");
}
if (usage)
{
puts("....");
}
while (usage)
{
puts("....");
}
int a = 9;
printf("%d\n", a);
{
printf("Hello ");
printf("World");
printf("\n");
}
free(random_p);
return 0;
int two = 1 + 1;
int fd = -1;
(int)42;
switch (val)
{
case 1:
break;
case 2:
break;
default:
}
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);
}