Sujet : Re: Which code style do you prefer the most?
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 04. Mar 2025, 17:01:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vq785i$1u7v7$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 04/03/2025 15:18, Richard Heathfield wrote:
On 04/03/2025 14:56, Anton Shepelev wrote:
Ar Rakin:
>
I've been writing C code for a long time, in different styles, but
always wanted to know which code style most people prefer. This is all
about that question. Which one of the following do you prefer the most?
>
1. GNU Style
2. Linux Style
>
I don't like them much. Below is a piece of my code
I don't like most of the choices you've made :-) *but* I can see perfectly good reasons for making those choices (if that's any consolation).
Just about the only choice of yours that I /do/ like is your brace placement:
{
{
{
}
}
}
which like because it makes the code's structure stand out so clearly. I know a lot of people criticise this style for being wasteful of vertical space, but I've never seen that as a problem. My screen can hold more than enough code to occupy me, and my annual vertical space bill is very reasonable.
I assume that style would be used like this:
if (cond)
{
stmt1;
}
else
{
stmt2;
}
So 50% of lines are solely for braces, with braces not having their own indent level. However if I now look at AS's post, that isn't it: opening braces don't have their own dedicated line, they are shared with the first line of the block:
if (cond)
{ stmt1;
}
else
{ stmt2;
}
This cuts down the line count, but now the first line is a special case: it's got that extra clutter at the start, makes it harder to swap with other lines, to temporarily comment out, to delete, or to add extra, perhaps debugging, lines at the start of the block.
The style I use for generated code is like this:
if (cond) {
stmt1;
}
else {
stmt2;
}
The same amount of vertical space, but none of those problems. So this is superior IMV.
(That is, when you have to use braces; generally I don't like that style of syntax *because* there are so many placement styles: there are multiple ways of writing the three tokens of '} else {'; not so many if it's just the single token 'else'.)