I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
[...]
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
You should get in the habit of being consistent with braces, and being generous with them. Your future self with thank you.
if (failed) {
WARN("failed because...");
} else {
ok++;
}
... is small
enough to fit entirely on a single line, and the statement could not
possibly be misinterpreted, use a macro, or have any other non-obvious behaviour.
Thus :
if (failed) return -1; // Early exit
or
if (!failed) ok++;
Otherwise, put in all the braces.
On 09/21/24 1:47 AM, David Brown wrote:
You should get in the habit of being consistent with braces, and being
generous with them. Your future self with thank you.
if (failed) {
WARN("failed because...");
} else {
ok++;
}
Nope. There's absolutely no reason to overuse braces.
And don't use "Egyptian" braces. The latter is actually an ill-begotten attempt to remedy the damage from the former. Just stop overusing
braces, and there'll be no need for the remedy. Easy.
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides separation
between condition and the branch,
Nope. There's absolutely no reason to overuse braces.I agree with that, but you realize you are kinda fighting
And don't use "Egyptian" braces. The latter is actually an ill-begotten attempt to remedy the damage from the former. Just stop overusing
braces, and there'll be no need for the remedy. Easy.
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides separation
between condition and the branch, which makes your code much more
readable. It is also a very good location for a comment, if you decide
to include one. Your future self with thank you.
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
One reason to "overuse" braces is that you can easily add another
statement. If you write:
if (failed)
WARN("failed because...");
else
ok++;
and later decide you need two statements in the else clause, you then
need to add braces. If they're already there, you don't.
What you call "Egyptian" braces is the style used by the creators of the language
and in a *lot* of open source software. Even if you don't like
the style, you'll need to deal with it.
I have my own fairly strong preferences about brace placement, but the
most important rule is to follow the conventions of the code I'm working
on.
On 09/21/24 1:47 AM, David Brown wrote:
You should get in the habit of being consistent with braces, and being
generous with them. Your future self with thank you.
if (failed) {
WARN("failed because...");
} else {
ok++;
}
Nope. There's absolutely no reason to overuse braces.
And don't use "Egyptian" braces. The latter is actually an ill-begotten attempt to remedy the damage from the former. Just stop overusing
braces, and there'll be no need for the remedy. Easy.
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides separation
between condition and the branch, which makes your code much more
readable. It is also a very good location for a comment, if you decide
to include one. Your future self with thank you.
... is small
enough to fit entirely on a single line, and the statement could not
possibly be misinterpreted, use a macro, or have any other non-obvious
behaviour.
Thus :
if (failed) return -1; // Early exit
or
if (!failed) ok++;
Otherwise, put in all the braces.
Nope. Do not ever write single-line `if` statements. This is a major impediment to step-by-step debugging.
On 09/21/24 2:54 PM, Keith Thompson wrote:
One reason to "overuse" braces is that you can easily add another
statement. If you write:
if (failed)
WARN("failed because...");
else
ok++;
and later decide you need two statements in the else clause, you then
need to add braces. If they're already there, you don't.
Adding braces in this situation is _incomparably_ easier than splitting
an annoying single-line `if` statement into multiple lines, discovered
during an interactive debugging session. Which is something you yourself described as "easy enough" below.
What you call "Egyptian" braces is the style used by the creators of the
language
Firstly, this is style. Being a "creator of the language" does not make
one an authority on code formatting style.
Secondly, most people pick up "the style used by the creators of the language" from the code samples used in the 2nd edition of K&R book.
And, as we know, "the creators of the language" went a little lazy here.
The samples were considered of "low importance" and fell victim to the tightening publishing schedules in front of the looming "threat" of the upcoming ANSI standard. The code samples were never properly updated to
match the style and spirit of modern C.
This is BTW, the reason we have to deal with that pesky and atrocious
manner to cast the result of `malloc` - another practice erroneously
believed to be "the style used by the creators of the language".
and in a *lot* of open source software. Even if you don't like
the style, you'll need to deal with it.
I have my own fairly strong preferences about brace placement, but the
most important rule is to follow the conventions of the code I'm working
on.
Certainly. It is not a good practice to force your own style onto
someone else's code or an already existing code. Still, in most
reasonably organized professional development environments personal preferences are normally welcomed with certain granularity. It is
usually organized on "per translation unit" basis.
I am not suggesting overuse of braces. I am suggesting /good/ use of them.
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by omitting
useful braces.
Consistency and clarity is important. So is maintainability. Suppose,
for example, you want to add a line "attempts++;" alongside the "ok++;"
line. When the braces are there, the change is exactly that - add the
new line you want. Without the original braces, you are now making
changes to the structural syntax of the code as well when you add in
original braces - or you are making a mistake in the code.
My super advanced text editor from the future isn't letting me do that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content into it.
OK, I lied about the super advanced from the future. It's just Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just concentrate on >whether it looks prettier without the braces or with.
On 2024-09-22, David Brown <[email protected]> wrote:
I am not suggesting overuse of braces. I am suggesting /good/ use of them. >>
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by omitting
useful braces.
Consistency and clarity is important. So is maintainability. Suppose,
for example, you want to add a line "attempts++;" alongside the "ok++;"
line. When the braces are there, the change is exactly that - add the
new line you want. Without the original braces, you are now making
changes to the structural syntax of the code as well when you add in
original braces - or you are making a mistake in the code.
My super advanced text editor from the future isn't letting me do that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content into it.
OK, I lied about the super advanced from the future. It's just Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just concentrate on whether it looks prettier without the braces or with.
Also GCC has been able to diagnose misleading indentation for some
years now.
On 2024-09-22, David Brown <[email protected]> wrote:
I am not suggesting overuse of braces. I am suggesting /good/ use of them. >>
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by omitting
useful braces.
Consistency and clarity is important. So is maintainability. Suppose,
for example, you want to add a line "attempts++;" alongside the "ok++;"
line. When the braces are there, the change is exactly that - add the
new line you want. Without the original braces, you are now making
changes to the structural syntax of the code as well when you add in
original braces - or you are making a mistake in the code.
My super advanced text editor from the future isn't letting me do that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content into it.
OK, I lied about the super advanced from the future. It's just Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just concentrate on whether it looks prettier without the braces or with.
On 22/09/2024 16:11, Kaz Kylheku wrote:
On 2024-09-22, David Brown <[email protected]> wrote:
I am not suggesting overuse of braces. I am suggesting /good/ use
of them.
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by
omitting useful braces.
Consistency and clarity is important. So is maintainability.
Suppose, for example, you want to add a line "attempts++;"
alongside the "ok++;" line. When the braces are there, the change
is exactly that - add the new line you want. Without the original
braces, you are now making changes to the structural syntax of the
code as well when you add in original braces - or you are making a
mistake in the code.
My super advanced text editor from the future isn't letting me do
that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content
into it.
OK, I lied about the super advanced from the future. It's just Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just
concentrate on whether it looks prettier without the braces or with.
So, everyone has to write code exactly to the C standard.
But when it comes to more practical matters, it's OK to depend on the arbitrary characteristics and abilities of whichever one of 1001
different text editors we happen to be using.
Or we have to use a specific compiler with a specific set of options.
Also GCC has been able to diagnose misleading indentation for some
years now.
How many years was that out of the last 52? How exactly do you turn
it on? Since -Wall -Wpedantic -Wextra doesn't report it.
It is a failure in the design of the language. You can't really
depend on ad hoc features of the tools you use to create and compile
source code.
At least guidelines can be used such as always using braces, then
errors can occur less often.
(I've been bitten by this endless times when trying to add debugging statements to existing code. If that code consistently used braces,
then it wouldn't happen.)
On Sun, 22 Sep 2024 16:56:47 +0100
Bart <[email protected]> wrote:
On 22/09/2024 16:11, Kaz Kylheku wrote:
On 2024-09-22, David Brown <[email protected]> wrote:
I am not suggesting overuse of braces. I am suggesting /good/ use
of them.
<https://www.synopsys.com/blogs/software-security/understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by
omitting useful braces.
Consistency and clarity is important. So is maintainability.
Suppose, for example, you want to add a line "attempts++;"
alongside the "ok++;" line. When the braces are there, the change
is exactly that - add the new line you want. Without the original
braces, you are now making changes to the structural syntax of the
code as well when you add in original braces - or you are making a
mistake in the code.
My super advanced text editor from the future isn't letting me do
that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content
into it.
OK, I lied about the super advanced from the future. It's just Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just
concentrate on whether it looks prettier without the braces or with.
So, everyone has to write code exactly to the C standard.
But when it comes to more practical matters, it's OK to depend on the
arbitrary characteristics and abilities of whichever one of 1001
different text editors we happen to be using.
Or we have to use a specific compiler with a specific set of options.
> Also GCC has been able to diagnose misleading indentation for some
> years now.
How many years was that out of the last 52? How exactly do you turn
it on? Since -Wall -Wpedantic -Wextra doesn't report it.
My gcc warns just fine.
void bar(int);
int foo(int x) {
if (x)
bar(x);
x -= 1;
return x;
}
gcc -c -Wall foo.c
foo.c: In function 'foo':
foo.c:3:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation] 3 | if (x)
| ^~
foo.c:5:5: note: ...this statement, but the latter is misleadingly
indented as if it were guarded by the 'if'
5 | x -= 1;
| ^
It is a failure in the design of the language. You can't really
depend on ad hoc features of the tools you use to create and compile
source code.
At least guidelines can be used such as always using braces, then
errors can occur less often.
(I've been bitten by this endless times when trying to add debugging
statements to existing code. If that code consistently used braces,
then it wouldn't happen.)
I wonder why I was not bitten by that more than, may be, 5 times in
30+ years. Probably, I am doing something wrong.
Bart <[email protected]> writes:
On 22/09/2024 16:11, Kaz Kylheku wrote:[...]
Also GCC has been able to diagnose misleading indentation for some
years now.
How many years was that out of the last 52? How exactly do you turn it
on? Since -Wall -Wpedantic -Wextra doesn't report it.
The -Wmisleading-indentation option was added to gcc on 2015-05-12,
and incorporated into -Wall 2015-12-10. gcc 6.1.0 has the option
and includes it in -Wall; gcc 5.3.0 does not. (Are you using a gcc
release that old?) It uses the -ftabstop= option (defaulting to 8)
to determine whether indentation lines up or not.
Inconsistent tabstops and mixing of spaces and tabs can certainly
cause problems.
On 22/09/2024 22:39, Keith Thompson wrote:
Bart <[email protected]> writes:
On 22/09/2024 16:11, Kaz Kylheku wrote:[...]
Also GCC has been able to diagnose misleading indentation for some
years now.
How many years was that out of the last 52? How exactly do you turn it
on? Since -Wall -Wpedantic -Wextra doesn't report it.
The -Wmisleading-indentation option was added to gcc on 2015-05-12,
and incorporated into -Wall 2015-12-10. gcc 6.1.0 has the option
and includes it in -Wall; gcc 5.3.0 does not. (Are you using a gcc
release that old?) It uses the -ftabstop= option (defaulting to 8)
to determine whether indentation lines up or not.
Inconsistent tabstops and mixing of spaces and tabs can certainly
cause problems.
That would be detected quite easily if the default for -ftabstop were,
say, 27. Then the chance of accidentally matching indents with tabs and spaces would be negligible.
On 23/09/2024 07:16, David Brown wrote:
On 22/09/2024 22:39, Keith Thompson wrote:
Bart <[email protected]> writes:
On 22/09/2024 16:11, Kaz Kylheku wrote:[...]
Also GCC has been able to diagnose misleading indentation for some
years now.
How many years was that out of the last 52? How exactly do you turn it >>>> on? Since -Wall -Wpedantic -Wextra doesn't report it.
The -Wmisleading-indentation option was added to gcc on 2015-05-12,
and incorporated into -Wall 2015-12-10. gcc 6.1.0 has the option
and includes it in -Wall; gcc 5.3.0 does not. (Are you using a gcc
release that old?) It uses the -ftabstop= option (defaulting to 8)
to determine whether indentation lines up or not.
Inconsistent tabstops and mixing of spaces and tabs can certainly
cause problems.
That would be detected quite easily if the default for -ftabstop were,
say, 27. Then the chance of accidentally matching indents with tabs
and spaces would be negligible.
Isn't that the opposite of what you want, though?
On 23/09/2024 09:06, Richard Harnden wrote:
On 23/09/2024 07:16, David Brown wrote:
On 22/09/2024 22:39, Keith Thompson wrote:
Bart <[email protected]> writes:
On 22/09/2024 16:11, Kaz Kylheku wrote:[...]
Also GCC has been able to diagnose misleading indentation for some >>>>>> years now.
How many years was that out of the last 52? How exactly do you turn it >>>>> on? Since -Wall -Wpedantic -Wextra doesn't report it.
The -Wmisleading-indentation option was added to gcc on 2015-05-12,
and incorporated into -Wall 2015-12-10. gcc 6.1.0 has the option
and includes it in -Wall; gcc 5.3.0 does not. (Are you using a gcc
release that old?) It uses the -ftabstop= option (defaulting to 8)
to determine whether indentation lines up or not.
Inconsistent tabstops and mixing of spaces and tabs can certainly
cause problems.
That would be detected quite easily if the default for -ftabstop
were, say, 27. Then the chance of accidentally matching indents with
tabs and spaces would be negligible.
Isn't that the opposite of what you want, though?
What I would be looking for in this case is a warning if I had used tabs
and spaces (for indents) at different places in the code. If the tab setting is a sensible choice - typically 4 or 8 spaces worth - then you
can easily have code that looks right (in the sense of having visual
indents that match the real block structure) with one setting and looks
wrong with a different setting.
For example, you might write this code :
<tb>if (test)
<tb><tb>foo();
< >< >bar();
You've made an error here - you had intended to have both calls within
the conditional. With 4 spaces per tab when you made the error, this
could be spotted by tools using tabstop settings of 4, or by a code
reviewer with those settings.
However, to tools or code reviewers with tabstop settings of 8, the code would appear as:
<tb >if (test)
<tb ><tb >foo();
< >< >bar();
Now the indentation matches the syntactical structure, and the mistake
is missed.
With a tabstop of 27, the "if" and "bar()" lines do not match up, and
the mistake can be flagged.
Of course a tabstop of 27 is not necessary - anything other than a sane
value of 4 or 8 would catch almost anything. But having a bit of extra distance also catches cases of tab then space typos.
And there could also be a warning that checked directly for mixes of
tabs and spaces in indents. But such mixes can happen when moving or copying code between files, and there are some people who like to mix
8-space tabs with 4 explicit spaces in their code.
So, to me anyway, telling gcc that tabstops are 27 would suppress all
the useful warnings.
Mark Summerfield wrote:
I have this macro:
#define WARN(...) \
do { \
fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
} while (0);
which I use like this:
total++;
if (failed) {
WARN("failed because...");
} else
ok++;
I would prefer to be able to write this instead:
total++;
if (failed)
WARN("failed because...");
else
ok++;
but doing so results in a compiler error:
error: 'else' without a previous 'if'
Since the initial problem has already been solved by other messages,
here's some extra completely unsolicited advice that you are free to
ignore:
If you intend for this macro to be invoked like how one would
invoke the printf function (i.e. the arguments consisting of a
format string literal followed by arguments to be formatted into
it), you could get away with defining it like this instead:
#define WARN(msg, ...) (fprintf(stderr, "%s#%d: " msg, __FILE__, __LINE__ __VA_OPT__(,) __VA_ARGS__))
This would break if you try to invoke it with a nonliteral string,
because this takes advantage of preprocessor adjacent string
literal concatenation, but modern compilers tend to complain
mightily if you try that anyway. It would have the advantage of
expanding to an expression instead, which is often more beneficial
for macros because it permits more flexibility in placement
(something that i find unlikely to be relevant for this particular
one, but which can't really particularly hurt to have anyway).
On Sun, 22 Sep 2024 16:56:47 +0100
Bart <[email protected]> wrote:
On 22/09/2024 16:11, Kaz Kylheku wrote:
On 2024-09-22, David Brown <[email protected]> wrote:
I am not suggesting overuse of braces. I am suggesting /good/ use
of them.
<https://www.synopsys.com/blogs/software-security/
understanding-apple-goto-fail-vulnerability-2.html>
That's perhaps the most famous example of the havoc caused by
omitting useful braces.
Consistency and clarity is important. So is maintainability.
Suppose, for example, you want to add a line "attempts++;"
alongside the "ok++;" line. When the braces are there, the change
is exactly that - add the new line you want. Without the original
braces, you are now making changes to the structural syntax of the
code as well when you add in original braces - or you are making a
mistake in the code.
My super advanced text editor from the future isn't letting me do
that:
if (failed)
WARN("failed because...");
else
ok++;
attempts++; // automatic deindent
When I add a line after ok++, it deindents it to be at the same
indentation level as the if, before letting me type any content
into it.
OK, I lied about the super advanced from the future. It's just
Vim.
Also GCC has been able to diagnose misleading indentation for some
years now.
Between those two, there is nothing to worry about; just
concentrate on whether it looks prettier without the braces or
with.
So, everyone has to write code exactly to the C standard.
But when it comes to more practical matters, it's OK to depend on
the arbitrary characteristics and abilities of whichever one of
1001 different text editors we happen to be using.
Or we have to use a specific compiler with a specific set of
options.
Also GCC has been able to diagnose misleading indentation for some
years now.
How many years was that out of the last 52? How exactly do you
turn it on? Since -Wall -Wpedantic -Wextra doesn't report it.
My gcc warns just fine.
void bar(int);
int foo(int x) {
if (x)
bar(x);
x -= 1;
return x;
}
gcc -c -Wall foo.c
foo.c: In function 'foo':
foo.c:3:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation] 3 | if (x)
| ^~
foo.c:5:5: note: ...this statement, but the latter is misleadingly
indented as if it were guarded by the 'if'
5 | x -= 1;
| ^
It is a failure in the design of the language. You can't really
depend on ad hoc features of the tools you use to create and
compile source code.
At least guidelines can be used such as always using braces, then
errors can occur less often.
(I've been bitten by this endless times when trying to add
debugging statements to existing code. If that code consistently
used braces, then it wouldn't happen.)
I wonder why I was not bitten by that more than, may be, 5 times
in 30+ years. Probably, I am doing something wrong.
My long-standing habit is to write this
if(x) bar(x);
or this
if(x){
bar(x);
}
but never this
if(x)
bar(x);
The reason for this habit is that many years ago I got bitten by
trying to add a line in the last case. The habit subsequently
adopted has proven very effective at eliminating such errors.
On 09/24/24 7:36 AM, Tim Rentsch wrote:
My long-standing habit is to write this
if(x) bar(x);
or this
if(x){
bar(x);
}
but never this
if(x)
bar(x);
The reason for this habit is that many years ago I got bitten by
trying to add a line in the last case. The habit subsequently
adopted has proven very effective at eliminating such errors.
It is just weird.
This is no different from insisting in using "Yoda conditions", claiming
that "many years ago I was bitten by an accidental = in place of ==". In reality we all know that regardless of how many scary stories someone
might tell about dangers of accidental assignment in place of
comparison, it just does not happen. There's no such issue. People just
don't make this mistake.
The same applies to
if(x)
bar(x);
This is the right thing to do. It is the most readable and elegant way
to write a simple `if`. And the dreaded "one day you will add a line and suffer" just doesn't happen. There's no such issue. People just don't
make this mistake.
On 09/24/24 7:36 AM, Tim Rentsch wrote:
My long-standing habit is to write this
if(x) bar(x);
or this
if(x){
bar(x);
}
but never this
if(x)
bar(x);
The reason for this habit is that many years ago I got bitten by
trying to add a line in the last case. The habit subsequently
adopted has proven very effective at eliminating such errors.
It is just weird.
This is no different from insisting in using "Yoda conditions", claiming
that "many years ago I was bitten by an accidental = in place of ==". In reality we all know that regardless of how many scary stories someone
might tell about dangers of accidental assignment in place of
comparison, it just does not happen. There's no such issue. People just
don't make this mistake.
The same applies to
if(x)
bar(x);
This is the right thing to do. It is the most readable and elegant way
to write a simple `if`.
And the dreaded "one day you will add a line and
suffer" just doesn't happen. There's no such issue.
People just don't
make this mistake.
Michael S <[email protected]> writes:
The idea of changing the language to always require braces is
unnecessary and wasteful (besides being a non-starter for purely
practical reasons).
Bart <[email protected]> writes:
On 24/09/2024 15:36, Tim Rentsch wrote:
Michael S <[email protected]> writes:
The idea of changing the language to always require braces is
unnecessary and wasteful (besides being a non-starter for purely
practical reasons).
Well, practically it is not difficult.
Changing a compiler to require compound statements would not be
difficult. Changing the language in that way would be completely impractical, because it would break existing code.
(Maybe the language needs a construct like 'elif', as is used in
On 2024-09-24, Bart <[email protected]> wrote:
(Maybe the language needs a construct like 'elif', as is used in
The language doesn't need such a construct in order for a compiler
to implement it internally.
You just have to treat the token sequence else if as a supertoken.
Doing it at the grammar level may require two symbols of lookahead,
which is a a problem for off-the-shelf parser generation tooling based
on LALR(1) and whatnot.
On 24/09/2024 20:59, Kaz Kylheku wrote:
On 2024-09-24, Bart <[email protected]> wrote:
(Maybe the language needs a construct like 'elif', as is used in
The language doesn't need such a construct in order for a compiler
to implement it internally.
You just have to treat the token sequence else if as a supertoken.
Doing it at the grammar level may require two symbols of lookahead,
which is a a problem for off-the-shelf parser generation tooling based
on LALR(1) and whatnot.
That sounds as much of a hack as making a special case for 'else if' so
that it doesn't need 'else {if' (and a matching } later on) when
enforcing compound statements for such blocks.
One consequence of how it works now is that if I write:
if (a) {}
else if (b) {}
else if (c) {}
else {}
then use a tool I have to visualise C code in my syntax, it generates:
if a then
else
if b then
else
if c then
else
fi
fi
fi
The nested nature is revealed. If there were 50 'else if' links, the
output would disappear off to the right.
With elif, elsif etc, the compiler has the option to keep the internal structure linear (which also means it will not put pressure on the stack
if somebody decides to write a million of them).
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
On Sat, 28 Sep 2024 05:02:05 -0700
Tim Rentsch <[email protected]> wrote:
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
{ at the same level of indentation as its matching }
On 09/28/24 5:02 AM, Tim Rentsch wrote:
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Michael S <[email protected]> writes:
On Sat, 28 Sep 2024 05:02:05 -0700
Tim Rentsch <[email protected]> wrote:
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
{ at the same level of indentation as its matching }
Certainly it is true that the layout style shown has the open
brace at the same level of indentation as the matching close
brace. What about that property makes this layout "more
readable"? The statement given sounds like a tautology -
I don't see that any new information has been added.
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
On 09/28/24 10:47 PM, Tim Rentsch wrote:
efer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
No, the spacing in question is the spacing between the `if` condition
and the first line of the the first compound statement.
This is unreadable and unacceptable
if (condition) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
for (abc; def; ghi) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
This is _immensely_ more readable
if (condition)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
for (abc; def; fgh)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
On 09/28/24 10:47 PM, Tim Rentsch wrote:
efer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
No, the spacing in question is the spacing between the `if` condition
and the first line of the the first compound statement.
This is unreadable and unacceptable
if (condition) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
for (abc; def; ghi) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
This is _immensely_ more readable
On 09/28/24 10:47 PM, Tim Rentsch wrote:
efer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
No, the spacing in question is the spacing between the `if`
condition and the first line of the the first compound statement.
On 09/28/24 10:47 PM, Tim Rentsch wrote:
efer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
No, the spacing in question is the spacing between the `if` condition
and the first line of the the first compound statement.
This is unreadable and unacceptable
if (condition) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
for (abc; def; ghi) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
This is _immensely_ more readable
if (condition)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
for (abc; def; fgh)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
This readability problem exists one the other end with struct
declarations and `do{}while` syntax as well
typedef struct MyStruct
{
int a;
double b;
char c;
} MyStruct; /* <-- Bad! No vertical separation! */
do
{
whatever1;
whatever2;
} while (condition); /* <-- Bad! No vertical separation! */
I don't have a perfect solution for this variation of the same issue.
So, I tend to use
Andrey Tarasevich <[email protected]> writes:
[...]
This is unreadable and unacceptable[...]
if (condition) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
for (abc; def; ghi) {
whatever1; /* <-- Bad! No vertical separation! */
whatever2;
}
This is _immensely_ more readable
if (condition)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
for (abc; def; fgh)
{ /* <-- Good! Vertical space */
whatever1;
whatever2;
}
Andrey, I hope you're aware that you're stating your own personal
preferences as if they were incontrovertible fact.
Readability is a combination of the text being read and the person
reading it. I accept without question that *you* find K&R-style
brace placement "unreadable and unacceptable". A lot of experienced
C programmers, myself included, either prefer the K&R style or
find both styles more or less equally readable. And many prefer
vertically aligned braces but can deal with K&R-style braces.
On Sat, 28 Sep 2024 21:53:06 -0700
Tim Rentsch <[email protected]> wrote:
Michael S <[email protected]> writes:
On Sat, 28 Sep 2024 05:02:05 -0700
Tim Rentsch <[email protected]> wrote:
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
{ at the same level of indentation as its matching }
Certainly it is true that the layout style shown has the open
brace at the same level of indentation as the matching close
brace. What about that property makes this layout "more
readable"? The statement given sounds like a tautology -
I don't see that any new information has been added.
It makes it easier to see where block starts and where it ends.
Opening { followed by empty line is more bold visually than 'if
something { ' or then '} else {'.
Now, I can live with both styles, but can see why many people
prefer style advocated by Andrey.
[...]
For what it may be worth, the K&R style is pretty much baked into the
Awk language. These two Awk programs are not equivalent:
/foo/
{
foo++
}
vs:
/foo/ {
foo++
}
[...]
It is an undeniable fact that alignment and grouping is important
in visual design, and that use of these elements (and others)
is important in creating signs and displays that are easy to
grok at a glance.
[...]
Also, I can see how the structure is more nicely conveyed when the
eopening braces are indented, if the reader temporarily blocks out
the visibility of the code and focuses on only seeing the braces:
This:
{
{
{
}
}
}
{
}
versus:
{
{
{
}
}
} {
}
In other words, the vertical braces enable a mode of visually filtering
the code that may be of use to some. (Though, to me, choosing to see
the braces while suppressing the rest of the code seems wrongheaded. Or wrong-eyed?)
[...]
On 09/28/24 10:47 PM, Tim Rentsch wrote:
efer?
Er... The answer to his question is already present in the quoted
portion of my post. "The vertical spacing introduced..."
Does that mean you think this
if (failed) {
...
} else {
...
}
is just as readable? Or is it something besides the
vertical spacing that bears on your "more readable"
judgment?
No, the spacing in question is the spacing between the `if` condition
and the first line of the the first compound statement.
This is unreadable and unacceptable
This readability problem exists one the other end with struct
declarations and `do{}while` syntax as well
I don't have a perfect solution for this variation of the same issue.
So, I tend to use
typedef struct MyStruct
{
int a;
double b;
char c;
} MyStruct;
do
{
whatever1;
whatever2;
} while (condition);
although admittedly this has its own drawbacks.
On Sat, 28 Sep 2024 21:53:06 -0700
Tim Rentsch <[email protected]> wrote:
Michael S <[email protected]> writes:
On Sat, 28 Sep 2024 05:02:05 -0700
Tim Rentsch <[email protected]> wrote:
Andrey Tarasevich <[email protected]> writes:
[...]
And don't use "Egyptian" braces [the style used in the
first edition of The C Programming Language, by Kernighan
and Ritchie].
This is the proper formatting style with braces
if (failed)
{
...
}
else
{
...
}
The vertical spacing introduced by the `{` line provides
separation between condition and the branch, which makes
your code much more readable. [...]
What qualities does this layout style have that make it "more
readable", other than it being one that you like or prefer?
{ at the same level of indentation as its matching }
Certainly it is true that the layout style shown has the open
brace at the same level of indentation as the matching close
brace. What about that property makes this layout "more
readable"? The statement given sounds like a tautology -
I don't see that any new information has been added.
It makes it easier to see where block starts and where it ends. Opening
{ followed by empty line is more bold visually than 'if something { ' or
then '} else {'.
Now, I can live with both styles, but can see why many people prefer
style advocated by Andrey.
On 09/24/24 7:36 AM, Tim Rentsch wrote:
My long-standing habit is to write this
if(x) bar(x);
or this
if(x){
bar(x);
}
but never this
if(x)
bar(x);
The reason for this habit is that many years ago I got bitten by
trying to add a line in the last case. The habit subsequently
adopted has proven very effective at eliminating such errors.
It is just weird.
This is no different from insisting in using "Yoda conditions",
claiming that "many years ago I was bitten by an accidental =
in place of ==".
In reality we all know that regardless of how many scary
stories someone might tell about dangers of accidental
assignment in place of comparison, it just does not happen.
There's no such issue. People just don't make this mistake.
The same applies to
if(x)
bar(x);
This is the right thing to do. It is the most readable and
elegant way to write a simple `if`.
And the dreaded "one day you will add a line and suffer" just
doesn't happen. There's no such issue. People just don't make
this mistake.
On 09/21/24 2:54 PM, Keith Thompson wrote:
What you call "Egyptian" braces is the style used by the creators
of the language
Firstly, this is style. Being a "creator of the language" does not
make one an authority on code formatting style.
Secondly, most people pick up "the style used by the creators of
the language" from the code samples used in the 2nd edition of K&R
book. And, as we know, "the creators of the language" went a
little lazy here. The samples were considered of "low importance"
and fell victim to the tightening publishing schedules in front of
the looming "threat" of the upcoming ANSI standard. The code
samples were never properly updated to match the style and spirit
of modern C.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 141:13:49 |
| Calls: | 12,087 |
| Files: | 14,998 |
| Messages: | 6,517,442 |