Sujet : Re: Fixing a sample from K&R book using cake static analyser
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 24. Jun 2024, 02:31:36
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240623182210.663@kylheku.com>
References : 1 2 3 4 5
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-06-23, Kaz Kylheku <
643-408-1753@kylheku.com> wrote:
On 2024-06-23, Anton Shepelev <anton.txt@gmail.moc> wrote:
Kaz Kylheku:
>
What scatter-brained drivel. Watch and learn:
>
struct nlist *install(char *name, char *defn)
{
struct nlist *existing = lookup(name);
>
if (existing) {
return existing;
} else {
>
When the if-branch ends with a return, the else-branch is
redundant, and its body shall be written bare, removing one
(useless) level of nesting and indentation.
>
I did that because there are declarations in the else case.
>
I avoid mising declarations and statements, because I consider
that a language misfeature.
>
In a block structured language, declarations should come first,
then statements.
Also:
You generally want parallel elements at the same level of indentation.
The following structure can be grating on the eyes:
if (key == node->key)
return node;
else if (key < node->key)
return tree_search(node->right, key);
return tree_search(node->left, key);
This is just an example; I realize we are not handling the case
of the key being found.
This is better:
if (key == node->key)
return node;
else if (key < node->key)
return tree_search(node->right, key);
else
return tree_search(node->left, key);
the parallel elements align at the same indentation level.
The structure being recommended by Anton is most at home in imperative
situations like this:
stmt1;
stmt2;
if (condition1)
return early;
stmt3;
stmt4;
if (condition2)
return early;
stmt5;
...
When we have multiple cases that are all parallel and of equivalent
importance (they could be in in any order), not so much.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca