Search code examples
c#coding-stylestylecop

A comment may not be placed within the bracketed statement


I am using StyleCop on my C# files and it gives the warning :

SA1108 : CSharp.Readability : A comment may not be placed within the bracketed statement

for the following block of code :

// checking if the person is born or not.
if (AgeUtilities.IsPersonBorn(person.BirthDate) == false)
{
    Console.WriteLine("Error....The user is not yet born.");
}

// checking if the person's age is possible or not.
else if (AgeUtilities.IsPersonLongAgePossible(age, EXPECTEDAGELIMIT) == false)
{
    Console.WriteLine("This age is not possible in Today's world.");
}

What is the significance of this warning?


Solution

  • StyleCop tells you that you should replace/reorganise your comment above the else-statement inside the brackets, like

        // checking if the person is born or not
        // if not: check if the person's age is possible or not
        if (AgeUtilities.IsPersonBorn(person.BirthDate) == false)
        {
            Console.WriteLine("Error....The user is not yet born.");
        }
        else if (AgeUtilities.IsPersonLongAgePossible(age, EXPECTEDAGELIMIT) == false)
        {
            Console.WriteLine("This age is not possible in Today's world.");
        }
    

    Have a look here and there.