Dealing with curly brace soup

Posted by Cyborgx37 on Programmers See other posts from Programmers or by Cyborgx37
Published on 2012-11-07T15:38:20Z Indexed on 2012/11/07 17:16 UTC
Read the original article Hit count: 269

Filed under:
|

I've programmed in both C# and VB.NET for years, but primarily in VB. I'm making a career shift toward C# and, overall, I like C# better.

One issue I'm having, though, is curly brace soup. In VB, each structure keyword has a matching close keyword, for example:

Namespace ...
    Class ...
        Function ...
            For ...
                Using ...
                    If ...
                        ...
                    End If
                    If ...
                        ...
                    End If
                End Using
            Next
        End Function
    End Class
End Namespace

The same code written in C# ends up very hard to read:

namespace ... {
    class ... {
        function ... {
            for ... {
                using ... {
                    if ... {
                        ...
                    }
                    if ... {
                        ...
                    }
                }
            }
            // wait... what level is this?
        }
    }
}

Being so used to VB, I'm wondering if there's a technique employed by c-style programmers to improve readability and to ensure that your code ends up in the correct "block". The above example is relatively easy to read, but sometimes at the end of a piece of code I'll have 8 or more levels of curly braces, requiring me to scroll up several pages to figure out which brace ends the block I'm interested in.

© Programmers or respective owner

Related posts about c#

Related posts about programming-practices