سؤال

The customer has million lines of code (VB.Net and C#), and wants us to develop a tool to estimate the quality of the code.

What the information the customer wants to know include:

1)how many lines of comments in one code file

2) how many functions implemented in one class

3) whether all possible exception has been wrapped by a try/catch block

4) how many attributes attached to one function

5) ... (the customer said that the tool we provide should be configured and extensible so that they can implement more ideas later)

We plan to write a VS.Net add-on, which can parse the code of the opening project in time. seems the interesting thing in here is that we need to parsing the code of C# and VB.Net.

Please kindly provide some tips about how to start this interesting task.

Thanks in advanced!

هل كانت مفيدة؟

المحلول

You ask a very broad question, but you should begin by studying existing parser's APIs. Once you do that you're golden.

For example look at this SO question which provides some parsers for C#. Of course you could write your own but I don't find any reason to since the task isn't very easy.

So you get your AST and once you do that you have all the information you want.
Keep in mind that if you reference a type that isn't in the file you must have to get it from another one, and it could also be a type from .NET. So there is definitely more work to be done.

To go through your list:

1)how many lines of comments in one code file: You could find it through your C# parser of choice. They recognize comment aswell

2) how many functions implemented in one class: Likewise, should be very easy

3) whether all possible exception has been wrapped by a try/catch block: Likewise, just find exception throws (the parser is likely to have a special type for language keywords, so looking for throw should be easy).

4) how many attributes attached to one function: and... Likewise

5) ... (the customer said that the tool we provide should be configured and extensible so that they can implement more ideas later): Shouldn't differ from any other project. Just make sure you're using good design principles, keeping everything abstract, using interfaces wisely, make your work in layers, etc. etc...

نصائح أخرى

You can use Roslyn. For C# you can also use NRefactory.

Have a look at Stylecop, you may be able to add rules get the information you want?

http://stylecop.codeplex.com/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top