SlideShare a Scribd company logo
1 of 27
Download to read offline
Code metrics in PHP
From lines to code semantic
● Julio Martinez
● Developing PHP since 2001
● 1.5 years working at Ulabox
● Find me: @liopic
Code metrics in PHP: 0. Introduction
Who am I?
● El antisúper!
● 6-years-old startup
● 11 developers
● monolith & new services
● #rigor
● We are hiring!
Code metrics in PHP: 0. Introduction
What is Ulabox?
● Evaluate quality!
● We need objective, reproducible and quantifiable metrics
Could you tell me some examples of metrics?
Code metrics in PHP: 1. Looking for quality
Why do we need software metrics?
● number of bugfixes per month
● lines of code
● test coverage
● number of user stories covered
● follows clean code’s rules
● documentation lines / total of code lines
● etc
Code metrics in PHP: 1. Looking for quality
Some examples?
● Testing first!
● Code “surface”
● Lines grouping
● Code semantic
Disclaimer: I’ll discuss locally-executable tools (non SaaS)
● SaaS: Insight, Code climate, Scrutinizer, SonarQube...
Code metrics in PHP: 1. Looking for quality
Let’s start our knowledge journey...
● User stories: behat
● General testing: phpunit
○ Code coverage
○ Mutant testing (=test your tests): humbug
● Unit/spec testing: phpspec
Code metrics in PHP. Testing
Testing First!
● Code sniffer (code style, PSR2): phpcs
● Copy+paste detector (DRY): phpcpd
● Clean code & common smells:
○ phpmd (“mess detector”, a bit old)
○ Exakat (it’s SaaS but has trial download)
Code metrics in PHP: 3. Code surface
Code “surface”
● Counting lines: phploc
● Getting ratios: pdepend
Code metrics in PHP: 4. Lines grouping
Lines grouping
1. Cyclomatic complexity (paths of execution)
2. Coupling metrics (relations between “modules”)
3. Lack of cohesion, LCOM (relations between methods)
4. Halstead’s metrics (operands and operators)
5. Maintainability Index
Code metrics in PHP: 5. Code semantic
Code semantic analysis
Code metrics in PHP: 5. Code semantic
Cyclomatic complexity
abstract class BaseBird
{
private $eggs = 0;
public function makeEgg()
{
$this->eggs++;
}
public function crackEgg()
{
if ($this->eggs <=0) {
throw new NoEggsException();
}
$this->eggs--;
}
public function fly()
{
return 'flap-flap';
}
abstract public function sound();
}
class Duck extends BaseBird
{
public function sound()
{
return 'quack';
}
public function swim()
{
return 'splash';
}
}
class Parrot extends BaseBird
{
public function sound()
{
$friend = new Duck();
if($friend->sound()){
return 'Err '.$friend->sound();
}
return '';
}
}
Code metrics in PHP: 5. Code semantic
Cyclomatic complexity
abstract class BaseBird
{
private $eggs = 0;
public function makeEgg()
{
$this->eggs++;
}
public function crackEgg()
{
if ($this->eggs <=0) {
throw new NoEggsException();
}
$this->eggs--;
}
public function fly()
{
return 'flap-flap';
}
abstract public function sound();
}
class Duck extends BaseBird
{
public function sound()
{
return 'quack';
}
public function swim()
{
return 'splash';
}
}
class Parrot extends BaseBird
{
public function sound()
{
$friend = new Duck();
if($friend->sound()){
return 'Err '.$friend->sound();
}
return '';
}
}
2
2
1
● Afferent couplings (Ca) - “they use you”
● Efferent couplings (Ce) - “you use them”
● Instability, resilience to change (I): I = Ce / (Ce + Ca)
○ I=0 is a completely stable package
○ I=1 is a completely unstable package
Code metrics in PHP: 5. Code semantic
Coupling metrics
● Abstractness (A): ratio of abstract classes
○ A=0 is a completely concrete package
○ A=1 is a completely abstract package
● Examples:
○ BaseBird: Ce=1 (uses NoEggsException), Ca=0; A=1, I=1
○ Parrot, Ce=1 (uses Duck), Ca=0; A=0, I=1
○ Duck, Ce=0, Ca=0; A=0, I=0
Code metrics in PHP: 5. Code semantic
Coupling metrics
● Distance from the main sequence (D): D = |A+I-1|
○ Balance between abstractness and stability
○ Ideal packages: (I=1, A=0), (I=0, A=1)
Code metrics in PHP: 5. Code semantic
Coupling metrics: main sequence
● Groups of methods
● LCOM = 1 is ideal
Code metrics in PHP: 5. Code semantic
Lack of cohesion of methods (LCOM)
abstract class BaseBird
{
private $eggs = 0;
public function makeEgg()
{
$this->eggs++;
}
public function crackEgg()
{
if ($this->eggs <=0) {
throw new NoEggsException();
}
$this->eggs--;
}
public function fly()
{
return 'flap-flap';
}
abstract public function sound();
}
● Groups of methods
● LCOM = 1 is ideal
● BaseBird LCOM = 3
Code metrics in PHP: 5. Code semantic
Lack of cohesion of methods (LCOM)
abstract class BaseBird
{
private $eggs = 0;
public function makeEgg()
{
$this->eggs++;
}
public function crackEgg()
{
if ($this->eggs <=0) {
throw new NoEggsException();
}
$this->eggs--;
}
public function fly()
{
return 'flap-flap';
}
abstract public function sound();
}
● η1
= number of distinct operators
● η2
= number of distinct operands
● N1
= the total number of operators
● N2
= the total number of operands
● Program vocabulary: η = η1
+ η2
● Program length: N = N1
+ N2
Code metrics in PHP: 5. Code semantic
Halstead complexity measures (1977)
● Volume: V = N × log2
η (linearly with length, log with vocabulary)
● Difficulty : D = η1
/2 × N2
/η2
(half distinct operators, scarcity of operands)
● Effort: E = D × V
● Time required to program: T = E/18 seconds
● Delivered bugs:
Code metrics in PHP: 5. Code semantic
Halstead complexity measures
● University of Idaho, Oman and Hagemeister
● Volume (V), Cyclomatic Complexity (G), Lines of Code (LOC)
● Original MI = 171 - 5.2*ln(V) - 0.23*G - 16.2*ln(LOC)
Code metrics in PHP: 5. Code semantic
Maintainability Index (1991)
● phpmetrics
○ Let’s see all those indexes!
Code metrics in PHP: 5. Code semantic
Coupling + LCOM + Halstead + MI
Maintainability.................... 65.83 / 100
Accessibility for new developers... 41.03 / 100
Simplicity of algorithms........... 42.57 / 100
Volume............................. 64.90 / 100
Reducing bug's probability......... 65.57 / 100
● MUST!
○ phpcs - code sniffer - No extra effort need; always
○ phpunit - integration & unit tests - Write tests; always
● Adopt
○ phpspec - unit/specs - Write tests; always
○ behat - user stories (functional tests) - Write user stories; always
○ phpcpd - copy & paste - No effort; always
○ phpmetrics - No effort; main indexes weekly, others when refactor
● Give a try
○ humbug - test your tests - No effort; often
○ SaaS options - Customization effort; always
Code metrics in PHP: 6. Summing up!
RADAR of tools (how? when?)
Code metrics in PHP: 6. Summing up!
Questions?
Code metrics in PHP: 6. Summing up!
Thank you!

More Related Content

What's hot

10.2 Manage Communication
10.2 Manage Communication10.2 Manage Communication
10.2 Manage CommunicationDavidMcLachlan1
 
Farmers buddy ppt
Farmers buddy pptFarmers buddy ppt
Farmers buddy pptshindept123
 
Chap 8.1 Plan quality management
Chap 8.1 Plan quality managementChap 8.1 Plan quality management
Chap 8.1 Plan quality managementAnand Bobade
 
Blood donation ppt
Blood donation pptBlood donation ppt
Blood donation pptR prasad
 
Blood Bank and Donor Management System (PPT).pptx
Blood Bank and Donor Management System (PPT).pptxBlood Bank and Donor Management System (PPT).pptx
Blood Bank and Donor Management System (PPT).pptxNileshPatil90378
 
Effort estimation( software Engineering)
Effort estimation( software Engineering)Effort estimation( software Engineering)
Effort estimation( software Engineering)kiran Patel
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costslalithambiga kamaraj
 
SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... GCWUF
 
Software project management introduction
Software project management introductionSoftware project management introduction
Software project management introductionKanchana Devi
 
Business analysis ppt
Business analysis ppt Business analysis ppt
Business analysis ppt Babasab Patil
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageAniruddha Chakrabarti
 
Chap 4.7 Project or Phase Close
Chap 4.7 Project or Phase CloseChap 4.7 Project or Phase Close
Chap 4.7 Project or Phase CloseAnand Bobade
 
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...Techtic Solutions
 
Software project management
Software project managementSoftware project management
Software project managementR A Akerkar
 
hospital management System
hospital management Systemhospital management System
hospital management Systemsabin kafle
 

What's hot (20)

Go lang
Go langGo lang
Go lang
 
10.2 Manage Communication
10.2 Manage Communication10.2 Manage Communication
10.2 Manage Communication
 
Farmers buddy ppt
Farmers buddy pptFarmers buddy ppt
Farmers buddy ppt
 
Chap 8.1 Plan quality management
Chap 8.1 Plan quality managementChap 8.1 Plan quality management
Chap 8.1 Plan quality management
 
Project Charter in SPM
Project Charter in SPMProject Charter in SPM
Project Charter in SPM
 
Blood donation ppt
Blood donation pptBlood donation ppt
Blood donation ppt
 
Blood Bank and Donor Management System (PPT).pptx
Blood Bank and Donor Management System (PPT).pptxBlood Bank and Donor Management System (PPT).pptx
Blood Bank and Donor Management System (PPT).pptx
 
Effort estimation( software Engineering)
Effort estimation( software Engineering)Effort estimation( software Engineering)
Effort estimation( software Engineering)
 
Estimating Software Maintenance Costs
Estimating Software Maintenance CostsEstimating Software Maintenance Costs
Estimating Software Maintenance Costs
 
SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system... SRS on Online Blood Bank Managment system...
SRS on Online Blood Bank Managment system...
 
Software project management introduction
Software project management introductionSoftware project management introduction
Software project management introduction
 
Business analysis ppt
Business analysis ppt Business analysis ppt
Business analysis ppt
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
Chap 4.7 Project or Phase Close
Chap 4.7 Project or Phase CloseChap 4.7 Project or Phase Close
Chap 4.7 Project or Phase Close
 
Library management system using java technology
Library management system using java technologyLibrary management system using java technology
Library management system using java technology
 
PMP_Project Communication Management
PMP_Project Communication ManagementPMP_Project Communication Management
PMP_Project Communication Management
 
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...
6 Best Project Management Tools Comparison: Jira vs. Trello vs. MS Project vs...
 
Software project management
Software project managementSoftware project management
Software project management
 
Software Quality Metrics
Software Quality MetricsSoftware Quality Metrics
Software Quality Metrics
 
hospital management System
hospital management Systemhospital management System
hospital management System
 

Similar to Code metrics in PHP

Measuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedMeasuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedDennis de Greef
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
 
Joomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingJoomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingShyam Sunder Verma
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error predictionNIKHIL NAWATHE
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8Wim Godden
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetupsource{d}
 
The why and how of moving to php 7
The why and how of moving to php 7The why and how of moving to php 7
The why and how of moving to php 7Wim Godden
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6Wim Godden
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerAndrey Karpov
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityGeorgePeterBanyard
 
Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationTim Burks
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Insidejeffz
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonRalf Gommers
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...Towards Developing a Repository of Logical Errors Observed in Parallel Code t...
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...Ritu Arora
 
Php training100%placement-in-mumbai
Php training100%placement-in-mumbaiPhp training100%placement-in-mumbai
Php training100%placement-in-mumbaivibrantuser
 

Similar to Code metrics in PHP (20)

Measuring maintainability; software metrics explained
Measuring maintainability; software metrics explainedMeasuring maintainability; software metrics explained
Measuring maintainability; software metrics explained
 
More about PHP
More about PHPMore about PHP
More about PHP
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
Joomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation TestingJoomla Code Quality Control and Automation Testing
Joomla Code Quality Control and Automation Testing
 
Code Analysis-run time error prediction
Code Analysis-run time error predictionCode Analysis-run time error prediction
Code Analysis-run time error prediction
 
The why and how of moving to php 8
The why and how of moving to php 8The why and how of moving to php 8
The why and how of moving to php 8
 
Machine Learning on Code - SF meetup
Machine Learning on Code - SF meetupMachine Learning on Code - SF meetup
Machine Learning on Code - SF meetup
 
The why and how of moving to php 7
The why and how of moving to php 7The why and how of moving to php 7
The why and how of moving to php 7
 
Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
 
The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6The why and how of moving to PHP 5.5/5.6
The why and how of moving to PHP 5.5/5.6
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
 
PHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing InsanityPHP 8: Process & Fixing Insanity
PHP 8: Process & Fixing Insanity
 
Enforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code GenerationEnforcing API Design Rules for High Quality Code Generation
Enforcing API Design Rules for High Quality Code Generation
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
LINQ Inside
LINQ InsideLINQ Inside
LINQ Inside
 
Standardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for PythonStandardizing on a single N-dimensional array API for Python
Standardizing on a single N-dimensional array API for Python
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...Towards Developing a Repository of Logical Errors Observed in Parallel Code t...
Towards Developing a Repository of Logical Errors Observed in Parallel Code t...
 
Php training100%placement-in-mumbai
Php training100%placement-in-mumbaiPhp training100%placement-in-mumbai
Php training100%placement-in-mumbai
 

More from Julio Martinez

Customer segmentation scbcn17
Customer segmentation scbcn17Customer segmentation scbcn17
Customer segmentation scbcn17Julio Martinez
 
Remote working effectively
Remote working effectivelyRemote working effectively
Remote working effectivelyJulio Martinez
 
Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Julio Martinez
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to DockerJulio Martinez
 
Some OOP paradigms & SOLID
Some OOP paradigms & SOLIDSome OOP paradigms & SOLID
Some OOP paradigms & SOLIDJulio Martinez
 
Introduction to Clean Code
Introduction to Clean CodeIntroduction to Clean Code
Introduction to Clean CodeJulio Martinez
 
Professional development
Professional developmentProfessional development
Professional developmentJulio Martinez
 

More from Julio Martinez (7)

Customer segmentation scbcn17
Customer segmentation scbcn17Customer segmentation scbcn17
Customer segmentation scbcn17
 
Remote working effectively
Remote working effectivelyRemote working effectively
Remote working effectively
 
Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017Conclusion of the Seminary UPC 2017
Conclusion of the Seminary UPC 2017
 
Introduction to Docker
Introduction to DockerIntroduction to Docker
Introduction to Docker
 
Some OOP paradigms & SOLID
Some OOP paradigms & SOLIDSome OOP paradigms & SOLID
Some OOP paradigms & SOLID
 
Introduction to Clean Code
Introduction to Clean CodeIntroduction to Clean Code
Introduction to Clean Code
 
Professional development
Professional developmentProfessional development
Professional development
 

Recently uploaded

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Code metrics in PHP

  • 1. Code metrics in PHP From lines to code semantic
  • 2. ● Julio Martinez ● Developing PHP since 2001 ● 1.5 years working at Ulabox ● Find me: @liopic Code metrics in PHP: 0. Introduction Who am I?
  • 3. ● El antisúper! ● 6-years-old startup ● 11 developers ● monolith & new services ● #rigor ● We are hiring! Code metrics in PHP: 0. Introduction What is Ulabox?
  • 4. ● Evaluate quality! ● We need objective, reproducible and quantifiable metrics Could you tell me some examples of metrics? Code metrics in PHP: 1. Looking for quality Why do we need software metrics?
  • 5. ● number of bugfixes per month ● lines of code ● test coverage ● number of user stories covered ● follows clean code’s rules ● documentation lines / total of code lines ● etc Code metrics in PHP: 1. Looking for quality Some examples?
  • 6. ● Testing first! ● Code “surface” ● Lines grouping ● Code semantic Disclaimer: I’ll discuss locally-executable tools (non SaaS) ● SaaS: Insight, Code climate, Scrutinizer, SonarQube... Code metrics in PHP: 1. Looking for quality Let’s start our knowledge journey...
  • 7. ● User stories: behat ● General testing: phpunit ○ Code coverage ○ Mutant testing (=test your tests): humbug ● Unit/spec testing: phpspec Code metrics in PHP. Testing Testing First!
  • 8. ● Code sniffer (code style, PSR2): phpcs ● Copy+paste detector (DRY): phpcpd ● Clean code & common smells: ○ phpmd (“mess detector”, a bit old) ○ Exakat (it’s SaaS but has trial download) Code metrics in PHP: 3. Code surface Code “surface”
  • 9. ● Counting lines: phploc ● Getting ratios: pdepend Code metrics in PHP: 4. Lines grouping Lines grouping
  • 10. 1. Cyclomatic complexity (paths of execution) 2. Coupling metrics (relations between “modules”) 3. Lack of cohesion, LCOM (relations between methods) 4. Halstead’s metrics (operands and operators) 5. Maintainability Index Code metrics in PHP: 5. Code semantic Code semantic analysis
  • 11. Code metrics in PHP: 5. Code semantic Cyclomatic complexity abstract class BaseBird { private $eggs = 0; public function makeEgg() { $this->eggs++; } public function crackEgg() { if ($this->eggs <=0) { throw new NoEggsException(); } $this->eggs--; } public function fly() { return 'flap-flap'; } abstract public function sound(); } class Duck extends BaseBird { public function sound() { return 'quack'; } public function swim() { return 'splash'; } } class Parrot extends BaseBird { public function sound() { $friend = new Duck(); if($friend->sound()){ return 'Err '.$friend->sound(); } return ''; } }
  • 12. Code metrics in PHP: 5. Code semantic Cyclomatic complexity abstract class BaseBird { private $eggs = 0; public function makeEgg() { $this->eggs++; } public function crackEgg() { if ($this->eggs <=0) { throw new NoEggsException(); } $this->eggs--; } public function fly() { return 'flap-flap'; } abstract public function sound(); } class Duck extends BaseBird { public function sound() { return 'quack'; } public function swim() { return 'splash'; } } class Parrot extends BaseBird { public function sound() { $friend = new Duck(); if($friend->sound()){ return 'Err '.$friend->sound(); } return ''; } } 2 2 1
  • 13. ● Afferent couplings (Ca) - “they use you” ● Efferent couplings (Ce) - “you use them” ● Instability, resilience to change (I): I = Ce / (Ce + Ca) ○ I=0 is a completely stable package ○ I=1 is a completely unstable package Code metrics in PHP: 5. Code semantic Coupling metrics
  • 14. ● Abstractness (A): ratio of abstract classes ○ A=0 is a completely concrete package ○ A=1 is a completely abstract package ● Examples: ○ BaseBird: Ce=1 (uses NoEggsException), Ca=0; A=1, I=1 ○ Parrot, Ce=1 (uses Duck), Ca=0; A=0, I=1 ○ Duck, Ce=0, Ca=0; A=0, I=0 Code metrics in PHP: 5. Code semantic Coupling metrics
  • 15. ● Distance from the main sequence (D): D = |A+I-1| ○ Balance between abstractness and stability ○ Ideal packages: (I=1, A=0), (I=0, A=1) Code metrics in PHP: 5. Code semantic Coupling metrics: main sequence
  • 16. ● Groups of methods ● LCOM = 1 is ideal Code metrics in PHP: 5. Code semantic Lack of cohesion of methods (LCOM) abstract class BaseBird { private $eggs = 0; public function makeEgg() { $this->eggs++; } public function crackEgg() { if ($this->eggs <=0) { throw new NoEggsException(); } $this->eggs--; } public function fly() { return 'flap-flap'; } abstract public function sound(); }
  • 17. ● Groups of methods ● LCOM = 1 is ideal ● BaseBird LCOM = 3 Code metrics in PHP: 5. Code semantic Lack of cohesion of methods (LCOM) abstract class BaseBird { private $eggs = 0; public function makeEgg() { $this->eggs++; } public function crackEgg() { if ($this->eggs <=0) { throw new NoEggsException(); } $this->eggs--; } public function fly() { return 'flap-flap'; } abstract public function sound(); }
  • 18. ● η1 = number of distinct operators ● η2 = number of distinct operands ● N1 = the total number of operators ● N2 = the total number of operands ● Program vocabulary: η = η1 + η2 ● Program length: N = N1 + N2 Code metrics in PHP: 5. Code semantic Halstead complexity measures (1977)
  • 19. ● Volume: V = N × log2 η (linearly with length, log with vocabulary) ● Difficulty : D = η1 /2 × N2 /η2 (half distinct operators, scarcity of operands) ● Effort: E = D × V ● Time required to program: T = E/18 seconds ● Delivered bugs: Code metrics in PHP: 5. Code semantic Halstead complexity measures
  • 20. ● University of Idaho, Oman and Hagemeister ● Volume (V), Cyclomatic Complexity (G), Lines of Code (LOC) ● Original MI = 171 - 5.2*ln(V) - 0.23*G - 16.2*ln(LOC) Code metrics in PHP: 5. Code semantic Maintainability Index (1991)
  • 21. ● phpmetrics ○ Let’s see all those indexes! Code metrics in PHP: 5. Code semantic Coupling + LCOM + Halstead + MI Maintainability.................... 65.83 / 100 Accessibility for new developers... 41.03 / 100 Simplicity of algorithms........... 42.57 / 100 Volume............................. 64.90 / 100 Reducing bug's probability......... 65.57 / 100
  • 22.
  • 23.
  • 24.
  • 25. ● MUST! ○ phpcs - code sniffer - No extra effort need; always ○ phpunit - integration & unit tests - Write tests; always ● Adopt ○ phpspec - unit/specs - Write tests; always ○ behat - user stories (functional tests) - Write user stories; always ○ phpcpd - copy & paste - No effort; always ○ phpmetrics - No effort; main indexes weekly, others when refactor ● Give a try ○ humbug - test your tests - No effort; often ○ SaaS options - Customization effort; always Code metrics in PHP: 6. Summing up! RADAR of tools (how? when?)
  • 26. Code metrics in PHP: 6. Summing up! Questions?
  • 27. Code metrics in PHP: 6. Summing up! Thank you!