Search code examples
c#if-statementbraces

Is it better to use braces or no braces for single-line if blocks?


What is better coding practice for single-line code blocks following an if statement - braces or no braces? Just to avoid too many "Well, it depends on what language you're using..." answers, let's say for C#. In other words, which is better:

if(somecondition)
{
  singleLineStatement;
}

Or

if(somecondition)
  singleLineStatement;

Solution

  • I think the consensus is to use braces. If you omit the braces there is a risk that someone will add an extra line without noticing the missing braces.