Sujet : Re: Fixing a sample from K&R book using cake static analyser
De : anton.txt (at) *nospam* g{oogle}mail.com (Anton Shepelev)
Groupes : comp.lang.cDate : 24. Jun 2024, 12:28:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240624142816.a2323885c6e16cd244b895d3@g{oogle}mail.com>
References : 1 2 3 4 5 6
User-Agent : Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Kaz Kylheku:
Also:
>
You generally want parallel elements at the same level of
indentation.
Who, me? As a matter of fact, /I/ do.
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);
Yes.
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);
Just a tiny-little-bit better, becase I the last
unconditinoal return makes sense as the default exit route.
I therefore consder the following version more expressive:
if (key == node->key) return node;
if (key < node->key) return tree_search(node->right, key);
if (1 ) return tree_search(node->left , key);
To emphasize parallelism, and line length permitting, I
format my if-else chains thus:
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 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.
This structure is perfectly suitable for order-dependent
statements, too. And I have to use `goto' if some
deinitialisation is required before the function terminates.
-- () ascii ribbon campaign -- against html e-mail/\ www.asciiribbon.org -- against proprietary attachments