Home > Refactorings > Adding and removing block delimiters in C#/C++ and JavaScript

Adding and removing block delimiters in C#/C++ and JavaScript

March 12th, 2012

In C#, C++ and JavaScript languages, curly braces are used as block delimiters. A block allows multiple statements to be written inside. If a block contains a single statement, block delimiters, in most cases, are optional. For example, a single statement inside a loop does not require block delimiters:

CodeRush Optional block delimiters sample

There are two refactorings shipped in DevExpress Refactor! Pro and CodeRush Xpress that help you to add or remove optional block delimiters:

  • Add Block Delimiters
  • Remove Block Delimiters

The Add Block Delimiters refactoring embeds a single statement into curly braces. This refactoring is available at the start of the statement:

CodeRush Add Block Delimiters refactoring sample

Applying this refactoring may increase code readability and allow you to make the parent block a multi-statement block, so you can add additional statements inside, for example:

CodeRush Add Block Delimiters refactoring result

The Remove Block Delimiters refactoring, on the other hand, removes optional curly braces if appropriate. It is available on a curly brace:

CodeRush Remove Block Delimiters refactoring sample

This may also improve readability in certain cases:

CodeRush Remove Block Delimiters refactoring result

As always, refactorings are smart, and are not available where they are not appropriate.

—–
Products: Refactor! Pro, CodeRush Xpress
Versions: 11.2 and up
VS IDEs: 2008 and up
Updated: Mar/12/2012
ID: R053

Similar Posts:

  1. March 14th, 2012 at 12:52 | #1

    Cool! Looks like that would be very good for codebases trying to impose a one way or the other standard of doing this too.

  2. March 23rd, 2012 at 06:00 | #2

    Now, if we could only have this for lambdas, 4 years later

  3. April 9th, 2012 at 08:04 | #3

    Hi Ruben, we have implemented support for lambdas in the Add Block Delimiters refactoring for the next major release (2012.1).