Article View: comp.lang.c
Article #586Re: Taming The Switch-Statement
From: Waldek Hebisch
Date: Tue, 20 Oct 2026 00:16
Date: Tue, 20 Oct 2026 00:16
104 lines
6336 bytes
6336 bytes
Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> antispam@fricas.org (Waldek Hebisch) writes:
>
>> Lawrence D?Oliveiro <ldo@nz.invalid> wrote:
>>
>>> On Tue, 13 Oct 2026 07:23:40 -0000 (UTC), Waldek Hebisch wrote:
>>>
>>>> So what those counts indicate: majority of loops is simple, that is
>>>> have no early exit. Vast majority of early exits is done via
>>>> 'return'.
>>>
>>> My experience in my own C code is quite the opposite. Lots of loops
>>> have exits in the middle, leading me to use forms like ?for (;;)?. I
>>> also try to avoid putting ?return? anywhere other than the end of a
>>> function, to make it easier to see the flow of control.
>>
>> Well, usually there is a lot of loops. Those with exit in the
>> middle are still significant.
>>
>> Concernig 'return', having it in place where all work inside function
>> is done help me to see flow control and avoids complications. In
>> case of search functions it means return from the middle of the loop.
>> IMO possible alternatives like flag variables or break and retrun
>> after loop are worse.
>
> I rarely use 'break' to exit a loop. Part of this is that I try to
> write standard loops so that they complete as part of the loop test
> (i.e., in while() or for() control expressions). That isn't the
> whole story though so let me talk about 'return'.
I counted various constructs in a small suite of 2 programs (1306
lines in 3 files). There are 4 breaks outside switches and 32
loops in total. My gut feeling is that this proportion is
reasonable close to typical in my code. For comparison, there
are 67 functions and 70 'if'-s.
> Most of the functions I write have exactly one return, at the end of
> the function. I estimate this pattern accounts for around 85% of
> all functions, let's say plus or minus 5%.
In code above about half of functions is 'void' and there are 34
explicit returns (of which at least 2 are early returns).
> Sometimes functions have early returns for errors or exceptional
> conditions.
Code above contains 16 occurences of 'exit(1)'. One of those is
in a macro that is used 10 times, so actually there are 25 places
which call 'exit(1)'. That is related to the programs being
simple utilities, there is no attempt to error recovery, on error
programs simply print error message and exit.
> Early returns are easily seen to lie outside the main
> body of function processing so they don't interfere with
> understanding what the rest of the function is doing or how it
> works.
>
> A fairly common sub-pattern are functions most of whose body is a
> switch() statement, with all of the switch() cases being a simple
> return.
Code above has 2 switches, both are in parsing functions, where
cases either move to next state or signal error. Parsing functions
also open file and close it when finished and return data structure
with parsed info.
> It does sometimes happen that a function will have a loop with a
> return in the middle, and also a return at the end for what to do if
> the loop terminates. To me having an in-loop return looks better
> than using an assignment and a break.
>
> A fairly rare but important pattern is what I might call vectoring
> or decomposition. Here the function body serves to distinguish and
> separate out different regimes of subprocessing. To continue
> processing in a particular regime, a return is used not to directly
> yield a value but rather to call a further function to handle that
> sub-domain. The benefit here is both to keep function bodies small
> and to allow re-use of commonly called sub-functions.
>
> Two aspects are important for having these different styles work
> together. One is that it's easy to distinguish the different kinds
> of patterns, and by far the most simple pattern is the most common.
> The other is to be sure to keep function bodies small. Keeping
> function bodies small goes a long way in making them easy to
> understand, which obviously is a key property in writing good code.
> Also keeping function bodies small effectively prevents mixing
> different styles in a single function, serving to reinforce point
> one.
I think that in general my C code has similar characteristics. But
let me note that while I have a lot of small functions, I also have
some which are bigger. I suspect that 10% or more of my code is
in functions that are bigger then 30 lines. Such functions
frequently have several variables and there is setup stage to
initialize things, actual processing and final part (cleanups,
possibly extraction of result). Stages are clearly sepatated
by their position inside function, OTOH are strongly coupled due
to use of variables. My feeling here is that in case of strongly
coupled code with no reusable parts keeping it inside single
function is more managable than trying to split it into smaller
functions.
--
Waldek Hebisch
Technical Details
Message-ID:
<11b6bsj$ctsd$1@paganini.bofh.team>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mailReferences:
<119qf6l$2rmg3$1@dont-email.me> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <11akm9q$29tpl$1@paganini.bofh.team> <11am99u$ul56$2@dont-email.me> <11atfif$38b7p$1@paganini.bofh.team> <8633u3pwwd.fsf@linuxsc.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Tue, 20 Oct 2026 00:16:21 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11b6bsj$ctsd$1@paganini.bofh.team>
References: <119qf6l$2rmg3$1@dont-email.me> <11afvqt$2kloc$1@dont-email.me> <11ag37o$2ln0a$1@dont-email.me> <11agbsh$2pbk7$1@dont-email.me> <11aggaq$2qqh9$1@dont-email.me> <11agils$2s3tg$1@dont-email.me> <11agv1t$30q22$2@kst.eternal-september.org> <20261012220256.00003d07@yahoo.com> <11ajb95$3t8jj$1@dont-email.me> <20261012223144.00000f95@yahoo.com> <11akm9q$29tpl$1@paganini.bofh.team> <11am99u$ul56$2@dont-email.me> <11atfif$38b7p$1@paganini.bofh.team> <8633u3pwwd.fsf@linuxsc.com>
Injection-Date: Tue, 20 Oct 2026 00:16:21 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="423821"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2