break

C#: FxCop

Lately, I have been putting my programming practices to the test with FxCop. FxCop makes sure your code conforms the best .NET coding practices.

I will write in a separate post how to use FxCop, but for the moment i will write about some of the improvements FxCop suggested for my code.

Warning, Certainty 95, for UseLiteralsWhereAppropriate
{
Resolution : “Field ‘MOST_ACTIVE’ is declared as ’static readonly’
but is initialized with a constant value ‘Most Active’.
Mark this field as ‘const’ instead.”
}

The line that was causing this warning:
private static readonly string MOST_ACTIVE = “Most Active”;
Solution:
private const string MOST_ACTIVE = “Most Active”;

Warning, Certainty 95, for TestForEmptyStringsUsingStringLength
{
Resolution : “Replace the call to String.op_Inequality(String.Empty)
in ‘MyDefaultPage.Populate(String, String):Void’ with
a call to String.IsNullOrEmpty.”
}

The line that was causing this warning:
if (panel != String.Empty)
Solution:
if (!String.IsNullOrEmpty(panel))

Warning, Certainty 90, for DoNotInitializeUnnecessarily
{
Resolution : “MyDefaultPage.MyDefaultPage() initializes field
behavior of type DeltaOptions.Windows.Forms.Behaviour.IReportFormBehavior
to null. Remove this initialization as it will be done
automatically by the runtime.”
}

The line that was causing this warning:
private IReportFormBehavior behavior = null;
Solution:
private IReportFormBehavior behavior;

Warning, Certainty 95, for RemoveUnusedLocals
{
Resolution : “MyDefaultPage.Populate():Void declares a local,
‘mypageOptions’, of type System.String, which is never
used or is only assigned to. Use this local or remove
it.”
}

The line that was causing this warning:
Some variable that wasn’t being used in a method
Solution:
Delete or comment the variable not used.

These are some of the multiple warnings that FxCop can point about your code. As you noticed, running your code against this tool will improve your source code greatly.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CAPTCHA Image
Reload Image