-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Placing spaces
Betty coding style for use of spaces depends (mostly) on function-versus-keyword usage.
Use a space after (most) keywords.
The notable exceptions are sizeof
, typeof
, alignof
, and __attribute__
, which look somewhat like functions.
So use a space after these keywords:
if, else if, switch, case, for, while, return
but not with sizeof
, typeof
, alignof
, or __attribute__
.
Keyword | Space After | Example |
---|---|---|
if |
Yes | if (condition) |
else if |
Yes | else if (condition) |
switch |
Yes | switch (variable) |
case |
Yes | case value: |
for |
Yes | for (i = 0; i < 10; ++i) |
while |
Yes | while (condition) |
return |
Yes | return (1); |
sizeof |
No | sizeof(struct file) |
typeof |
No | typeof(variable) |
alignof |
No | alignof(variable) |
__attribute__ |
No | __attribute__((unused)) |
Do not add spaces around (inside) parenthesized expressions.
This example is bad:
s = sizeof( struct file );
When declaring pointer data or a function that returns a pointer type, the preferred use of *
is adjacent to the data name or function name and not adjacent to the type name.
Examples:
char *str;
unsigned int sample(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
Use one space around (on each side of) most binary and ternary operators, such as any of these:
= + - < > * / % | & ^ <= >= == != ? :
but no space after unary operators:
& * + - ~ ! sizeof typeof alignof __attribute__ defined
no space before the postfix
increment & decrement unary operators:
++ --
Example:
int i;
i = 0;
i++;
i--;
no space after the prefix
increment & decrement unary operators:
++ --
Example:
int i;
i = 0;
++i;
--i;
and no space around the .
and ->
structure member operators.
Do not leave trailing whitespace at the ends of lines.
Some editors with smart
indentation will insert whitespace at the beginning of new lines as appropriate, so you can start typing the next line of code right away.
However, some such editors do not remove the whitespace if you end up not putting a line of code there, such as if you leave a blank line.
As a result, you end up with lines containing trailing whitespace.
Git will warn you about patches that introduce trailing whitespace, and can optionally strip the trailing whitespace for you; however, if applying a series of patches, this may make later patches in the series fail by changing their context lines.
0.1 - Betty-style usage
0.2 - Betty-doc usage
0.3 - References
1.1 - Indentation
1.2 - Breaking long lines and strings
1.3 - Placing Braces
1.4 - Placing Spaces
1.5 - Naming
1.6 - Functions
1.7 - Commenting
1.8 - Macros and Enums
1.9 - Header files
2.1 - Functions
2.2 - Data structures
3.1 - Emacs
3.2 - Vim
3.3 - Atom