main (18K)

Overview

Code Analyzer can be used to count lines of code (LoC) at the file level or at the function level. When counting at the function level, it also computes the McCabe complexity & the level of nesting inside the function.

Counting at file level

When used for this purpose, it gives the number of code lines, code & comment lines, commment lines and blank lines in the file. It supports the following file types:

  • Java
  • C++
  • VB
  • SQL
  • HTML
  • JSP

When counting for HTML or JSP files, it will count LoC correctly for javascript and vbscript code embedded within the <script> tag.

Counting can be done for a single file or a directory (with the option of recursively counting within sub-directories). The values are written to a .csv file so that it can be opened and analyzed using Excel.

This functionality is invoked when the Count LoC button is pressed after selecting a file or directory.

Counting at function level

This can be done for the filetypes - C++, Java and VB. In additional to giving LoC at function level, it also gives the McCabe Complexity of the function & the nesting level within the function. For an explanation of McCabe complexity, please see the document on Structured Testing that can be downloaded from this page.

Function level counting is invoked when the Count Detail button is pressed after selecting a file or directory.

Using the generated info

LoC is one of the measures of software size. It is often debated as a size measure because:

  • one person can implement a given functionality very optimally (in terms of size) and the other person may take more lines of code to implement the same.
  • even if the algorithm is the same, the code can be written such that it occupies more lines since arbitrary amount of whitespace can be inserted between tokens.
  • LoC is programming language dependent.

While this is true, I assume an organization will have coding standards and there will be code reviews, which will lessen the impact of the above anomalies. Hence, it is still a reasonable measure of size across projects implemented in a given language within the same organization.

Often I have seen management judging the quality of software based on bugs reported in a release. Modules are compared based on reported bugs, often ignoring their relative size. Defects per KLOC gives a more reasonable picture in this scenario. Also, KLOC can be used to measure individual productivity, especially when its fresh software development (not bug fixing or enhancements to existing software).

McCabe complexity is a good measure of how complex the software is and the number of tests required to validate the correctness of the software. For greater software reliability, it is desirable to limit this complexity. So if you have lengthy, highly complex functions that are frequently being touched to fix bugs, consider refactoring it to reduce the complexity.

Known tool limitations

  1. This tool does not do any pre-processing for C++ because of which macros are not expanded. Macros can sometimes embrace a huge amount of code including conditional logic and can be used to mask the inherent complexity of a function.
  2. With macros unexpanded, C++ code can look very different - sometimes even appear syntactically incorrect. Therefore, it is possible that under extreme conditions, this tool may generate a parse error and terminate processing for the file.
  3. Count LoC and Count Detail use different logic internally, so if you compare the line counts for a file generated by the two options, it may be a little different. The reason for this is not readily apparent to me since for small test files, the results invariably match.
  4. Consider the following piece of logic:
    			if (condition1) {
    				...
    			} else if (condition2) {
    				...
    			} else {
    				...
    			}
    			
    Nesting for the above piece of code will be 2 (and not 1). This is because the 2nd if is considered as nested within the else clause (and hence its contents are at nesting level 2).

Other info

Type of a file (whether Java or C++ etc.) is identified based on file extensions. Use the Config button to change the file extensions, if you are not ok with the defaults.

Feedback about the tool are most welcome - pl write to me.

1