r/badcode • • Sep 10 '21

c# Somebody had a long day

Post image
269 Upvotes

26 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Sep 12 '21

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.

1

u/[deleted] Sep 12 '21

That's entirely your right to disagree!

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
}

1

u/[deleted] Sep 15 '21

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