Omitting braces in this case leads to a lot of problems.
Omitting braces in this case leads to a lot of problems.
Using braces everywhere is definitely good style, but it isn't a practical solution because there's a ton of existing C/C++ code that doesn't use braces.
It would make sure that no new errors are introduced due to this.
The compiler would need to require braces in new code, but allow brace-free style in old code; you'd need to integrate it with the source control system so it knows what code is new. Doable, but tricky.
- all new files are checked
- old files are put in a whitelist for the style checker, the submitter is supposed to remove the file from the whitelist when signifcant changes are made
It is good to hear others are doing it.
It is never an error to insert braces, and the rules for inserting braces are fully deterministic in C.
if (true)
foo();
else
bar();
This looks prettier to my eyes than if(true){
foo();
} else {
bar();
}
However, I might not be the last person to touch the code. My coworker might come later and add: if (true)
foo();
else
bar();
baz();
And hence the indent problem.Why should the inability to write basic C preclude me from being allowed to pontificate about brace style like the greats?
And besides, code is for humans to read and only incidentally for computers to execute. Might as well optimize for prettiness.
if (true)
foo();
else
bar();
baz();
But I have to admit if you ever actually do that, you have failed to fundamentally understand how your code is executed. No braces means one statement. Period.In these cases languages which are auto formatted (C#, go-fmt, etc) likely have an advantage where these problems stick out more obviously.
I would say I have a pretty good understanding of how C & my code in general works, I'm currently trying to create a threadpool with support for co-routines/yielding to other threads in userspace. Nothing super fancy, but not something you can do without understanding how code is actually executed ;)
But I still lost an hours work last week because I hadn't put a brace after an if statement and when I came back to it, I didn't notice the lack of braces and put an extra statement behind it. This is why I usually stick to putting braces around everything.
if (true) foo();
else bar();
Not much room for a confused baz() here, or so I hope.No holy wars about style please! I was just offering an alternative that works well for me.
(true) ? foo() : bar();
Any style with braces looks like a cluttered mess compared to
if (foo)
throw ...
if (bar)
throw ...
if (baz)
throw ...
But I strictly limit that to the beginning of a function, and only `if (x) [throw|return] y;`you monster.
So much wasted vertical space. We use that at work and I am not at all a fan.
Try it once. You'll never go back.
I see from your comment that you might hire a team of developers to go through a critical path of important C/C++ packages checking for style violations. Any code found missing braces will have a patch submitted upstream to correct the errant style. Any package that declines the style changes would be removed from Debian. Any developer you hire that misses style violations will be removed from the team.
Another approach might be: Turn on the warning mentioned in the article and manually review any cases that come up. That still might generate a lot of cases, but it at least seems possible.
Is there another actionable interpretation of your comment that I'm missing?
This type of bug is largely introduced from someone who is going in and changing a section of code. My philosophy is that if you are editing a section of code, you should update the braces in that section along with your current patch. Over time you see a larger and larger drop in these kinds of bugs as people always, on my team due to the habit of fixing braces and a matching drop from outside contributes due to a larger and larger portion of the code matching the style guide.
Notice that I said "avoided", not "fixed". That is because I am rather focused on making code that is maintainable.
I posted this original comment to have a dialogue about something I include in my programming practices. I wanted to see how other people viewed this as a development technique.
C doesn't do this, but I like this feature from Perl for single-statement checks:
return if is_red($traffic_light);
return unless is_green($traffic_light);
In C, I usually put the early returns on their own line without curly braces, but place them all together at the start of the function to make it easier to spot inconsistencies. if ( $foo ) {
bar();
baa();
}
quux() if $foo;
And these are not: if ( $foo ) bar();
{ bar(); baz(); } if $foo;What I often see is people just ignore the output from tooling and commit anyway.
No method of prevention is perfect, but everything you do can help to increase stability in projects.
Yes, both are policy, but they are different kinds.
Of course, banishing single line blocks at the compiler would be infallible, but it's also not viable.
(Personally my preference would be a linter that automatically runs on checkin, and refuses commits that do not conform to the style guide)
one person's clutter is another person's markers. I wouldn't agree they reduce readability
What screen size? What resolution? What ide/editor window size? What font face & size? With or without soft line wrapping?
Honestly arguments about how "pretty" code is are fucking ridiculous. Despite what someone said, the purpose of source code is not to be "read" with "running" as a secondary task. That literally only applies to code written purely for educational purposes.
The purpose of code is to achieve the goals of the software as efficiently as possible. Efficiency is not just about speed - a fast but unreliable program is not efficient.
Does it matter? There will be a point at which an extra line makes the difference. And particularly with modern screen shapes, vertical space is at much more of a premium than horizontal space.
> Honestly arguments about how "pretty" code is are fucking ridiculous. Despite what someone said, the purpose of source code is not to be "read" with "running" as a secondary task. That literally only applies to code written purely for educational purposes. > The purpose of code is to achieve the goals of the software as efficiently as possible. Efficiency is not just about speed - a fast but unreliable program is not efficient.
The ability to understand software is vital to real-world usefulness though. Requirements change all the time, and so the ability to make changes to software is vital - and to effect desired changes to code you must first understand it.
What? Modern 10:16 screens give more lines than 3:4. 900x1440 gets like 70 lines with 100 columns at a decent font size, better than around 60 lines with 110 columns with 960x1280.
The exact term used was pretty.
> There will be a point at which an extra line makes the difference
So why not remove all blank lines too. They're less important for reducing issues that single line/braceless flow control blocks can cause.
By and large I do. But I don't think what you say is actually true. Given the choice between:
stepa1
if(something)
stepa2
stepa3
stepb1
stepb2
stepb3
and stepa1
if(something) {
stepa2
}
stepa3
stepb1
stepb2
stepb3
(same number of lines), I think the former is often more readable - stepa2 is visually separated in either case.The only code where readability isn't as important as functionality is finished code, and we all know that code is never finished.
I'm talking about people who skip braces on single statements, skip semicolons in JavaScript, etc because it "looks prettier" without them