Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: Fixing a sample from K&R book using cake static analyser Date: Mon, 24 Jun 2024 00:36:46 +0100 Organization: A noiseless patient Spider Lines: 42 Message-ID: <87tthjnsdt.fsf@bsb.me.uk> References: <20240623022343.ec355c69a3d9eb03ad4a50f2@gmail.moc> <878qywq7ou.fsf@bsb.me.uk> <20240624013337.36fdb40f0766c6e1c8ce67c7@gmail.moc> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 24 Jun 2024 01:36:46 +0200 (CEST) Injection-Info: dont-email.me; posting-host="2fe2b5b522b691828527b639c59887b1"; logging-data="606629"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BDnMeqPV6TBDlPIc7pVL9/+ewCinZOBY=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:U2BHY1wbFFqkgFPgQ4j3DLlKDqo= sha1:Zeq+uF0p/xmlfhcq4P6exLH3Poo= X-BSB-Auth: 1.2660e711ded85da1e9c2.20240624003646BST.87tthjnsdt.fsf@bsb.me.uk Bytes: 2961 Anton Shepelev writes: > Ben Bacarisse to Anton Shelelev: > >> > Why are you so afraid of `goto' that you must emulate it >> > with `while' and `break'? >> >> Why are you so fond of them that you add three extra state >> variables that have to be (mentally and/or mathematically >> tracked) just understand this supposedly simply function? > > Because my attempts to fix the function without the extra > variables proved even worse. OK, but two labels and three extra state variables makes it much more complex to think about. I don't think the simple "if this then that" approach is at all problematic in this case. I can't see why adding even one goto helps when the structured approach is so simple. (I've tried to show this with code in another reply.) >> This, to me, is a textbook case of why goto is harmful. I >> have spend considerable time jumping up and down checking >> the state of all the variables and I am still not sure I >> follow what this supposedly simple function is doing. > > I have tried to keep the structure simple: all the goto's > jump down, both the labels define isolated blocks, guared by > returns, to prevent fall-though. Trying to make gotos less bad than they can be is not usually an overall positive. Whenever I see a "good" use of goto, I try to see if there's a better way with no use of goto. So far (with the exception of an example of tightly bound co-routines being simulated in a single C function) I have not yet seen one that can't. There are sometimes run-time considerations (giant state machines, for example) but even there the structured code is probably clearer. The use of gotos in those cases is not to improve the logic of the code but to placate the code generator. -- Ben.