ACM SIGSOFT Software Engineering Notes
Page 1
September 2013 Volume 38 Number 5
Tool for Generating Code Metrics for C# Source Code using Abstract Syntax Tree Technique Pavitdeep Singh, MIE
Prof. Satwinder Singh
Prof. Jatinder Kaur
Dept. of Credit Risk & Trading Book Markets & International Banking Royal Bank of Scotland 91987101390
Dept. of Computer Science & Eng. BBSBEC, Fatehgarh Sahib, Punjab Technical University 919781380780
Dept. of Applied Sciences University College of Eng. & Tech. Chandigarh University 918146392851
[email protected]
[email protected]
[email protected]
ABSTRACT
2. MARKET SURVEY
Software maintenance is one of the key activities in any software engineering process in which source code analysis plays a crucial role. Due to the high cost of maintenance, it has become quite necessary to produce high quality software. Over time, numerous analyses have been performed on source code to determine complexity and other metrics. Lots of papers have been published for object oriented languages but mostly concentrating on C++ and Java, very few has been published for more modern languages like C#. This paper proposes a Software Quality Assurance Tool for measuring the different code metrics for the object oriented language C# at the class and method levels. The technique consists of generating the abstract syntax tree of the source code using Nfactory libraries. The Interface is built using the Win Form application which provides an impressive GUI for the tool.
A substantial number of essays have been published on software metrics and obviously measuring the correct code metrics always remains important to various decisions. In this section, the tools that have been developed so far to measure those code metrics will be discussed.
Categories and Subject Descriptors D.2.8 [Software Engineering]: Metrics – Complexity measures, Product metrics.
General Terms Measurement
Keywords Code Metrics, Nfactory, Abstract Syntax Tree, C#, Source Code Analysis
1. INTRODUCTION In many software engineering disciplines, source code analysis represents a fundamental and preliminary step required to perform activities such as software maintenance, program transformation and software metrics extraction. This paper proposes a Software Quality Assurance Tool for measuring the different code metrics for object oriented language programs written in C#. It also makes use of the Nfactory libraries (open source) to extract the information at various levels (method/class/assembly) in order to generate the abstract syntax tree from which the various metrics are calculated. This tool can run as a standalone Win forms application without using any other application or IDE. This tool is designed to work in an interactive mode using a rich GUI interface. Another important feature of this tool is the non-invasive capability: the metrics are calculated in a background thread, so the software development process is not impacted. This capability is mandatory to correctly measure the code. This paper is organized as follows: section 2 presents the market survey, section 3 discusses the proposed methodology used to construct the syntax or parse tree from the C# source code and then calculate the metrics. Section 4 talks about the implementation details of the tool, section 5 discusses the outcome of the project. Finally, section 6 draws the conclusions and section 7 discusses the future work.
DOI: 10.1145/2507288.2507312
2.1 Tools In this section, many software metric tools will be discussed. Each one of these tools calculates several possible metrics.
2.1.1 C and C++ Code Counter (CCCC) CCCC is an open source tool, developed as a testing ground to produce reports with different metrics such as LOC, McCabe’s complexity and metrics proposed by Chidamber and Kemerer[1]. It basically deals with C++ and Java files [2].
2.1.2 Analyst4j Analyst4j is available as a stand-alone Rich Client Application or as an Eclipse IDE plugin used for the Eclipse platform. Basically it automates software measurement and provides users with a distinctive search and analysis environment based on metrics [3]. It guarantees the effective use of metrics by providing a search capability which is based upon automated software metrics. Metrics based search allows users to find facts which are otherwise difficult to find given the size of application software. For example one can find quite easily all the methods that are not structured properly (prospective risk for maintenance) using Essential Complexity (McCabe).
2.1.3 Dependency Finder Dependency finder is another free open source tool. This tool is primarily used in examining compiled Java code. Actually, it is a dependency program which extracts dependency graphs and mines them for useful information [4].
2.1.4 Java Coding Standard Checker (JCSC) JCSC is quite a powerful tool which is used to inspect different aspects of source code: Compliance with coding standard and identification of probable bad code [5]. The standard covers naming conventions for classes, interfaces, fields and parameters. Also the structural layout of the type (class/interface) can be defined. The probable bad code includes empty catch/finally block, switch without default, throwing objects of type 'Exception'. It provides support for several metrics such as Non-Commenting Source Statement (NCSS) and Cyclomatic Complexity Number (CNN).
2.1.5 Chidamber and Kemerer Java Metrics (CKJM) CKJM is basically a command line tool which is open source and calculates CK metrics from Java programs. The metrics proposed by CK are Weighted Method per Class (WMC), Depth of Inheritance Tree of a class (DIT), Number of children of a Class (NOC), Response For a
http://doi.acm.org/10.1145/2507288.2507312
ACM SIGSOFT Software Engineering Notes
Page 2
Class (RFC), Coupling Between Objects (CBO) and Lack of Cohesion of Methods (LCOM)[6].
2.1.6 OOMeter OOMeter is a powerful metric tool developed by J. Alghamdi, R. Rufai and S. Khan [7]. It measures the quality characteristics of C# and Java program source code and UML models. OOMeter also supports many other metrics such as Weighted Method per Class, Depth of Inheritance Tree of a Class, Number of Children of a Class, Response For a Class, Coupling Between Objects and Lines of Code (LOC).
September 2013 Volume 38 Number 5
3.1 C# Source Code The proposed tool initially takes a single file as input and then reads all the projects within that single file (Solution file) and then parses the project files to find all the source files within the projects. During traversal of various files present it will also filter out the files which are marked for exclusion during parsing. Once all the required files are read by the system they are passed to the language parser for parse or syntax tree creation.
2.1.7 Understand for Java
3.2 Language Parser
Understand for Java is a commercial tool that is used to analyze different groups of metrics namely Complexity, Volume and Object oriented [9]. Metrics supported by this tool are Coupling Between Objects, Depth of Inheritance Tree of a class, Number of Instance Methods, Weighted Method per Class and Cyclomatic Complexity [10].
C# source code is simply a string. Parsing the string into a syntax tree tells us that it is an invocation expression, which has a member reference as target. The syntax tree would be similar to the tree shown in Figure 2.
2.1.8 NDepend NDepend is a Visual Studio tool which is used to manage complex .NET code in order to achieve high code quality. With this tool, software quality can be measured using Code Metrics. The GUI provides visualized views in the form of Graphs and Tree maps. It supports the Code Query LINQ (CQLinq) for maximum flexibility. It computes the various metrics in different categories for .Net Code. NDepend counts the number of lines of code. It also comes with 82 other code metrics. Some of them are related to your code organization (the number of classes or namespaces, the number of methods declared in a class...), some of them are related to code quality (complexity, percentage of comments, number of parameters, cohesion of classes and stability of assemblies...), some of them are related to the structure of code (which types are the most used, depth of inheritance...) and some of them are related to code coverage [11].
2.1.9 Code Metrics in Visual Studio 2010 The Code Metrics is a tool provided by Visual Studio which provides a set of software metrics to measure the quality of the code that is being developed. It offers a quick-and-easy way to find out the complexity of the code, and to segregate code areas that may be cumbersome to maintain in the future. In Visual Studio 2010, code metric information starts at the method level, and then rolls up all the way to the assembly level. Basically, it determines five different code metrics [12] namely Cyclomatic Complexity, Class Coupling, Lines of Code, Depth of Inheritance and the Maintainability Index.
Figure 2. Syntax Tree A syntax tree doesn’t provide us the complete information regarding what someObject or DoThis are all about. DoThis most likely is an instance method, and someObject seems to be a local variable, parameter or a field of the current class. It may be that someObject could be a class name and DoThis a static field containing a delegate. The semantic tree provides the information regarding these attributes. The semantic tree constructed from the above syntax tree would to be similar to the tree shown in Figure 3. Figure 5 illustrates an example of the parse or syntax tree for the sample C# source code from Figure 4.
3. PROPOSED METHODOLOGY The proposed design of the tool consists of various steps from C# source code to syntax tree creation. Once the syntax parse is generated it is resolved to using the Type system to generate the semantic tree, which is further utilized by the tool to compute various code metrics.
Figure 3. A Semantic Tree
Figure 1. Overall Architecture The overall architecture of the proposed system is described as shown Figure 1.
Figure 4. Source Code Example
DOI: 10.1145/2507288.2507312
http://doi.acm.org/10.1145/2507288.2507312
ACM SIGSOFT Software Engineering Notes
Page 3
September 2013 Volume 38 Number 5
Figure 5. Syntax Tree Example
3.3 Code Metrics The proposed tool takes a single file as input and then reads all the projects within the file (Solution file) and then parses the project files to find all the source files within the projects. During the traversal of various files present, it will filter out the files which are marked for exclusion during parsing. Once all the required files are read by the system, they pass the language parser for syntax tree creation. Metrics defined at the Class Level: Lack of Cohesion of Methods (LCOM) No. of Public Variables No. of Protected Variables No. of Private Variables No. of Public Overridden Variables No. of Protected Overridden Variables No. of Children (NOC) Depth of Inheritance Tree of a class (DIT) No. of Public Methods No. of Private Methods No. of Public Overridden Methods No. of Protected Overridden Methods Weighted Methods per Class (WMC) Metrics defined at the Method Level:
No of Parameters Cyclomatic Complexity (CC) No of Variables No of Overloads
4. IMPLEMENTATION In this section, we describe our tool’s (SQAT) simple but intuitive graphical user interface with a focus on its main features. The top display form is shown in Figure 4. Its three main features: Analyze, Configure and Display are activated by clicking the respective buttons in the ribbon toolbar. The user interface also contains Help, Exit, Reload and Export buttons.
When the Configure button is clicked, SQAT brings up a configure screen (Figure 7) for excluding the files and the file extensions during
DOI: 10.1145/2507288.2507312
Figure 6. (A) Main Display of Code Metrics Tool. (B) An Enlarged Image of the Main Toolbar. (C) An Enlarged Image of the Result Summary View of Code Metrics
http://doi.acm.org/10.1145/2507288.2507312
ACM SIGSOFT Software Engineering Notes
Page 4
parsing. Once saved, these settings will be used by the parser to ignore the files.
September 2013 Volume 38 Number 5
After highlighting a file in the browser window, all the source files for the solution file are read and a list is generated by the tool. SQAT then begins the process of analyzing those files at the class and method levels. Upon completion of the parsing or analyzing process, all the extracted metrics are shown in the Grid at the bottom of the Ribbon bar. Figure 9 shows the various metrics extracted at the class level. Clicking upon the plus sign next to the row (Class Level) displays the metrics value at the method level for that class (Figure 10).
In case, settings are changed after loading of the solution file, clicking upon the Reload button will re analyze the source files and generate the code metrics. Figure 7. Configure
Finally, the Main Display of the SQAT shown in Figure 4 has an Export button which exports all metrics data to Microsoft Excel. A spreadsheet is created and the metrics are displayed in a hierarchical grid form (Figure 11).
5. RESULTS AND DISCUSSION We analyzed iTextSharp (Core project), the source code of this project is freely available on the SourceForge site. This project is a porting of the iText open source Java library to the .NET platform written completely in C# language. iTextSharp is a library that allows the developer to generate PDF files on the fly. The project under consideration has 1591 classes and has 7994 methods under these classes. The results are shown in Figure 12. The correctness of the tool is confirmed by a manual check process wherein various classes of the source code are considered and walking through the code established the correctness of our tool. The tool yields correct results for all the applications developed with different .Net frameworks. The same data can be exported to excel using the export functionality provided via the ‘Export’ button on the tool below the Code Metrics group.
6. CONCLUSIONS
Figure 8. File Browser
Software metrics have an important indirect role in increasing the quality of software systems. Through these measurements, managers and developers can ensure that the developed product is within regulations. This paper presented a software metric tool which is
Figure 9. Metrics at Class Level
Figure 10. Metrics at Method Level When the Load button is clicked, SQAT brings up a browser window (Figure 8) for locating and selecting the solution file which contains the references to various project files which in turn refer the source code files to be analyzed.
DOI: 10.1145/2507288.2507312
specifically designed to cover the latest C# feature constructs. It takes the solution file for the C# application as an input to the system. It then loads all the projects files into memory. Project files are read one by one to load all the source code files in order to construct the syntax trees. Once the creation of the syntax trees are completed, it is ready for the analysis. Various code metrics are generated at the class and method levels. The code metrics generated via this tool are quite satisfactory. Moreover, users can save the data to either excel or word using the export functionality for further analysis and research purposes.
7. FUTURE WORK
http://doi.acm.org/10.1145/2507288.2507312
ACM SIGSOFT Software Engineering Notes
Page 5
September 2013 Volume 38 Number 5
Figure 11. Spreadsheet Showing the Metrics Results The metrics presented by the tool are by no means a complete set of object oriented metrics. However, the tool does provide the ability to include additional metrics. The results obtained using the tool can be compared with some of the best industry standard tools available for the C# language. This analysis can be used as a reference by software developers and managers for building a bug free, reliable and easy to maintain software applications written in C#. The tool currently works for the C# code files and can be easily extended to include various other object oriented languages of the Microsoft world, for example VB.Net. The tool can also be extended to include the range section, which is nothing but the critical and acceptable values of the various metrics. The tool can be quite easily extended with the algorithm for finding
DOI: 10.1145/2507288.2507312
duplicity in the source code for various types of clones (Type1, Type 2 and Type3). Another prominent application of this tool can be found in refactoring and optimization activities of the source code.
8. REFERENCES [1] S.Chidamber and C. Kemerer. “A Metrics Suite for ObjectOriented Design ”In procIEEE Transactions on Software Engineering, Vol. 20, No. 6, pp. 476-493, 1994.
http://doi.acm.org/10.1145/2507288.2507312
ACM SIGSOFT Software Engineering Notes
Page 6
September 2013 Volume 38 Number 5
Figure 12. Code Metrics Results for iTextSharp Open Source (Core Project) [2] http://cccc.sourceforge.net/. Accessed on 5/11/2010 [3] http://www.eclipse4you.com/?q=en /eclipse_plugins/analyst4j/. Accessed on 5/11/2010 [4] http://depfind.sourceforge.net/. Accessed on 6/11/2010 [5] http://jcsc.sourceforge.net/. Accessed on 10/11/2010 [6] http://www.spinellis.gr/sw/ckjm/. Ac-cessed on 10/11/2010 [7] J. Alghamdi, R. Rufai and S. Khan “OOMeter: A Software Quality Assurance Tool” In proc. Ninth European Conference on Software Maintenance and Reengineering (CSMR'05), pp.190-191, 2005. [8] R. Chatterjee, B. Ryder,“Complexity of Points-To Analysis of Java in the Presence of Exceptions”, In proc. IEEE Transaction on Software Eng.,Vol. 27, No. 6,pp. 481-512, 2001.
[10] http://www.scitools.com/ features/metrics.php. Accessed on 5/12/2010 [11] http://www.ndepend.com/. Accessed on 03/03/2013 [12] http://msdn.microsoft.com/en-us/library/bb385914.aspx Accessed on 23/03/2013 [13] Satwinder Singh, K. S. Kahlon. “Effectiveness of refactoring metrics model to identify smelly and error prone classes in open source software” In SIGSOFT Software Engineering Notes, Vol. 37, No. 2, April 2012 [14] Satwinder Singh, K. S. Kahlon.”Effectiveness of encapsulation and object-oriented metrics to refactor code and identify error prone classes using bad smells” In SIGSOFT Software Engineering Notes, Vol. 36, No. 5, Sept 2011.
[9] R.Lincke, J.Lundberg and L. Welf. “Comparing software metrics tools”,In proc. International Symposium on Soft-ware Testing and Analysis, pp. 131-142, 2008.
DOI: 10.1145/2507288.2507312
http://doi.acm.org/10.1145/2507288.2507312