Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Last active December 20, 2015 09:30
Show Gist options
  • Save whyrusleeping/6108204 to your computer and use it in GitHub Desktop.
Save whyrusleeping/6108204 to your computer and use it in GitHub Desktop.
examples of the C coding style i am going to enforce, for future students
/*
surround a binary operator (particular a low precedence
one) with spaces; don't try to write the most compact
code possible but rather the most readable.
*/
int i = 5 + 3;
//NOT
int i=5+3;
/*
Keep the opening brace on the same line as the conditional
this is referred to as Egyptian brackets, and i like them
*/
if (i == 4) {
...
}
//NOT
if (i == 4)
{
//This just looks bad, dont do it.
}
/*
put braces around single-line blocks (e.g., `if', `for',
and `while' bodies).
*/
if (1) {
dothisthing();
}
//NOT
if (1)
dothisthing();
/*
variable and function names should all be lowercase, and SHOULD use underscores between words.
*/
/*
enum or #defined constants should be Uppercase (or UPPER-
CASE).
*/
#define HELLO 5
//NOT
#define hello 5
/*
follow the standard idioms: use `x < 0' not `0 > x', etc.
*/
if (x < 0)
//NOT
if (0 > x)
//And please no
if (!x) //or similar, be clear about what you mean
/*
Please, Please, Please no super-lines.
if the line is much more than 80 characters wide, break it up
*/
if (this && that && whatsthis && whosthere || (go_to_gym ? whatever : no) && (this_is || too_long_please_end_the_line_people_might && dir) {
//Can be written with appropriate newlines, like so
if (this && that && whatsthis && whosthere ||
(go_to_gym ? whatever : no) &&
(this_is || too_long_please_end_the_line_people_might && dir) {
//And honestly, if youre going to /seriously/ write a conditional like that,
//find a good tall building and leap from it.
/*
if you write code that looks like the code in this link, youll probably fail.
https://gist.github.com/whyrusleeping/5887111
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment