I'm probably going to make myself unpopular with this comment, but the only time braces on separate lines is acceptable, is when applying the K&R styleguide. Other than that, having braces on new lines doesn't just waste space, but any argument saying "but it helps me see my scope boundaries" is just as invalid as any justification for Hungarian notation. Literally every modern (and halfway decent) IDE will highlight the braces of the current scope and VS Code has plugins like Bracket Colorizer which will highlight the entire scope with different colours so you can easily tell them apart. It'll even go red if it detects an error (or you add too many lambdas in POC code in JS).
I won't judge code based on brace placement, but I will reformat it so it's readable. The only reason I say this, is because I used to write code the same way; braces on the next line for each and every little scope.
I disagree. I genuinely find newline braces easier to read an follow. I don't want my code to be a Christmas tree, with different colors for braces. I am able to follow it easily if it's on newlines.
The space wasted is minimal. I'd rather have 5 more characters per brace than find it harder (even if slightly) to follow my code.
I don't understand your last paragraph. What isn't readable about different-line-braces, exactly? Braces go on the same indentation as the line above. Only the inner code is indented more.
Little scopes, as in, one-statement scopes, don't need braces. Languages such as C# and Java allow you to omit braces, I'm assuming you know that.
As a little confession, I put braces for statements like that as well. Again, I personally find it easier to read.
Is he talking about disk size wastage or screen size wastage? Because to me, I only write code in the 20-25 lines of code I see on the screen, and 1-2 lines wasted is a lot sometimes. I want to see my function on the screen if possible
I'll tell you why and how I find it easier to read.
No matter the scope, provided correct indentation, a code block will always look similar to this:
if (whatever) {
// Do whatever
}
I see the beginning statement/keyword and just scroll down.
Whereas code formatted like this:
if (whatever)
{
// Do whatever
}
Looks compacter, and I have a completely useless line to mentally parse before I can go on to to scrolling down. I find the brace distracting.
Another downside (and this is just programmers being programmers), is that having a brace below allows for situations like this (I've seen this in production code):
if (obsureConditionThatHappensOnceInABlueMoon) return 0xff; // this is just a quick test!
{
// Evaluate some more
return (anotherWeirdCondition ? 0xf0 : 0xff);
}
This broke this ancient piece of SW written in VC2005 on 0.001% of runs because of some quirk in the Windows CE kernel.
Yes, I'm aware that you can omit braces from Single-Statement scopes, but you shouldn't unless it's super clear.
I've written a whole boatload of bugs, forgetting to add the missing braces - it happens, such is life, so I prefer just writing the braces, at least like so:
if (cond) { break; }
For example.
I guess the only place I have to put braces in their own line, is when I need to scope something separately, like when I need a variable removed from the stack/scope right after use.
(This will be a bad example):
void foo() {
// Do stuff
{ // scope this to release the mutex immediately after use and not block other portions of app
unique_lock<mutex> locky(m_myMutex);
myThreadUnsafeContainer.push_back(stuff);
}
// Do more time-uncritical stuff
}
He's checking the return of the function in the if statement as a boolean, and returning true or false. He could just return the function and the result would be a boolean
31
u/avmist15951 Sep 11 '21
Having open braces on their own lines makes me uncomfortable
36
u/KurosakiEzio Sep 11 '21
Is this a picture of me after 12 hours of pure grind? Yes it is
16
u/cdp1337 Sep 11 '21
Sleep deprivation causes some weird code.
7
u/george6823 Sep 11 '21
He probably had some other functionality that was removed later on.
11
u/seeroflights Sep 10 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/Equixels Sep 11 '21
I would have coded this the same way for readability reasons.
2
u/jjman72 Sep 11 '21
hacker.
2
u/_senpo_ Sep 11 '21
this is what happens when you get paid by line
1
u/Knuffya Sep 11 '21
especially variable naming. unless these are not variables at all but getters. C# really has some weird quirks.
1
u/wouldsmackurbooty Sep 11 '21
This is bad because it can be done in one line correct? Are there other reasons this is bad code that I am not seeing?
0
u/[deleted] Sep 10 '21
[deleted] — view deleted comment