Code analysis

Plan for today:

  1. Follow up on last homework exercise
  2. Static analysis tools and code review
  3. Performance measurement and profiling

Learning outcomes

After this module you should be able to:

Relevance

It goes without saying that the quality of your code is an important aspect of your professional work. Besides fulfilling the functional requirements, you should be able to write code that is up to professional standards.

Resources

Static tools

Performance testing

Example code

Sample code full of bad style.

Exercises

Short exercises

Performance testing

We are going to find the bottleneck in the mergesort algorithms in this small suite of implementation attemps by Kasper.

Looking for the bottleneck…

Static analysis

Next, lets take a look at the PMD tool from above.

  1. download it to anywhere on your machine
  2. try it out on the code in the the basic algorithm project.The following java checks can be relevant to try:
    • -R rulesets/java/optimizations.xml
    • -R rulesets/java/naming.xml
    • -R rulesets/java/coupling.xml
    • -R rulesets/java/controversial.xml

For me to get the tool running, I just needed to download it and unzip it. I was then able to run it in command line. I cd to the directory where I unzipped it, and down into its bin directory: /Users/kasper/Downloads/pmd-bin-5.8.1/bin
From there I issued commands like:
./run.sh pmd -d ~/nearlyEmpty/MergeSortTrouble/src/main/java/dk/cphbusiness/soft/mergesorttrouble -R rulesets/java/controversial.xml -f html > ~/pmdresult.html
which gives me a html file I can load into the browser.

For next week

You should try to use one of the tools above on a larger example. If you have a project lying around from earlier projects, feel free to use that.

If not, this project on github, GraphQL for JPA has a suited level of complexity, and it is not quite clear if it well done, or just functional.

Exercise 1.

Pick one of the tools from the above list, and run it on your chosen project.

As there are 100s of different metrics, you have to argue for which 10 you pick. In particular what situation would make those you pick particular relevant.

Exercise 2

You can again pick something from a project you have done yourself, or just pick something from the GraphQL sample.

The exercise is to locate a library call which takes up most of the overall execution time for a user story.

If you do not have something of your own, I suggest you look into the the “find droid by name” test.

Notice, it calls `executor.execute(query).data - which is the thing we should see where its bottlenecks are hidden.