Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, July 6, 2010

Where did FxCop go?

I've used FxCop to do code analysis for years now (and brow beat co-workers to use it, too).  I've incorporated it into my automated build scripts and use it every time I do a code review.  So imagine my dismay when I heard the FxCop site was no longer running (I usually get there from the MSDN Library entry about FxCop).  I wasn't sure if this was just a case of a dead link, but then I read this article by Jonathan Allen on  InfoQ.  Apparently FxCop 1.36 was yanked from the Microsoft Downloads site.  Jonathan's article directs the reader to the Windows 7 SDK.  The Microsoft Download site has an entry for FxCop 10 but it turns out that just gets you a readme.txt file instructing you to download the Windows SDK for Windows 7.

That's not terribly useful.  There isn't even a link to where to get the Windows 7 SDK in the readme file.

So thank you to InfoQ and Jonathan for providing the link.

BTW, it seems the only reason the older FxCop version was removed was because the functionality FxCop provides is in some versions of VS2010.  This is going to have an impact on my automated build scripts so I expect I'll be following the advice from this blog post from Travis Illig's blog at some point - so thanks to him, too.

Monday, June 14, 2010

Which came first, the variable or the literal?

Today I received the feedback from a code review one of my peers did of my code and they asked a question about a particular coding style technique I use.  Specifically they asked why when comparing a variable to a literal value I always put the literal value on the left side of the operator?  Put another way, why do I do this:
if(null == someVariable)
instead of this:
if(someVariable == null)

After all, this person pointed out, the second method is more easily read as "someVariable equals null" as opposed to "null is equal to the value of someVariable."  I think that's a fair point and while I'm a big proponent of increasing the readability of code this is an instance where I'll take the hit.

My habit of putting the literal value before the variable comes from dealing with Javascript.  When coding Javascript I might check to see if a value is set to zero, like this:
if(someVariable = 0)

Maybe that looks good to me because I haven't had my third espresso of the day - but it introduces a bug in my code.  I fat-fingered the operator and put = instead of ==.  So now the statement isn't doing an evaluation, it's doing an assignment.  I'm not going to catch that as part of a compile because Javascript is not compiled.  And it's not going to raise a run-time error because it's syntactically valid.

But it's not going to do what I want it to do.

Because I've been burned by this in the past I got in the habit of putting the literal on the left side of the operator.  Because while I still won't get any compile-time love I will get a run-time error when the code says:
if(0 = someVariable)

Yes, the code review was being done against some of my C# code so the same risk isn't there.  But I don't feel like changing this habit from language to language.  Especially where I'm doing ASP.NET and am jumping between C# and Javascript all day anyway.

Wednesday, April 29, 2009

I don't know what it's called, but I like it

I've recently started using JetBrains' Resharper. Today I was reviewing the code inspection rules and came across a few that had to do with using an operator with which I was unfamiliar. So I took a moment to learn about the ?? operator (link) which must have come out with the .NET 2.0 framework because it has to do with nullable types.

What it does is kind of like the old IsNull method from VB (you remember VB don't you?). Here's an example in two lines:

int? x = null;

int y = (null != x) ? x : -1;

The first line declares a nullable int variable named x which is assigned null.
The second line declares a non-nullable int variable named y. Because it's not nullable we've got to ensure a null value isn't being assigned (otherwise we'll raise an exception). To do this we're using the ternary operator. What the ?? operator does is allow us to write the second line like this:

int y = x ?? -1;

So if x is null y is set to -1. Nice, right? Especially if you replace x and y with more meaningful variable names, such as this:

int? someMeaningfulName = null;

int whatYouReallyWant = someMeaningfulName ?? -1;

Or, more common in my current job, getting values from a web form or querystring:

int desiredFormId = 0;

if (null != Request.Form["Activate_FormId"]) {
desiredFormId = Request.Form["Activate_FormId"];
}

can become:

int desiredFormId = Request.Form["Activate_FormId"] ?? 0;

Now if I could just figure out how to pronouce this operator I can tell people about it.

Friday, January 30, 2009

Tool Review: RockScroll

Doing my fluffy-Friday reading I found this post on Scot Hanselman's blog about a tool named RockScroll that turns your VS2005/2008 scroll bar into a thumbnail view of the open document (when working with .cs files anyway). It also has a nifty feature that if you double click a word in the source code, say a variable name, it will highlight all occurrences of that word in a lovely lilac color (and red in the thumbnail scroll bar - which is the useful bit, IMO).

Anyway, I'm digging this new tool and hope you might also find it useful.

Monday, January 12, 2009

Additional C# Code Snippets for VS2005

Something you might find useful when coding in C# is the way code snippets can increase your productivity and provide guidance/reminders on syntax. If you like snippets, you might want to grab the additional snippets published here.