Home
Do you know how to avoid problems in if-statements?
  v1.0 Posted at 25/04/2018 8:01 AM by Tiago Araujo

Try to avoid problems in if-statements without curly brackets and just one statement which is written one line below the if-statement. Use just one line for such if-statements. If you want to add more statements later on and you could forget to add the curly brackets which may cause problems later on.​

​if (ProductName == null) ProductName = string.Empty; if (ProductVersion == null)
ProductVersion = string.Empty; if (StackTrace == null) StackTrace = string.Empty;

​​​Figure: Ba​d Example

if (ProductName == null)
{
ProductName = string.Empty;
}
if (ProductVersion == null)
{
ProductVersion = string.Empty;
}
if (StackTrace == null)
{
StackTrace = string.Empty;
}

​Figure: Good Example


Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.