2024-09-05 12:44:14 -06:00
|
|
|
|
“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?”
|
|
|
|
|
– Brian Kernighan, The Elements of Programming Style
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
|
|
|
|
Use
|
|
|
|
|
===
|
|
|
|
|
|
|
|
|
|
0. A single line for control flow statements short enough to be easily
|
|
|
|
|
understood at a glance:
|
2024-07-19 16:41:02 -06:00
|
|
|
|
|
2024-08-08 02:31:54 -06:00
|
|
|
|
if !(argc < 0) { usage(program_name); }
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
This applies to C switch statements and cases and Rust match statements, as
|
|
|
|
|
well:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
switch (value) { /* aligning stuff to make it easier to read is fine */
|
|
|
|
|
case possibility: variable = foo; break;
|
|
|
|
|
default: variable = NULL; break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
1. Switch cases in C and match arms in Rust should start another level of
|
|
|
|
|
indentation:
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
switch (value) {
|
|
|
|
|
case possibility:
|
|
|
|
|
statement;
|
|
|
|
|
break;
|
|
|
|
|
case default:
|
|
|
|
|
statement;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
Ok(n) => variable = n,
|
|
|
|
|
Err(e) => error = e,
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
2. Braces in control flow where their inclusion is left optional in C:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
if (condition) { statement; }
|
2024-07-19 16:41:02 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
3. Empty lines between different kinds of statements:
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-07-15 13:29:12 -06:00
|
|
|
|
int t;
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-07-15 13:29:12 -06:00
|
|
|
|
assert(io->bufuse > 0);
|
|
|
|
|
assert(io->bufuse <= io->bs);
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-07-15 13:29:12 -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
|
|
|
|
|
2024-07-15 13:29:12 -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
|
|
|
|
|
2024-07-15 13:29:12 -06:00
|
|
|
|
return io;
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
4. Compiler options that yield the most useful warnings, such as -Wpedantic in
|
2024-09-05 12:44:14 -06:00
|
|
|
|
a lot of C compilers. Fix the warnings, too [0].
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
5. Fixed bounds for loops [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
6. One more level of indentation and one argument per line when a function
|
2024-09-05 12:44:14 -06:00
|
|
|
|
call or statement header is too long to fit on one line:
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-07-15 13:29:12 -06:00
|
|
|
|
let usage = format!(
|
|
|
|
|
"Usage: {} [-d delimiter] index command [args...]",
|
|
|
|
|
argv[0],
|
|
|
|
|
);
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
7. One more level of indentation than the keyword that initiated a multi-line
|
2024-09-05 12:44:14 -06:00
|
|
|
|
block.
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-08-08 02:31:54 -06:00
|
|
|
|
if (condition) {
|
|
|
|
|
statement;
|
|
|
|
|
statement;
|
|
|
|
|
}
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
8. The return value of all non-void functions, or explicitly ignore them (like
|
2024-09-05 12:44:14 -06:00
|
|
|
|
casting to void in C) [0]:
|
2024-07-13 23:47:50 -06:00
|
|
|
|
|
2024-08-08 02:31:54 -06:00
|
|
|
|
if ((a = malloc(sizeof char)) == NULL) { /* handle this error */
|
|
|
|
|
(void)fprintf(stderr, "oh noes!"); /* explicitly ignore this one */
|
|
|
|
|
return EX_OSERR; /* ...because the program is exiting anyway */
|
|
|
|
|
}
|
2024-07-15 13:29:12 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
9. The smallest possible scope for data [0].
|
2024-07-15 13:29:12 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
10. Comments noting all the symbols and macros used from a C header file, next
|
2024-09-05 12:44:14 -06:00
|
|
|
|
to its include macro:
|
2024-07-15 13:29:12 -06:00
|
|
|
|
|
|
|
|
|
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
|
2024-08-08 02:31:54 -06:00
|
|
|
|
(space-aligned) * optarg, optind, STDIN_FILENO, STDOUT_FILENO */
|
2024-07-14 02:15:07 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
11. Spaces in control flow statements, after the keyword and before the
|
2024-09-05 12:44:14 -06:00
|
|
|
|
opening brace:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
for (i = 2; i < argc; ++i) {
|
|
|
|
|
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
12. In Rust, a trailing comma on all arguments or fields that are on their own
|
2024-09-05 12:44:14 -06:00
|
|
|
|
lines:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
return Err(EvaluationError {
|
|
|
|
|
message: format!("{}: Invalid token", i),
|
|
|
|
|
code: EX_DATAERR,
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
13. In Rust, place extern statements after use statements that include standard
|
2024-09-05 12:44:14 -06:00
|
|
|
|
library crates. Group like statements:
|
2024-07-19 16:41:02 -06:00
|
|
|
|
|
|
|
|
|
use std::fs::Path;
|
|
|
|
|
|
|
|
|
|
extern crate strerror;
|
|
|
|
|
extern crate sysexits;
|
|
|
|
|
|
|
|
|
|
use strerror::StrError;
|
|
|
|
|
use sysexits::{ EX_OSERR, EX_USAGE };
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
14. If text is on the same line as a brace, spaces after an opening brace and
|
2024-09-05 12:44:14 -06:00
|
|
|
|
before a closing one:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
15. Alphabetic sorting, where applicable:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
2024-09-05 12:44:14 -06:00
|
|
|
|
use std::io::{ BufWriter, Read, Write, stderr, stdin, stdout }
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
16. In Rust, use the to_owned() method on string types (str, OsStr, CStr, etc.)
|
2024-09-05 12:44:14 -06:00
|
|
|
|
and the to_string() method on other types.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Avoid
|
|
|
|
|
=====
|
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
17. Function pointers [0].
|
2024-09-05 15:55:32 -06:00
|
|
|
|
|
2024-09-07 12:51:18 -06:00
|
|
|
|
18. Heap memory allocation [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
19. Using too much nested logic (within reason).
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
20. Too many levels of dereferences [0]:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
/* do not do this */
|
|
|
|
|
for (size_t i = 0; i < sizeof a / sizeof *a; ++i) {
|
|
|
|
|
if (a[i].id == MATCH) { a[i].val = 0; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* do this */
|
|
|
|
|
for (struct MadeUp *s = &a[0]; *s != NULL; s = &s[1]) {
|
|
|
|
|
if (s->id == MATCH) { s->val = 0; }
|
|
|
|
|
}
|
2024-07-28 00:34:42 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
21. Using C preprocessor macros; the fewer, the better [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
22. The exit(3p) and std::process::exit() functions; returning from the main
|
2024-09-05 12:44:14 -06:00
|
|
|
|
function skips a system call.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Do Not Use
|
|
|
|
|
==========
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
23. More than the length of one printed page for a function [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
24. Recursion, as it’s complex and can unexpectedly overflow the stack [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
25. Any functionality not in the POSIX C specification and language features not
|
2024-09-05 12:44:14 -06:00
|
|
|
|
in C99.
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
26. Do-while loops, as they’re unique to C and confusing for casual programmers.
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
27. Labels and goto statements; use sensible flow control [0].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
28. Pointer arithmetic, as it tends to be confusing and unnecessary; use
|
2024-09-05 12:44:14 -06:00
|
|
|
|
index-reference patterns like &p[1] instead of p + 1. &p[n] is the address at
|
|
|
|
|
p + sizeof p * n, not p + n, like pointer arithmetic suggests.
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
29. C struct bitfields in unions, to access certain bits of bigger data types,
|
2024-09-05 12:44:14 -06:00
|
|
|
|
as it’s poorly defined in the C standards; use bit arithmetic.
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
30. C trigraphs.
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
31. Inclusions in C header files, to prevent multiple file inclusions.
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
32. C preprocessor variables to prevent multiple inclusions of the same file,
|
2024-09-05 12:44:14 -06:00
|
|
|
|
such as:
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
|
|
|
|
#ifdef _FILE
|
|
|
|
|
#define _FILE
|
|
|
|
|
/* file body */
|
|
|
|
|
#endif /* ifdef _FILE */
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
Instead, take the time to ensure other files aren’t including any files twice.
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
33. The gets(3p) function from <stdio.h>, as it’s impossible to prevent buffer
|
2024-09-05 12:44:14 -06:00
|
|
|
|
overflows when it's used; use fgets(3p) from <stdio.h>.
|
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
34. The scanf(3p) function from <stdio.h> [1].
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
35. Any functionality not described in the latest POSIX make(1) specification.
|
2024-09-05 12:44:14 -06:00
|
|
|
|
|
2024-09-05 15:55:32 -06:00
|
|
|
|
36. Macros which panic on failure in Rust (such as the print!() and println!()
|
2024-09-05 12:44:14 -06:00
|
|
|
|
macros). Use a function and handle any errors. However, do use the eprintln!()
|
|
|
|
|
macro for error messages. Handling an error for writing an error message is
|
|
|
|
|
redundant.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Usage Text
|
|
|
|
|
==========
|
|
|
|
|
|
|
|
|
|
This section is adapted from the NetBSD style guide [2].
|
|
|
|
|
|
|
|
|
|
When programs are invoked incorrectly and in the synopsis of manual pages, uasge
|
|
|
|
|
text should be provided to the user. The following is the format used by this
|
|
|
|
|
project for this purpose:
|
|
|
|
|
|
|
|
|
|
All optional arguments are to be placed in square brackets (U+005B, U+005D).
|
|
|
|
|
Mutually exclusive arguments can be separated by a vertical line (U+007C).
|
|
|
|
|
Groups of arguments should be specified in alphabetical order in most cases. The
|
|
|
|
|
order of arguments and an example of these rules follows:
|
|
|
|
|
|
|
|
|
|
0. Options with no option arguments.
|
|
|
|
|
1. Options with option arguments. Arguments should be specified inside the same
|
|
|
|
|
square brackets as the options.
|
|
|
|
|
3. Non-option arguments.
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
2024-09-05 12:44:14 -06:00
|
|
|
|
"usage: f [-aDde] [-b b_arg] [-m m_arg] req1 req2 [opt1 [opt2]]\n"
|
|
|
|
|
"usage: f [-a | -b] [-c [-de] [-n number]]\n"
|
2024-08-08 02:31:54 -06:00
|
|
|
|
|
2024-07-19 17:31:51 -06:00
|
|
|
|
|
|
|
|
|
References
|
|
|
|
|
==========
|
|
|
|
|
|
|
|
|
|
[0] <https://web.eecs.umich.edu/~imarkov/10rules.pdf>
|
2024-08-08 02:31:54 -06:00
|
|
|
|
[1] <http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html>
|
2024-09-05 12:44:14 -06:00
|
|
|
|
[2] <http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style>
|
2024-07-19 17:31:51 -06:00
|
|
|
|
|
2024-07-13 23:47:50 -06:00
|
|
|
|
--
|
|
|
|
|
Copyright © 2024 Emma Tebibyte <emma@tebibyte.media>
|
2024-08-08 02:31:54 -06:00
|
|
|
|
Copyright © 2024 DTB <trinity@trinity.moe>
|
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/>.
|