0

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!

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • 2
    Once the Roslyn compiler and C# 6.0 ship you should be able to examine all of the syntax nodes in a very convenient way. Its not out yet, but the CTP was very impressive. – Aluan Haddad Feb 09 '14 at 09:52
  • All of this can easyly be done with searching keywords, searching end of class(funktion) and parsing it to classes. – Eddynand Fuchs Feb 09 '14 at 09:53
  • @EddynandFuchs that's true, but it may be hard to maintain as new versions of C# and VB ship. For example, C# is said to be getting new syntax for Property and Method definitions. – Aluan Haddad Feb 09 '14 at 09:54
  • @AluanHaddad it would work with a database of syntaxes(keywords(function,...), startchars({, *keyword*,...), endchars(}, End *keyword) – Eddynand Fuchs Feb 09 '14 at 09:57
  • @EddynandFuchs it depends on the syntax they settle on I've seen this proposed for method definitions: `public Point Move(int dx, int dy) => new Point(X + dx, Y + dy);`and this `public double Distance => Math.Sqrt((X * X) + (Y * Y));` – Aluan Haddad Feb 09 '14 at 10:02
  • 1
    If you need to ask you probably shouldn't be doing this project... Visual Studio has built in code metrics and analysis that does a lot of this anyway. http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-components-postattachments/00-01-77-44-83/CTP_5F00_Metrics_5F00_Window.png – ta.speot.is Feb 09 '14 at 10:17
  • @ta.speot.is I share the same opinion; developing a custom tool to calculate metrics seems to be a waste of time. If the built-in code metrics functionality of Visual Studio does not meet all requirements, one should try NDepend: http://www.ndepend.com/ – Matze Feb 10 '14 at 08:15

3 Answers3

1

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...

Community
  • 1
  • 1
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
1

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

erikkallen
  • 33,800
  • 13
  • 85
  • 120
0

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

http://stylecop.codeplex.com/

Mark Redman
  • 24,079
  • 20
  • 92
  • 147