paritybit.ca

Files for paritybit.ca
Log | Files | Refs | README | LICENSE

commit 675e3064411c4df7585b8092a895fa2947b3fc7d
parent 6515ca2dc35fbbde573315ec3e4f10fd533ea9c7
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Wed, 29 Mar 2023 18:53:30 -0400

*

Diffstat:
Mcontent/garden/programming-style.md | 201++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mcontent/index.md | 7+++----
2 files changed, 121 insertions(+), 87 deletions(-)

diff --git a/content/garden/programming-style.md b/content/garden/programming-style.md @@ -270,72 +270,6 @@ a procedure?](https://stackoverflow.com/questions/721090/what-is-the-difference- Braces and parentheses should be used liberally to make code easy to follow. -In general, single-line control blocks like so: - -``` -if (condition) - do_stuff(); -``` - -should have surrounding braces. Although it can seem excessive, especially -paired with my other preferences, it eliminates a class of errors that arises -when one adds code to a control block or comments out a line but forgets to -also add the braces: - -``` -if (condition) - do_stuff(); - do_more_stuff(); <- this is not in the if block! - -if (condition) - /* do_stuff(); */ - -do_other_stuff(); <- now this is in the if block! -``` - -Plus, I find it more ergonomic to be able to quickly comment out or add -statements without also then having to add or remove braces to keep the style -consistent or the code correct. - -(Although whitespace-indented languages such as Python do not have to deal with -this issue, that property brings along other issues with code readability due -to blocks not being as clearly delineated. Like I said, there is no One True -Style.) - -Furthermore, in if/else statements, this style: - -``` -if (condition) -{ - do(); - things(); -} -else -{ - other(); - stuff(); -} -``` - -is preferable to: - -``` -if (condition) { - do(); - things(); -} else { - other(); - stuff(); -} -``` - -because it is much easier to select the else statement (for deletion, copying, -etc.) if it is not on the same line as the closing brace of the if statement. -This is not only helpful in vim's visual mode, but also when selecting code with -a mouse as one can be less precise when selecting the block of code. It is also -easier to read this way, regardless of how you feel about what I am about to -say: - Although many have unfortunately settled on the following brace style for functions, control statements, and the like: @@ -359,7 +293,19 @@ if (condition) While this might appear excessive for such a trivial statement, the superiority of this style for readability quickly becomes evident when blocks get longer or -more complex. For example: +more complex. + +The additional vertical whitespace afforded by the opening brace being on a new +line helps to visually separate distinct blocks of code, and it is much easier +to find matching braces and keep track of block boundaries since the opening +and closing braces visually line up. + +It also means that, should the conditions in an `if` statement or the arguments +to a function grow too long to fit comfortably on one line, there is still +clear separation between the statement and its body without needing to +awkwardly double-indent the set of conditions or arguments. + +For example: ``` if (is_logged_in(client) @@ -409,8 +355,9 @@ for (s = opts; (p = strsep(&s, ",")) != NULL;) { } } ``` -<small>The above code is from <code>fsck.c</code> in the OpenBSD codebase and -is ISC-licensed.</small> +<small>The above code is from <a +href="https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sbin/fsck/fsck.c">fsck.c</a> +in the OpenBSD codebase and is BSD-3-Clause-licensed.</small> is not as nice as: @@ -446,22 +393,110 @@ for (s = opts; (p = strsep(&s, ",")) != NULL;) } ``` -because the additional vertical whitespace afforded by the opening brace being -on a new line helps to visually separate distinct blocks of code, and it is -much easier to find matching braces since the opening and closing braces -visually line up. +This also means that subsequent control statements should go on their own line like so: + +``` +if (condition) +{ + do(); + things(); +} +else +{ + other(); + stuff(); +} +``` + +instead of: + +``` +if (condition) +{ + do(); + things(); +} else +{ + other(); + stuff(); +} +``` + +because it is much easier to select the else statement (for deletion, copying, +etc.) if it is not on the same line as the closing brace of the if statement. +This is not only helpful in vim's visual mode, but also when selecting code +with a mouse as one can be less precise when selecting the block of code. It is +also easier to read this way. + +Also, never put single-line bodies on the same line as control statements. It's +much harder to read. Don't do this: + +``` +for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) printf("(%d,%d)\n", i, j); +``` + +Also, in general, single-line control blocks like so: + +``` +if (condition) + do_stuff(); +``` + +should have surrounding braces. Although it can seem excessive, especially +paired with my other preferences, it eliminates a class of errors that arises +when one adds code to a control block or comments out a line but forgets to +also add the braces: + +``` +if (condition) + do_stuff(); + do_more_stuff(); <- this is not in the if block! + +if (condition) + /* do_stuff(); */ + +do_other_stuff(); <- now this is in the if block! +``` + +(Although whitespace-indented languages such as Python do not have to deal with +this issue, that property brings along other issues with code readability due +to blocks not being as clearly delineated. Like I said, there is no One True +Style.) + +Plus, I find it more ergonomic to be able to quickly comment out or add +statements without also then having to add or remove braces to keep the style +consistent or the code correct. I also find it easier to read when control +statements are next to each other such as in: + +``` +if (condition) +{ + continue; +} +if (other_condition) +{ + printf("Other condition reached!\n"); + do_something(); +} +``` + +compared to: -With this style, it is much easier to keep track of block boundaries when the -braces are distinctly on their own lines. It also means that, should the -conditions in an `if` statement or the arguments to a function grow too long to -fit comfortably on one line, there is still clear separation between the -statement and its body without needing to awkwardly double-indent the set of -conditions or arguments. +``` +if (condition) + continue; +if (other_condition) +{ + printf("Other condition reached!\n"); + do_something(); +} +``` -While some denounce this style for being not as compact, note that, just like -the line length argument, this has been largely irrelevant for a long time. -Most displays from the last couple decades can show a lot more than 25 or even -40 rows of text at a time at reasonable resolutions and font sizes. +While some denounce my preferred style for being not as compact as some +alternatives, note that, just like the line length argument, this has been +largely irrelevant for a long time. Most displays from the last couple decades +can show a lot more than 25 or even 40 rows of text at a time at reasonable +resolutions and font sizes. Speaking of line length... diff --git a/content/index.md b/content/index.md @@ -5,10 +5,9 @@ Summary: The personal website and blog of Jake Bauer. My name is Jake though I usually go by "jbauer" online. -I'm a hobbyist of many hobbies. I love simple and sustainable technologies, tea, -writing, gardening, and more. I'm a big fan of low-tech or analog solutions to -life's problems. I also have many opinions about things; some strong, some not, -and most are fluid. +I'm a person with many interests and hobbies. I love simple and sustainable +technologies, tea, writing, gardening, and more. I'm a big fan of low-tech or +analog solutions to life's problems. Have a look at [my values and beliefs](values-and-beliefs.html) if you want to know more, [my projects](projects/) to see what I'm working on, or [my digital