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 : 25. Jun 2024, 02:21:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240624180824.534@kylheku.com>
References : 1 2 3 4 5 6 7 8 9
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-06-24, Lawrence D'Oliveiro <
ldo@nz.invalid> wrote:
On Mon, 24 Jun 2024 11:53:51 +0100, Ben Bacarisse wrote:
>
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
Too many different paths in the control flow, though. I think it’s a
good idea to minimize that.
Your non-solution has more.
>
My solution only has one major flow of control: in the top and out the
bottom.
This is false. Every branch in the code creates a separate control
flow path. For instance, this has 8 possible control flow paths:
if (this)
that;
if (other)
foo;
else
bar;
if (condition)
xyzzy;
One entry at the top and one exit at the bottom does not imply
one control path.
Everything else is error checks, and it is quite obvious where
they all go--through the same cleanup path.
Your solution intertwines both cases together and jumbles the error
handling between other logic, requiring the reader to untangle all
that.
Ben's and mine solution (very similar) separates the two major
cases: name exists vs. doesn't. The former is so short,
it has no reason to share code with the other.
The happy case is consolidated together at the top of each
case: the code (1) tries to acquire all the needed resources before
doing anything, without doing error checks. Then (2) there is a single
if which checks that we have all the resources. If so, then there
is a block which configures the registration, no longer requiring
error checks. If the resource allocation failed, all the resources
are freed in one block.
The function is divided into blocks that have a single, clear
responsibility.
(If a coding convention were imposed that there must be a single exit
point from a function, the code could be easily adjusted to that without
losing these attributes.)
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca