bonsix/STYLE

125 lines
3.8 KiB
Plaintext
Raw Normal View History

2024-07-19 17:04:49 -06:00
The following guidelines are conducive to clear and readable code that is
consistent with the style of the rest of the Bonsai Computer System.
0. Braces are mandatory for all control flow.
2024-07-19 16:41:02 -06:00
1. Nested indentation should be kept to a minimum.
2024-07-19 16:41:02 -06:00
2. Empty lines should be placed between different kinds of statements:
2024-07-13 23:47:50 -06:00
int t;
2024-07-13 23:47:50 -06:00
assert(io->bufuse > 0);
assert(io->bufuse <= io->bs);
2024-07-13 23:47:50 -06:00
if ((t = write(io->fd, io->buf, io->bufuse)) < 0) {
io->error = errno;
t = 0;
} else if (t > 0) {
memmove(io->buf, &(io->buf)[t], (io->bufuse -= t));
}
2024-07-13 23:47:50 -06:00
io->bytes += t;
io->prec += (t > 0 && io->bufuse > 0);
io->rec += (t > 0 && io->bufuse == 0);
2024-07-13 23:47:50 -06:00
return io;
2024-07-13 23:47:50 -06:00
3. Each block of code should be indented once more than the keyword which
2024-07-19 16:41:02 -06:00
initiated the block:
switch (c) {
case 'e': mode |= EQUAL; break;
case 'g': mode |= GREATER; break;
case 'l': mode |= LESS; break;
default: return usage(s);
}
4. In C, spaces should be placed in control flow statements after the keyword
2024-07-19 16:41:02 -06:00
and before the opening brace:
2024-07-13 23:47:50 -06:00
for (i = 2; i < argc; ++i) {
2024-07-13 23:47:50 -06:00
5. If a function, a C control flow statement, or a Rust macro has arguments that
2024-07-19 16:41:02 -06:00
cause the statement to be broken into multiple lines, this should be done by
placing the arguments on a new line inside the parentheses:
2024-07-13 23:47:50 -06:00
let usage = format!(
"Usage: {} [-d delimiter] index command [args...]",
argv[0],
);
2024-07-13 23:47:50 -06:00
6. If Rust function arguments or fields are on their own lines, they should
2024-07-19 16:41:02 -06:00
always have a trailing comma:
2024-07-13 23:47:50 -06:00
return Err(EvaluationError {
message: format!("{}: Invalid token", i),
code: EX_DATAERR,
})
2024-07-13 23:47:50 -06:00
7. If text is on the same line as a brace, spaces should be placed after an
2024-07-19 16:41:02 -06:00
opening curly brace and before a closing one:
2024-07-13 23:47:50 -06:00
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
2024-07-13 23:47:50 -06:00
8. If a control flow statement is short enough to be easily understood in a
2024-07-19 16:41:02 -06:00
glance, it may be placed on a single line:
2024-07-28 00:12:34 -06:00
if !(argc < 0) { usage(program_name); }
9. In C, note everything you use from a library in a comment subsequent to its
2024-07-19 16:41:02 -06:00
#include statement:
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
* optarg, optind, STDIN_FILENO, STDOUT_FILENO */
2024-07-14 02:15:07 -06:00
2024-07-19 16:41:02 -06:00
10. In Rust, place extern statements after use statements that include standard
library crates. Group alike statements:
use std::fs::Path;
extern crate strerror;
extern crate sysexits;
use strerror::StrError;
use sysexits::{ EX_OSERR, EX_USAGE };
2024-07-19 17:31:51 -06:00
11. Do not use do while loops in C.
2024-07-28 00:34:42 -06:00
12. Adhere to the following rules from the paper The Power of 10: Rules for
2024-07-28 00:12:34 -06:00
Developing Safety-Critical Code [0]:
2024-07-19 17:31:51 -06:00
1. Avoid complex flow constructs, such as goto and recursion.
2. All loops must have fixed bounds. This prevents runaway code.
3. Avoid heap memory allocation.
2024-07-28 00:12:34 -06:00
4. Restrict functions to the length of a single printed page.
2024-07-19 17:31:51 -06:00
6. Restrict the scope of data to the smallest possible.
7. Check the return value of all non-void functions, or cast to void to
2024-07-28 00:34:42 -06:00
indicate the return value is useless (such as in the case of using
fprintf(3p) to print to the standard error).
2024-07-19 17:31:51 -06:00
8. Use the preprocessor sparingly.
9. Limit pointer use to a single dereference, and do not use function
pointers.
10. Compile with all possible warnings active; all warnings should then be
2024-07-28 00:12:34 -06:00
addressed before release of the software (for C compilers, compile with
-Wpedantic).
2024-07-19 17:31:51 -06:00
2024-07-28 00:34:42 -06:00
13. Remember this quote from The Elements of Programming Style by Brian
Kernighan:
Everyone knows that debugging is twice as hard as writing a program in the
first place. So if you're as clever as you can be when you write it, how
will you ever debug it?
2024-07-19 17:31:51 -06:00
References
==========
[0] <https://web.eecs.umich.edu/~imarkov/10rules.pdf>
2024-07-13 23:47:50 -06:00
--
Copyright © 2024 Emma Tebibyte <emma@tebibyte.media>
2024-07-19 17:31:51 -06:00
Copyright © Wikipedia contributors
2024-07-13 23:47:50 -06:00
This work is licensed under CC BY-SA 4.0. To view a copy of this license, visit
<http://creativecommons.org/licenses/by-sa/4.0/>.