Claudio 的个人资料Claudio Lassala in Softw...照片日志列表更多 工具 帮助
5月27日

Checking out the Source Analysis for C#

I've just spent about 30 minutes checking out the recently announced Microsoft Source Analysis for C#. I definitely love the idea, but I'm not sure I'll be using it just yet. Here are some of my impressions (again, I haven't spent a lot of time on it yet)...

All using directives must be placed inside of the namespace

Hmm, but VS itself violates that rule on every class we create...!

Fig01

The property must not be placed on a single line. The opening and closing curly brackets must each be placed on their own line

Huh? But that an auto-implemented property. What would we want to split that into multiple lines?!

Fig02

Variable names must not start with 'm_'

Hmpf, that's the standard with have here at the company...

Fig03

A closing curly bracket must not be preceded by a blank line

Ok, this one I like.

Fig04

Tabs are not allowed. Use spaces instead

Huh? Thanks, but no, thanks. I've seen a lot of people complaining online about this one too. What's the problem with using tabs??

Fig05

The call ... must begin with 'this.'...

Ah, I like that one. This is something we've made as part of our guideline here, but couldn't use FxCop to trap for that, since the 'this.' doesn't make into the IL.

Now, the problem here is that, again, the code that VS generates violates this rule all over the place, such as on the example below:

Fig06

I'll continue to use this tool to see if it's going to stick with me. I've tried it on a very small project, and got 246 violations; the vast majority where produced by code generated by VS. On a big project, I'd probably end up with a LOT more violations, so I'm not sure I can live with that. I believe there is a way to disable some of the rules, but haven't looked for it just yet...

Anyway, I do appreciate Microsoft releasing tools like this one. Hopefully, more and more developers will start paying more attention to how they write code.

评论 (8)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

neslihan发表:
very good . Thanks
7 月 30 日
LiLex发表:
It is rational to use spaces because not everyone uses the same tab settings in his/her IDE. In my case, when I navigate source code files on CodePlex.com, tabs are misaligned (CodePlex web page uses more spaces than 4).
 
Also I find it a must to rurn off some rules (persoanlly I hate "this", and ReSharper hates it, too) on legacy projects. Otherwise, over 10,000 warnings may be provided by StyleCop.
7 月 28 日
As a matter of fact, "this" ALWAYS makes its way into IL.
private string foo;

public string Foo

{

get

{

return foo;

}

set

{

this.foo = value;

}

}

 

gets translated to

method public hidebysig specialname instance string get_Foo() cil managed
{
    .maxstack 1
    .locals init ( [0] string CS$1$0000)
    L_0000: nop
    L_0001: ldarg.0
    L_0002: ldfld string ItauTests.Program::foo
    L_0007: stloc.0
    L_0008: br.s L_000a
    L_000a: ldloc.0
    L_000b: ret
}
.method public hidebysig specialname instance void set_Foo(string 'value') cil managed { .maxstack 8 L_0000: nop L_0001: ldarg.0 L_0002: ldarg.1 L_0003: stfld string ItauTests.Program::foo L_0008: ret } arg.0 is "this"If you use .NET Reflector you'll get:public string Foo { get { return this.foo; } set { this.foo = value; } }

 

7 月 8 日
Jane发表:
In the Detailed Settings pane you can choose to switch off "Analyze designer files" and "Analyze generated files" which I found help remove the number of warnings. 
7 月 8 日
Don Awalt发表:
Be wary of this add-on. When I added it to my Windows Server 2008 dev system for VS 2008, when I went into Project properties a couple of days later, I received an error message "COM object that has been separated from its underlying RCW cannot be used" and nothing showed up on any of the Properties pages! I un-installed this add-on and the problem went away.

Not worth the hassle, besides I too got a bunch of really un-useful reports. Very hard to find the gems in all the fluff.
5 月 30 日
没有名字发表:
"What's the problem with using tabs??"
You can change how many spaces wide a tab looks in the IDE.  That's the problem.  are they 2 spaces, 4, 8, or what?  Visual Studio defaults them to 4, but meanwhile, most diff tools display them at 8.

As soon as tabs mix with spaces, then formatting goes to hell.

That said, that's the only one of these rules that I agree with.
5 月 30 日
Holy crap!
 
Sorry about the duplicate comments ... don't know how they came up. I only submitted once. Apparently Live Spaces have issues with Firefox... Any way you could delete the duplicate comments? I don't seem to find a way to delete my own.
 
Sorry about the inconvenience again...
5 月 28 日
Hi Claudio,

Thank you for the great review. I too advocate for developers to adopting good coding style.

I wonder if you like our CodeIt.Right tool - http://submain.com/codeit.right - it is more efficient as it can automatically correct violations and code smells for you.

Please feel free to contact me if you have any questions or are interested in a review copy.

5 月 28 日

引用通告

此日志的引用通告 URL 是:
http://claudiolassala.spaces.live.com/blog/cns!E2A4B22308B39CD2!779.trak
引用此项的网络日志