Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: Fixing a sample from K&R book using cake static analyser Date: Mon, 24 Jun 2024 01:31:36 -0000 (UTC) Organization: A noiseless patient Spider Lines: 73 Message-ID: <20240623182210.663@kylheku.com> References: <20240623034624.135@kylheku.com> <20240624014040.c7a4b34e68ce734ff837868d@gmail.moc> <20240623160155.649@kylheku.com> Injection-Date: Mon, 24 Jun 2024 03:31:37 +0200 (CEST) Injection-Info: dont-email.me; posting-host="0a91c74ce60a0e44ab75e91b4c3a6a2f"; logging-data="642246"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/SR6RWZsuJbPX938CtzY+MWQ6WL1Ub6+Q=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:Zj62sIhrwVhnZhHZiAfcJzOw/l0= Bytes: 3037 On 2024-06-23, Kaz Kylheku <643-408-1753@kylheku.com> wrote: > On 2024-06-23, Anton Shepelev 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/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca