{} Matter!

        [SuppressMessage( "Style", "IDE0011:Add braces" )]
        public int BadlyWrittenCode( ref int x ) {
            if ( foo( ref x ) )
                bar( ref x );
            if ( foo( ref x ) )
                bar( ref x );
                baz( ref x );
            if ( foo( ref x ) )
                bar( ref x );
            else
                baz( ref x );
            if ( ++x < 127 )
                bar( ref x );
            x++;

            //TODO (1 year later..) Was baz always supposed to be called, or is this a bug?
            //TODO (1 year later..) Was x intended to always be incremented, or is this a bug?
        }

Code is for humans to read. By adding these brackets, you make your code’s intention (the LOGIC) clear.

(Note: For those of you in the U.S., “{}” may be called braces. Other parts of the world may call them brackets. They’re the same set! Lol. “<>, (), [], {}”. Open your mind.. Not everyone codes in the U.S.)

We now have the ability to use the new form of using, i.e. “using var outer = new Outer();”. And please use the new format when it makes sense.

But for “if”, “for”, and “while” (and others) you still should use the curly brackets “{” and “}“.

If your braceless code is ever used/refactored again, even by you, the lack of proper brackets will bite with a nasty bug and (more than likely) that will not show up at compile time.

If your argument against using curly brackets is that it’s not worth your time to take the extra milliseconds to type them.. then you’re charging too much. Way too much.

Focus on writing bug free and future-proof code instead of saving keystrokes or minimizing line counts. Personally, I am sick and tired of finding & fixing bugs caused by other people’s laziness (even though I get paid quite well to do so).

Do not write code to the lowest common denominator!

Adding the brackets is so simple, most code formatters can even add them for you.
It’s simply laziness to not add them.

Okay, an example you need? Nah, if you haven’t learned from this example, experience, or an experienced programmer, then learn from Google.. I’m done wasting my time trying to teach you.

You’re still reading? Good.
Do you contest my assertion that adding brackets/braces is always the correct way? Yes? Then show me an example that causes the code to BREAK by adding the proper braces.

I triple double dog dare you.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s