Showing posts with label Code Metrics. Show all posts
Showing posts with label Code Metrics. Show all posts

Sunday, September 22, 2019

Static code analysis and source code complexity tool - Plato for Typescript

I was looking for a tool which can report code complexity for an Angular application. The application is written in Typescript and there are hardly any tools for it.

The first place to look was Wikipedia : https://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#JavaScript which has some tools for Javascript but none for Typescript.

Looking across the web : https://www.npmjs.com/search?q=keywords:complexity , I came across tools like Plato and Complexity Report based on EsComplex. I was quickly drawn to Plato for it nice graphical reports.

npm install -g plato

Still the problem was how to make Plato run on TypeScript. Yes there were people asking for it on the web but no answers.

Now I knew all the TS code of my angular application is compiled and put into main.js. So I applied Plato on the file directly. First thing I hit was a memory constraint put by node that it can use a maximum memory of around 1.5GB. So had to increase it using:

node --max_old_space_size=4096 ./node_modules/es6-plato/bin/plato -r -d report -e .eslintrc.json -t "MyApp" -x .json dist\MyApp\main.js.

It worked but resultant report was so big that beyond the basic summary, navigating it was causing browser to error out. Looking into it in notepad++, I found that it a lot of Angular internal code around Webpack and then a small amount of the application code.

So had to work out a better solution. Need to compile the individual TS files in to JS file and run plato only on them.

Luckily we have a tsc compiler for TS which will output the JS and then can run plato on it.
So first install:

npm install -g typescript

Now we can run the build task from VS Code directly using CTRL+SHIFT+B i.e. Run Build Task and select tsc:build

This will execute the command:

node_modules\.bin\tsc.cmd -p tsconfig.json

Depending on your tsconfig.json, your output will normally be at :

"outDir": "./dist/out-tsc",

Now when I look at ./dist/out-tsc/src/app, I see all by TS files compiled and resulting in to JS and JS.MAP files.

Now I can run plato and it does not even hit the 1.5 GB memory as it has smaller files to look into :

plato -r -d report -e .eslintrc.json -t "MyApp" -x .json dist/out-tsc/src

and see some wonderful results:



Friday, April 10, 2009

Unit Testing, Code Analysis and Code Metrics in VS.NET 2008


In .NET we prefer to use VS .NET to do unit testing.Both VS 2005 and VS 2008 Team Editions support unit testing.
Way to do :

  1. Add a unit test project to your solution.
    Navigate to the method that needs to be tested. Do a right click : Create Unit Test on the method.



  • In the Wizard provided:Click on Setting. As of now MS does not support prepending Namespace before the class. This is of great value to us as need the namespace to distinguish similar class names in multiple namespaces. So we manual prepend the namespace i.e. 'Manager_' in the current case.



  • A dummy test case gets created:Modify the expected value to match whats expected. We can add more 'Asserts' to improve the test case.




  • To execute the test case, do a right click on the test case and select 'Run Tests':




  • The test will be executed and the run status displayer. We are re-run the test case or we can debug it with the appropriate buttons selected on the top:




  • To view the code coverage results, right click on the test case and select 'Code Coverage Results'.




  • The result will contain all the project which got invoked to make the test run:




  • Details of the method tested:






  • Similarly we can do a Code Analysis based on Coding Standards and generate Metrics

    Coding Standards:




    Coding Metrics: