Jan 9 2020 Code quality metrics By Ivelin
Jan 9, 2020 Code quality metrics By Ivelin Pavlov, PHP Developer Object systems international
ABOUT ME WEB DEVELOPER SINCE 2008 STARTED AT OSI BACK IN 2016 AS A BACK-END WEB DEVELOPER I LOVE HELPING OTHERS, CONTRIBUTING TO OPEN SOURCE PROJECTS, READING NEWSIEST TRENDS AND EXPERIMENTING WITH NEW TOOLS. OUR COMPANY FINDS THE BENEFIT IN THAT AND LETS US GROW NOW I TRY TO SHARE KNOWLEDGE ABOUT BEST PRACTICES IN DEVELOPMENT BY MENTORING HTTPS: //WWW. LINKEDIN. COM/IN/IBPAVLOV/ HTTPS: //GITHUB. COM/IBPAVLOV Object systems international
CODE QUALITY METRICS QUANTATIVE METRICS STATIC ANALYTICS TOOL WE USE ATD EVOOPS SCHEDULED OR CI/CD TACTICS Object systems international
WHY MEASURE ANYTHING? CAN WE TRUST OUR CODE? WE DON’T WRITE BAD CODE CONSCIOUSLY. Object systems international
BAD CODE GETS TO PRODUCTION SOMEHOW LACK OF QUALITY MISSING STANDARDS, FORGET ABOUT TESTS OR POOR CODE REVIEWS MESSY CODE (MAYBE LEGACY) LOTS OF DEPENDENCIES. Object systems international
DO SOMETHING ABOUT IT OR JUST PROVE THAT YOUR CODE IS FLAWLESS AND BRAG ABOUT IT. Object systems international
HOW TO MEASURE CODE QUALITY • CODE LINTERS • BUGS PER HOUR • WTF PER MINUTE • TEST COVERAGE Object systems international
NOT REALLY AN EMPIRICAL SCIENCE Object systems international
HOW TO MEASURE CODE QUALITY. • HALSTEAD COMPLEXITY MEASURES – COGNITIVE ABILITY TO READ CODE • LCOM - LACK OF COHESION – DOES MY CLASS DO ONLY ONE THING • CYC - CYCLOMATIC COMPLEXITY – HOW MANY TESTS DO I WRITE • MAINTAINABILITY INDEX – HOW EASY TO SUPPORT AND CHANGE YOUR CODE IS Object systems international
HALSTEAD COMPLEXITY MEASURES Object systems international
HALSTEAD COMPLEXITY MEASURES Object systems international
HALSTEAD COMPLEXITY MEASURES Object systems international
HALSTEAD COMPLEXITY MEASURES Object systems international
HALSTEAD COMPLEXITY MEASURES the number of distinct operators = 18 the number of distinct operands = 10 the total number of operators = 32 the total number of operands = 17 Object systems international
HALSTEAD COMPLEXITY MEASURES The number of distinct operators = n 1 = 18 The number of distinct operands = n 2 = 10 The total number of operators = N 1 = 32 The total number of operands = N 2 = 17 Program vocabulary: n = n 1 + n 2 Program length: N = N 1 + N 2 Calculated program length: N’=n 1*log 2(n 1)+n 2*log 2(n 2) Volume: V= N * log 2(n) Difficulty: D= (n 1/2) * (N 2/n 2) Effort: E= DV Time required to program: T= E/18 seconds Number of delivered bugs: B=V/3000 = 28 = 49 = 75 = 236 = 15. 30 = 3604 = 200 = 0. 08 Object systems international Prune to errors (Better smaller) The effort to implement or understand The time to implement or understand That many errors should be found during testin
HALSTEAD COMPLEXITY MEASURES Screenshot from our php. Metrics report for project MMT Object systems international
LCOM 4 - LACK OF COHESION (A) 1 (C) 2 (B) 3 (A) could be considered cohesive (B) shows an extreme case of a non-cohesive class , and (C) is non-cohesive. "connected components" in a class Object systems international
LCOM - LACK OF COHESION • Object systems international
LCOM 2 - LACK OF COHESION (A) 0. 33 (B) 0. 66 Shows low cohesion, but not real independent methods Object systems international (C) 0. 33
LCOM - LACK OF COHESION Object systems international
Object systems international
height grow. Tree $branches place. Ornaments Object systems international $ornaments
INCLUDE COHESION Object systems international
SEPARATE THE CLASS Object systems international
Object systems international
Lower Complexity = Better Code Bigger CYC = More Unit tests Object systems international
CYC - CYCLOMATIC COMPLEXITY EXAMPLE 1) 2) 3) 4) 2 a 3 1 2 b Object systems international 4
CYC - CYCLOMATIC COMPLEXITY EXAMPLE 1 2 3 4 Object systems international
CYC - CYCLOMATIC COMPLEXITY EXAMPLE 1 2 3 4 Object systems international E = 4 //Edges N = 4 //Nodes P = 1 //Paths
CYC - CYCLOMATIC COMPLEXITY EXAMPLE 1 2 CYC = area blocks + 1 = 2 3 4 Object systems international
CYC - CYCLOMATIC COMPLEXITY EXAMPLE 1 2 3 4 Object systems international
, 1]; Object systems international
, 1]; 1 a 2 b finish 1 b 2 a 1 2 3 a 3 b 3 Object systems international
CYC - MCCABE'S CYCLOMATIC COMPLEXITY • Object systems international
MAINTAINABILITY INDEX V = Halstead Volume G = Cyclomatic Complexity LOC = count of source Lines Of Code (SLOC) CM = percent of lines of Comment (optional) grow. Tree(int $grow. Height, int $grow. Branches)): void 98 place. Ornaments(int $branch. Position): void 75 print(): void 40 The original formula: MI = 171 - 5. 2 * ln(V) - 0. 23 * (G) - 16. 2 * ln(LOC) The derivative used by SEI is calculated as follows: MI = 171 - 5. 2 * log 2(V) - 0. 23 * G - 16. 2 * log 2 (LOC) + 50 * sin (sqrt(2. 4 * CM)) The derivative used by Microsoft Visual Studio (since v 2008) is calculated as follows: MI = MAX(0, (171 - 5. 2 * ln(Halstead Volume) - 0. 23 * (Cyclomatic Complexity) - 16. 2 * ln(Lines of Code))*100 / 171) Function over size of an algorithm implementation, linearly independent paths through a program's source code and Lines of code Object systems international
STATIC ANALYTICS TOOLS OUR USAGE Object systems international
Legacy project report from 2019. 01 Object systems international
Object systems international
+5005 +51 -0. 13 -2. 11 -975 Legacy project from 2019. 12. 01 Object systems international
Object systems international
Object systems international
Enspiri from 2019. 12. 01 Object systems international
Enspiri from 2019. 12. 01 Object systems international
Enspiri from 2019. 12. 01 Object systems international
Enspiri from 2019. 12. 01 Object systems international
Enspiri from 2019. 12. 01 Object systems international
STATIC ANALYTICS TOOLS FOR OTHER LANGUAGES? Object systems international
STATIC ANALYTICS TOOLS Programming Language Name and link of tool PHPMetrics: https: //github. com/phpmetrics/Php. Metrics Java Jtest: https: //www. parasoft. com/jtest/reporting ? JArchitect: https: //www. jarchitect. com/ C#/. NET Code analyzer from Visual Studio 2012+ Ndepend https: //www. ndepend. com/ Java. Script Deepscan: https: //deepscan. io/ Jshint: https: //jshint. com C++ http: //frama-c. com/ Multi: Sonar. Qube: https: //www. sonarqube. org/features/issues-tracking/ Scrutinizer: https: //scrutinizer-ci. com/ Object systems international
PHPMETRICS • STATIC ANALYSIS TOOL FOR PHP • GENERATES HTML REPORT Object systems international
INTEGRATION • PHP PROJECT • GITLAB CI/CD WORKFLOW • JOB PHPMETRICS • GENERATE HTML REPORT • SEND REPORT TO: NGINX WEBSITE FOLDER FOR REVIEW • SCHEDULE: Object systems international
Scheduled or CI/CD tactics gitlab. org. com Web. objectsystems. com Docker nginx-proxy Docker nginx Project pages projectname Pages. org. com Branch master development /: project/: branch/: job Job (CI/CD) deploy phpmetrics Schedule On change Monthly Deploy using ansible Generate HTML report and send to Pages server Object systems international
RUN PHPMETRICS Object Systems International
REPORT FOR PROJECT MMT REST-API FROM 2019 -11 -01 Object systems international
RESOURCES • HALSTEAD COMPLEXITY MEASURE DEFINITION - HTTPS: //EN. WIKIPEDIA. ORG/WIKIH / ALSTEAD_COMPLEXITY_MEASURES • HALSTEAD CALCULATOR - HTTP: //GROUPS. UMD. UMICH. EDU/CIS/TINYTOOLS/CIS 375/F 18/TEAM 3 -HALSTEADCALCULATOR/INDEX. HTML • / YCLOMATIC_COMPLEXITY CYCLOMATIC COMPLEXITY - HTTPS: //EN. WIKIPEDIA. ORG/WIKIC • MAINTAINABILITY INDEX - HTTP: //WWW. PROJECTCODEMETER. COM/COST_ESTIMATION/HELPGL / _MAINTAINABILITY. HTM • PHPMETRICS - HTTPS: //PHPMETRICS. ORG/ Object systems international
QUESTIONS? Object systems international
THANK YOU Object systems international
- Slides: 56