Having the following code:
enum TestEnum
{
Value1,
Value2
}
private static TestEnum DeserializeConfiguration(string configuration)
{
TestEnum result;
if (configuration == "OldValue")
{
result = TestEnum.Value1;
}
else
{
result = Enum.TryParse(configuration, true, out TestEnum databaseType)
? databaseType
: TestEnum.Value1;
}
return result;
}
SonarQube raises S3240 “The simplest possible condition syntax should be used” on the IF statement.
Following the rule I can rewrite the code to the following method:
private static TestEnum DeserializeConfiguration(string configuration)
{
TestEnum result;
result = configuration == "OldValue"
? TestEnum.Value1
: Enum.TryParse(configuration, true, out TestEnum databaseType)
? databaseType
: TestEnum.Value1;
return result;
}
But, this raises S3358 “Ternary operators should not be nested”.
At least for readability S3240 should not raise if a ternary operator is already part of the IF/ELSE statement.