Deutsch English Français Italiano |
<20240624142816.a2323885c6e16cd244b895d3@g{oogle}mail.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Anton Shepelev <anton.txt@g{oogle}mail.com> Newsgroups: comp.lang.c Subject: Re: Fixing a sample from K&R book using cake static analyser Date: Mon, 24 Jun 2024 14:28:16 +0300 Organization: A noiseless patient Spider Lines: 72 Message-ID: <20240624142816.a2323885c6e16cd244b895d3@g{oogle}mail.com> References: <v53sl1$35qt7$1@dont-email.me> <v558hv$3dskb$1@dont-email.me> <20240623034624.135@kylheku.com> <20240624014040.c7a4b34e68ce734ff837868d@gmail.moc> <20240623160155.649@kylheku.com> <20240623182210.663@kylheku.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Mon, 24 Jun 2024 13:28:17 +0200 (CEST) Injection-Info: dont-email.me; posting-host="2fe77130d5d186ea939aebf0305b8a56"; logging-data="953027"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18WleDA85IVAIXkYBtwQuA6+Z0s6INl2sQ=" Cancel-Lock: sha1:7m5ykWUM63LicxjzLQrNSSbZMxM= X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Bytes: 3225 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