Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Measuring the Quality of Your Perl Code

282 views

Published on

Some thoughts on ways to measure the quality of Perl code (and, hence, get a basis for improving it)

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Measuring the Quality of Your Perl Code

  1. 1. August 2019 Dave Cross / @davorg Measuring the Quality of Your Perl Code
  2. 2. 01 Why? AGENDA 03 Why would I want to do this? How? 02 What? 04 Summarise How can I do this? What shall I measure? Showing off your work
  3. 3. Why?
  4. 4. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “You can’t manage what you can’t measure” Peter Drucker Says 4
  5. 5. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code In order to improve something you need to have a baseline to measure your improvements against Measure THEN Improve 5
  6. 6. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “I made that database query faster” vs “I made that database query 25% faster” Measure THEN Improve 6
  7. 7. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Quantify your improvements Measure THEN Improve 7
  8. 8. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code 1. Measure 2. Improve 3. Measure again 4. (Repeat) Three Step Process 8
  9. 9. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code A set of values you can track over time to demonstrate improvements Measure THEN Improve 9
  10. 10. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code (Managers love that stuff) Measure THEN Improve 10
  11. 11. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Measure THEN Improve 11
  12. 12. What?
  13. 13. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code We need dates Measuring Code Quality 13
  14. 14. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code We need numbers Measuring Code Quality 14
  15. 15. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Dates are easy Measuring Code Quality 15
  16. 16. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code What numbers can we use? Measuring Code Quality 16
  17. 17. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code There are no numbers that directly and accurately measure the quality of code Measuring Code Quality 17
  18. 18. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code We need to use proxies Measuring Code Quality 18
  19. 19. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “In statistics, a proxy or proxy variable is a variable that is not in itself directly relevant, but that serves in place of an unobservable or immeasurable variable.” [Wikipedia: Proxy (statistics)] Measuring Code Quality 19
  20. 20. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “In order for a variable to be a good proxy, it must have a close correlation, not necessarily linear, with the variable of interest. This correlation might be either positive or negative.” [Wikipedia: Proxy (statistics)] Measuring Code Quality 20
  21. 21. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ● Test coverage ● Perl critic ● Cyclomatic complexity Four Proxies 21
  22. 22. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Code that passes its test suite is presumably better than code that doesn't Test Passes 22
  23. 23. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Code with a test suite that covers more of the code is likely to be better code Test Coverage 23
  24. 24. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Code that conforms to an arbitrary set of “best practices” is better than code that doesn't Perl Critic 24
  25. 25. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Simple code is likely to contain fewer bugs than complex code Cyclomatic Complexity 25
  26. 26. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Measure of the complexity of a piece of code Cyclomatic Complexity 26
  27. 27. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Count the branching points in the code Cyclomatic Complexity 27
  28. 28. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Perl::Metrics::Simple Perl Advent 8th Dec 2014 Cyclomatic Complexity 28
  29. 29. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Cyclomatic Complexity 29
  30. 30. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code I know that all of these measurements are potentially contentious Four Proxies 30
  31. 31. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code That's why they are proxies Four Proxies 31
  32. 32. How?
  33. 33. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code We would like to measure each of our proxy values on a regular basis Measuring Our Proxies 33
  34. 34. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Perhaps weekly? Measuring Our Proxies 34
  35. 35. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Perhaps daily? Measuring Our Proxies 35
  36. 36. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Perhaps on every commit? Measuring Our Proxies 36
  37. 37. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Measure automatically Measuring Our Proxies 37
  38. 38. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Sounds like a job for Continuous Integration Measuring Our Proxies 38
  39. 39. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code A continuous integration system monitors your source code repository and takes various actions in response to various events Continuous Integration 39
  40. 40. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Jenkins ● Travis CI ● Circle CI Continuous Integration 40
  41. 41. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “When code is committed to any branch, run this task” Continuous Integration 41
  42. 42. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “Every Sunday at 2am, run this task” Continuous Integration 42
  43. 43. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code “When code is merged to the release branch run these tasks” Continuous Integration 43
  44. 44. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Where “task” means “anything you can write a script for” Continuous Integration 44
  45. 45. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Run tests Continuous Integration Tasks 45
  46. 46. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Email someone Continuous Integration Tasks 46
  47. 47. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Build a new Docker image, deploy that to a staging environment, run some tests and, if they all pass, email someone asking them to approve a release to production Continuous Integration Tasks 47
  48. 48. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code So it's perfect for our requirements Continuous Integration 48
  49. 49. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Every time code is committed, run a series of scripts that measures our proxies and records the values in a database against today's date Continuous Integration 49
  50. 50. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code And plot a graph Continuous Integration 50
  51. 51. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code A simple matter of programming Continuous Integration 51
  52. 52. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Continuous Integration 52
  53. 53. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Someone has already written it Continuous Integration 53
  54. 54. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Travis CI example Continuous Integration 54
  55. 55. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Travis CI example (Other CI systems are available) Continuous Integration 55
  56. 56. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Sign up with Github account Travis CI 56
  57. 57. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Add repos Travis CI 57
  58. 58. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Add .travis.yml to your repo Travis CI 58
  59. 59. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code language: perl perl: - "5.12" - "5.14" - "5.16" - "5.18" - "5.20" - "5.22" - "5.24" - "5.26" - "5.28" Travis CI 59
  60. 60. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Travis CI 60
  61. 61. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ● Test coverage ● Perl critic ● Cyclomatic complexity Four Proxies 61
  62. 62. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ● Perl critic ● Cyclomatic complexity Four Proxies 62
  63. 63. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code But it gets better Travis CI 63
  64. 64. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code coveralls.io Coveralls 64
  65. 65. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Reports on test coverage Coveralls 65
  66. 66. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Sign up with Github account Coveralls 66
  67. 67. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Add repos Coveralls 67
  68. 68. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Add to .travis.yml Coveralls 68
  69. 69. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code before_install: cpanm -n Devel::Cover::Report::Coveralls script: perl Makefile.PL && make && cover -test -report coveralls Coveralls 69
  70. 70. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Coveralls 70
  71. 71. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ● Perl critic ● Cyclomatic complexity Four Proxies 71
  72. 72. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ✓ ● Perl critic ● Cyclomatic complexity Four Proxies 72
  73. 73. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code kritika.io Kritika 73
  74. 74. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Sign up with Github account Kritika 74
  75. 75. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Add repos Kritika 75
  76. 76. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Kritika 76
  77. 77. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Kritika 77
  78. 78. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Kritika 78
  79. 79. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ✓ ● Perl critic ● Cyclomatic complexity Four Proxies 79
  80. 80. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ✓ ● Perl critic ✓ ● Cyclomatic complexity Four Proxies 80
  81. 81. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ✓ ● Perl critic ✓ ● Cyclomatic complexity ✓ Four Proxies 81
  82. 82. Summarise
  83. 83. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Finding good ways to display your success Summarise 83
  84. 84. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Everyone likes badges Summarise 84
  85. 85. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Summarise 85
  86. 86. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code All of these services generate badges Summarise 86
  87. 87. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Travis CI 87
  88. 88. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Coveralls 88
  89. 89. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Kritika 89
  90. 90. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code People sometimes put these badges on CPAN pages Summarise 90
  91. 91. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code CPAN 91
  92. 92. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code I wanted a status page for all of my CPAN modules Summarise 92
  93. 93. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Dashboard 93
  94. 94. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Code is on Github https://github.com/davorg/dashboard Summarise 94
  95. 95. In conclusion
  96. 96. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code To quantify your improvements Why? 96
  97. 97. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Find proxy measures What? 97
  98. 98. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code ● Test passes ✓ ● Test coverage ✓ ● Perl critic ✓ ● Cyclomatic complexity ✓ Four Proxies 98
  99. 99. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Use continuous integration to automate the process How? 99
  100. 100. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Display badges Summarise 10 0
  101. 101. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code Use my dashboard Summarise 10 1
  102. 102. © Equals Group Plc 2019 Measuring the Quality of Your Perl Code (Actually improving your code is left as an exercise for the reader) Making Improvements 10 2
  103. 103. Thanks Any questions? Dave Cross @davorg

×