[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <aVUUXAKjiNroU5tR@black.igk.intel.com>
Date: Wed, 31 Dec 2025 13:17:32 +0100
From: Andy Shevchenko <andriy.shevchenko@...el.com>
To: Alexey Dobriyan <adobriyan@...il.com>
Cc: James Bottomley <James.Bottomley@...senpartnership.com>,
ksummit@...ts.linux.dev, Dan Williams <dan.j.williams@...el.com>,
linux-kernel <linux-kernel@...r.kernel.org>,
Dan Carpenter <dan.carpenter@...aro.org>
Subject: Re: Clarifying confusion of our variable placement rules caused by
cleanup.h
On Tue, Nov 25, 2025 at 05:25:19PM +0300, Alexey Dobriyan wrote:
> On Tue, Nov 18, 2025 at 11:39:26AM -0500, James Bottomley wrote:
>
> > So which should we do?
>
> The best way to understand that C89 style of declaring in the beginning
> of the function is pointless rule is to write some code in a language
> which doesn't enforce it. You should see that nothing bad happens.
>
> It increases bug rate due to increased variable scope allowing typos.
>
> It bloats LOC -- in many cases declaration and initializer can fit
> into a single line.
It's a weak argument, see below.
> It prevents adding "const" qualifier if necessary.
>
> Pressing PageUp and PageDown when adding new variable is pointless
> busywork and distracts, breaks the tempo(flow?) so to speak.
I think with something like VSCode, it's much easier to handle, but it's just a
side note.
> C89 style provokes substyles(!) which makes adding new variables even
> more obnoxious: some subsystems have(had?) a rule saying that declarations
> (with initializers) must be sorted by length, so not only programmer has
> to PageUp to the beginning of the block, but then aim carefully and
> insert new declaration.
>
> None of this is necessary (or possible) if the rule says "declare as low
> as possible".
This is a bad rule, if hard defined, as it provokes to have a code like
int ret = foo(...);
if (ret)
do_smth(...);
The evolution of such code is prone to subtle mistakes, as more code will be
added during the development of the mentioned function. I saw the real error
case when somebody does something wrong in between the lines. That's why I
prefer the assignment to be split from the definition.
int ret;
ret = foo(...);
if (ret)
do_smth(...);
is more maintainable and robust.
However, if we talk about RAII variables, the assignment and definition makes
a lot of sense to go together.
> There was variation of this type of nonsense with headers (not only it has
> to be sorted alphabetically but by length too!)
By length it indeed sounds weird, but alphabetical is the natural language
order everybody learnt from the daycare / school years, so it's properly
programmed in our deep brain. Having that allows to find easily if anything one
is interested in is already being included. Also it allows to avoid dup inclusions
(was there, fixed that for real). So, it's not bad.
> There is no practical difference between code and declarations:
> declarations can have initializers which can be arbitrary complex,
> just like "real" code. So the only difference is superficial.
>
> C89 declaration style is pointless and dumb, no wonder other programming
> languages dumped it (or never had), it should be simply discarded.
>
> It will also make Linux slightly less white crow to newcomers
> (C++ doesn't have this rule after all).
--
With Best Regards,
Andy Shevchenko
Powered by blists - more mailing lists