SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
Code quality

© 2012 Cognifide Limited. In commercial confidence only.
What is code quality
• Sample code quality questions:
− Are methods and classes clean, readable and short?
− Are connections between classes well-designed?
− Does your code follow Java conventions?


private static final rather than private final static

− Do you have unit tests? What is the coverage?
− Don’t you have some other code-smells?


Eg. if statement with too many || conditions

• Some issues caused by low quality code:
−
−
−
−

Problems with adding new features
Problems with collaboration
Hidden bugs
Hard to debug

• Static code analysis tools can check all things automatically
• Sonar – popular tool for Java

© 2012 Cognifide Limited. In commercial confidence only.
Coding standards

© 2012 Cognifide Limited. In commercial confidence only.
Coding standards – example
class FileUtils {
public final static String pngExtension = "png";
public static String getExtension(String filename) {
int DOTPos = filename.indexOf(".");
if (filename != null && DOTPos > -1) {
String extension = filename.substring(DOTPos+1);
return extension;
}
return null;
}
}

© 2012 Cognifide Limited. In commercial confidence only.
Cyclomatic complexity

© 2012 Cognifide Limited. In commercial confidence only.
Cyclomatic complexity
•
•
•
•
•

Basic code metric
Describes number of “branches” in method or class
Sequential code (no ifs, fors and whiles): complexity = 1
Each additional condition: +1
If method is too complex, we should split it into smaller ones

© 2012 Cognifide Limited. In commercial confidence only.
House of cards

© 2012 Cognifide Limited. In commercial confidence only.
House of cards

© 2012 Cognifide Limited. In commercial confidence only.
House of cards

© 2012 Cognifide Limited. In commercial confidence only.
Complexity for sequential code
byte[] buffer = new byte[1024];
InputStream stream = new FileInputStream("/my/file");
InputStream buffered = new BufferedInputStream(stream);
buffered.read(buffer);
buffered.close();

• Complexity: 1
• No conditions, simple flow

© 2012 Cognifide Limited. In commercial confidence only.
Complexity with one condition
byte[] buffer = new byte[1024];
File file = new File("/my/file");
if (!file.exists()) {
return;
}
InputStream stream = new FileInputStream(file);
InputStream buffered = new BufferedInputStream(stream);
buffered.read(buffer);
buffered.close();

• Complexity: 2

© 2012 Cognifide Limited. In commercial confidence only.
Complexity: formal definition
Formal definition: Edges – Nodes + 2 Components
for(int i=0;i<3;i++) {
System.out.println(i);
}

if(j == 42) {
System.out.println(”j is the answer”)
}
Edges: 9, Nodes: 8, Components: 1
E-N+2P=3

© 2012 Cognifide Limited. In commercial confidence only.
Class responsibilities

© 2012 Cognifide Limited. In commercial confidence only.
Single responsibility principle
• Each class should be responsible for only one thing
• Example: class Driver:
− 2 fields: car, brain,
− 5 methods: drive(), goTo(), stop(), getAngry(), drinkCoffee(),

• This class has 3 responsibilities:
− Driving car
− Getting angry
− Drinking coffe

• It should be split into 3 classes

© 2012 Cognifide Limited. In commercial confidence only.
LCOM4 & cohesion
• LCOM4 – Lack Of Cohesion Methods.
• How many responsibilities class?
• LCOM4 = 3 means that
− there are 3 not connected sets of methods/properties
− class can be split into three new classes.

• Class should be designed to have LCOM4 = 1
• Low LCOM4 = high cohesion
• Cohesion relates to coupling

© 2012 Cognifide Limited. In commercial confidence only.
Coupling

© 2012 Cognifide Limited. In commercial confidence only.
Tight coupling
• If a class (eg. Driver) invokes methods on some other class
(eg. Car) it’s tightly coupled with it
• In this case we can’t easily change Car implementation to
something other (eg. Tank)
• Real-life example: iPhone and its battery
public class Driver {
private final Car car;
public Driver(Car car) {
this.car = car;
}
public void driveCar() {
car.startEngine();
car.drive();
}

public class Car {
public void startEngine() {
…
}
public void drive() {
…
}
}

}
© 2012 Cognifide Limited. In commercial confidence only.
Loose coupling
• Class doesn’t need to know about other classes existence
• It only need to know about contracts (interfaces) provided by
other class
• Real-life example: screw and screwdriver
public class Driver {
private Vehicle vehicle;
public Driver(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void drive() {
vehicle.startEngine();
vehicle.drive();
}

public interface Vehicle {
void startEngine();
void drive();
}
public class Car implements Vehicle {
public void startEngine() {
…
}
public void drive() {
…
}

}
}

© 2012 Cognifide Limited. In commercial confidence only.
Coupling & cohesion – summary
• Classes must be loosely coupled
and highly cohesive.
• Cohesion is the degree to which the
methods of a single class are tight
together.
− If class is not cohesive (eg. two methods
doesn’t share any property), it can be
split to two classes.

• Coupling is the degree to which each
class is tight to the others.
− Any change you make in one class has
impact to some other classes.
− Use interfaces & depdency injection

• Read more…
• Read even more…

© 2012 Cognifide Limited. In commercial confidence only.
Technical debt

© 2012 Cognifide Limited. In commercial confidence only.
Technical debt
• „Doing things the quick and dirty way sets us up with a
technical debt, which is similar to a financial debt.”
• Metric based on:
−
−
−
−
−

•
•
•
•

duplications,
violations,
(lack of) comments,
coverage,
complexity.

Absolute debt in mandays and cash
Debt ratio – dependent on the project size.
Useful for project manager
Read more...

© 2012 Cognifide Limited. In commercial confidence only.
Other metrics

© 2012 Cognifide Limited. In commercial confidence only.
Other code metrics
•
•
•
•
•

Lines of code
Unit test coverage
Responses for class
…
All these metrics can be checked via Sonar
− Free & very useful software
− Can be integrated with Eclipse

• Visual Studio provides integrated metrics as well

© 2012 Cognifide Limited. In commercial confidence only.

Mais conteúdo relacionado

Mais procurados

DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance
 
Vb net xp_04
Vb net xp_04Vb net xp_04
Vb net xp_04Niit Care
 
Review Session and Attending Java Interviews
Review Session and Attending Java InterviewsReview Session and Attending Java Interviews
Review Session and Attending Java InterviewsRatnaJava
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesHitesh-Java
 
Top 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | EdurekaTop 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | EdurekaEdureka!
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
3. C# Guide Advance - To Print
3. C# Guide Advance - To Print3. C# Guide Advance - To Print
3. C# Guide Advance - To PrintChinthaka Fernando
 
Top 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetTop 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetDevLabs Alliance
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview QuestionsEhtisham Ali
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersKhaled Adnan
 
C# Interview Questions | Edureka
C# Interview Questions | EdurekaC# Interview Questions | Edureka
C# Interview Questions | EdurekaEdureka!
 

Mais procurados (14)

DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
 
Vb net xp_04
Vb net xp_04Vb net xp_04
Vb net xp_04
 
Review Session and Attending Java Interviews
Review Session and Attending Java InterviewsReview Session and Attending Java Interviews
Review Session and Attending Java Interviews
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
 
Top 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | EdurekaTop 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | Edureka
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
3. C# Guide Advance - To Print
3. C# Guide Advance - To Print3. C# Guide Advance - To Print
3. C# Guide Advance - To Print
 
Top 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetTop 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdet
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
Java interview question
Java interview questionJava interview question
Java interview question
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
C# Interview Questions | Edureka
C# Interview Questions | EdurekaC# Interview Questions | Edureka
C# Interview Questions | Edureka
 

Destaque

Project quality (and test process) metrics
Project quality (and test process) metricsProject quality (and test process) metrics
Project quality (and test process) metricsZbyszek Mockun
 
CRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migrationCRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migrationTomasz Rękawek
 
Agile Scrum in 60 minutes
Agile Scrum in 60 minutesAgile Scrum in 60 minutes
Agile Scrum in 60 minutesSyed Arh
 
Agile metrics - Measure and Improve
Agile metrics - Measure and ImproveAgile metrics - Measure and Improve
Agile metrics - Measure and ImproveWemanityUK
 
Top 10 Agile Metrics
Top 10 Agile MetricsTop 10 Agile Metrics
Top 10 Agile MetricsXBOSoft
 
Presentation -Quality Metrics For Agile Development
Presentation -Quality Metrics For Agile DevelopmentPresentation -Quality Metrics For Agile Development
Presentation -Quality Metrics For Agile DevelopmentNabilahmed Patel
 
Agile Base Camp - Agile metrics
Agile Base Camp - Agile metricsAgile Base Camp - Agile metrics
Agile Base Camp - Agile metricsSerge Kovaleff
 
Measuring the Code Quality Using Software Metrics
Measuring the Code Quality Using Software MetricsMeasuring the Code Quality Using Software Metrics
Measuring the Code Quality Using Software MetricsGeetha Anjali
 
Agile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User GroupAgile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User Groupsuwalki24.pl
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metricsGil Nahmias
 
Agile Metrics : A seminal approach for calculating Metrics in Agile Projects
Agile Metrics : A seminal approach for calculating Metrics in Agile ProjectsAgile Metrics : A seminal approach for calculating Metrics in Agile Projects
Agile Metrics : A seminal approach for calculating Metrics in Agile ProjectsPrashant Ram
 
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2VersionOne
 

Destaque (17)

Project quality (and test process) metrics
Project quality (and test process) metricsProject quality (and test process) metrics
Project quality (and test process) metrics
 
Test Metrics and KPIs
Test Metrics and KPIsTest Metrics and KPIs
Test Metrics and KPIs
 
SlingQuery
SlingQuerySlingQuery
SlingQuery
 
CRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migrationCRX2Oak - all the secrets of repository migration
CRX2Oak - all the secrets of repository migration
 
Agile Scrum in 60 minutes
Agile Scrum in 60 minutesAgile Scrum in 60 minutes
Agile Scrum in 60 minutes
 
Agile metrics - Measure and Improve
Agile metrics - Measure and ImproveAgile metrics - Measure and Improve
Agile metrics - Measure and Improve
 
Agile Metrics
Agile MetricsAgile Metrics
Agile Metrics
 
Agile Metrics
Agile MetricsAgile Metrics
Agile Metrics
 
Top 10 Agile Metrics
Top 10 Agile MetricsTop 10 Agile Metrics
Top 10 Agile Metrics
 
Presentation -Quality Metrics For Agile Development
Presentation -Quality Metrics For Agile DevelopmentPresentation -Quality Metrics For Agile Development
Presentation -Quality Metrics For Agile Development
 
Agile Base Camp - Agile metrics
Agile Base Camp - Agile metricsAgile Base Camp - Agile metrics
Agile Base Camp - Agile metrics
 
Measuring the Code Quality Using Software Metrics
Measuring the Code Quality Using Software MetricsMeasuring the Code Quality Using Software Metrics
Measuring the Code Quality Using Software Metrics
 
Agile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User GroupAgile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User Group
 
Agile code quality metrics
Agile code quality metricsAgile code quality metrics
Agile code quality metrics
 
Agile Metrics : A seminal approach for calculating Metrics in Agile Projects
Agile Metrics : A seminal approach for calculating Metrics in Agile ProjectsAgile Metrics : A seminal approach for calculating Metrics in Agile Projects
Agile Metrics : A seminal approach for calculating Metrics in Agile Projects
 
Agile Metrics That Matter
Agile Metrics That MatterAgile Metrics That Matter
Agile Metrics That Matter
 
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2
AgileLIVE Webinar: Measuring the Success of Your Agile Transformation - Part 2
 

Semelhante a Code metrics

SPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactorySPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactoryNCCOMMS
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDDDhaval Dalal
 
Seven elements of technical Agility - Gil Broza - Agile Israel 2013
Seven elements of technical Agility - Gil Broza - Agile Israel 2013Seven elements of technical Agility - Gil Broza - Agile Israel 2013
Seven elements of technical Agility - Gil Broza - Agile Israel 2013AgileSparks
 
Project Work
Project WorkProject Work
Project Workz02247
 
Edge Presentation Ppt
Edge Presentation PptEdge Presentation Ppt
Edge Presentation PptBrendaDean
 
DS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham ChartersDS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Chartersmfrancis
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito
 
DS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham ChartersDS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Chartersmfrancis
 
Our research lines on Model-Driven Engineering and Software Engineering
Our research lines on Model-Driven Engineering and Software EngineeringOur research lines on Model-Driven Engineering and Software Engineering
Our research lines on Model-Driven Engineering and Software EngineeringJordi Cabot
 
Framework for Web Automation Testing
Framework for Web Automation TestingFramework for Web Automation Testing
Framework for Web Automation TestingTaras Lytvyn
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net coreSam Nasr, MCSA, MVP
 
Integrating DDS into AXCIOMA - The Component Approach
Integrating DDS into AXCIOMA - The Component ApproachIntegrating DDS into AXCIOMA - The Component Approach
Integrating DDS into AXCIOMA - The Component ApproachReal-Time Innovations (RTI)
 
Integrating DDS into AXCIOMA, the component approach
Integrating DDS into AXCIOMA, the component approachIntegrating DDS into AXCIOMA, the component approach
Integrating DDS into AXCIOMA, the component approachRemedy IT
 
Software quality - no more bugs!
Software quality - no more bugs!Software quality - no more bugs!
Software quality - no more bugs!Arnon Axelrod
 
Common Design Patterns.pptx
Common Design Patterns.pptxCommon Design Patterns.pptx
Common Design Patterns.pptxfake195786
 
Mobile Code Optimisation Services
Mobile Code Optimisation ServicesMobile Code Optimisation Services
Mobile Code Optimisation ServicesRaja Nagendra Kumar
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio
 
Ada 2012
Ada 2012Ada 2012
Ada 2012AdaCore
 

Semelhante a Code metrics (20)

SPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint FactorySPCA2013 - Building a SharePoint Factory
SPCA2013 - Building a SharePoint Factory
 
Integration testing - A&BP CC
Integration testing - A&BP CCIntegration testing - A&BP CC
Integration testing - A&BP CC
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Seven elements of technical Agility - Gil Broza - Agile Israel 2013
Seven elements of technical Agility - Gil Broza - Agile Israel 2013Seven elements of technical Agility - Gil Broza - Agile Israel 2013
Seven elements of technical Agility - Gil Broza - Agile Israel 2013
 
Project Work
Project WorkProject Work
Project Work
 
Edge Presentation Ppt
Edge Presentation PptEdge Presentation Ppt
Edge Presentation Ppt
 
DS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham ChartersDS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Charters
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
 
DS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham ChartersDS, BP, EJB, CDI, WTF!? - Graham Charters
DS, BP, EJB, CDI, WTF!? - Graham Charters
 
Our research lines on Model-Driven Engineering and Software Engineering
Our research lines on Model-Driven Engineering and Software EngineeringOur research lines on Model-Driven Engineering and Software Engineering
Our research lines on Model-Driven Engineering and Software Engineering
 
Framework for Web Automation Testing
Framework for Web Automation TestingFramework for Web Automation Testing
Framework for Web Automation Testing
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
 
Integrating DDS into AXCIOMA - The Component Approach
Integrating DDS into AXCIOMA - The Component ApproachIntegrating DDS into AXCIOMA - The Component Approach
Integrating DDS into AXCIOMA - The Component Approach
 
Integrating DDS into AXCIOMA, the component approach
Integrating DDS into AXCIOMA, the component approachIntegrating DDS into AXCIOMA, the component approach
Integrating DDS into AXCIOMA, the component approach
 
Software quality - no more bugs!
Software quality - no more bugs!Software quality - no more bugs!
Software quality - no more bugs!
 
Common Design Patterns.pptx
Common Design Patterns.pptxCommon Design Patterns.pptx
Common Design Patterns.pptx
 
Ravindra Prasad
Ravindra PrasadRavindra Prasad
Ravindra Prasad
 
Mobile Code Optimisation Services
Mobile Code Optimisation ServicesMobile Code Optimisation Services
Mobile Code Optimisation Services
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
Ada 2012
Ada 2012Ada 2012
Ada 2012
 

Mais de Tomasz Rękawek

Deep-dive into cloud-native AEM deployments based on Kubernetes
Deep-dive into cloud-native AEM deployments based on KubernetesDeep-dive into cloud-native AEM deployments based on Kubernetes
Deep-dive into cloud-native AEM deployments based on KubernetesTomasz Rękawek
 
Emulating Game Boy in Java
Emulating Game Boy in JavaEmulating Game Boy in Java
Emulating Game Boy in JavaTomasz Rękawek
 
Zero downtime deployments for the Sling-based apps using Docker
Zero downtime deployments for the Sling-based apps using DockerZero downtime deployments for the Sling-based apps using Docker
Zero downtime deployments for the Sling-based apps using DockerTomasz Rękawek
 
Inter-Sling communication with message queue
Inter-Sling communication with message queueInter-Sling communication with message queue
Inter-Sling communication with message queueTomasz Rękawek
 
Shooting rabbits with sling
Shooting rabbits with slingShooting rabbits with sling
Shooting rabbits with slingTomasz Rękawek
 

Mais de Tomasz Rękawek (7)

Radio ad blocker
Radio ad blockerRadio ad blocker
Radio ad blocker
 
Deep-dive into cloud-native AEM deployments based on Kubernetes
Deep-dive into cloud-native AEM deployments based on KubernetesDeep-dive into cloud-native AEM deployments based on Kubernetes
Deep-dive into cloud-native AEM deployments based on Kubernetes
 
Emulating Game Boy in Java
Emulating Game Boy in JavaEmulating Game Boy in Java
Emulating Game Boy in Java
 
Zero downtime deployments for the Sling-based apps using Docker
Zero downtime deployments for the Sling-based apps using DockerZero downtime deployments for the Sling-based apps using Docker
Zero downtime deployments for the Sling-based apps using Docker
 
Inter-Sling communication with message queue
Inter-Sling communication with message queueInter-Sling communication with message queue
Inter-Sling communication with message queue
 
Sling Dynamic Include
Sling Dynamic IncludeSling Dynamic Include
Sling Dynamic Include
 
Shooting rabbits with sling
Shooting rabbits with slingShooting rabbits with sling
Shooting rabbits with sling
 

Último

UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 

Último (20)

UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 

Code metrics

  • 1. Code quality © 2012 Cognifide Limited. In commercial confidence only.
  • 2. What is code quality • Sample code quality questions: − Are methods and classes clean, readable and short? − Are connections between classes well-designed? − Does your code follow Java conventions?  private static final rather than private final static − Do you have unit tests? What is the coverage? − Don’t you have some other code-smells?  Eg. if statement with too many || conditions • Some issues caused by low quality code: − − − − Problems with adding new features Problems with collaboration Hidden bugs Hard to debug • Static code analysis tools can check all things automatically • Sonar – popular tool for Java © 2012 Cognifide Limited. In commercial confidence only.
  • 3. Coding standards © 2012 Cognifide Limited. In commercial confidence only.
  • 4. Coding standards – example class FileUtils { public final static String pngExtension = "png"; public static String getExtension(String filename) { int DOTPos = filename.indexOf("."); if (filename != null && DOTPos > -1) { String extension = filename.substring(DOTPos+1); return extension; } return null; } } © 2012 Cognifide Limited. In commercial confidence only.
  • 5. Cyclomatic complexity © 2012 Cognifide Limited. In commercial confidence only.
  • 6. Cyclomatic complexity • • • • • Basic code metric Describes number of “branches” in method or class Sequential code (no ifs, fors and whiles): complexity = 1 Each additional condition: +1 If method is too complex, we should split it into smaller ones © 2012 Cognifide Limited. In commercial confidence only.
  • 7. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 8. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 9. House of cards © 2012 Cognifide Limited. In commercial confidence only.
  • 10. Complexity for sequential code byte[] buffer = new byte[1024]; InputStream stream = new FileInputStream("/my/file"); InputStream buffered = new BufferedInputStream(stream); buffered.read(buffer); buffered.close(); • Complexity: 1 • No conditions, simple flow © 2012 Cognifide Limited. In commercial confidence only.
  • 11. Complexity with one condition byte[] buffer = new byte[1024]; File file = new File("/my/file"); if (!file.exists()) { return; } InputStream stream = new FileInputStream(file); InputStream buffered = new BufferedInputStream(stream); buffered.read(buffer); buffered.close(); • Complexity: 2 © 2012 Cognifide Limited. In commercial confidence only.
  • 12. Complexity: formal definition Formal definition: Edges – Nodes + 2 Components for(int i=0;i<3;i++) { System.out.println(i); } if(j == 42) { System.out.println(”j is the answer”) } Edges: 9, Nodes: 8, Components: 1 E-N+2P=3 © 2012 Cognifide Limited. In commercial confidence only.
  • 13. Class responsibilities © 2012 Cognifide Limited. In commercial confidence only.
  • 14. Single responsibility principle • Each class should be responsible for only one thing • Example: class Driver: − 2 fields: car, brain, − 5 methods: drive(), goTo(), stop(), getAngry(), drinkCoffee(), • This class has 3 responsibilities: − Driving car − Getting angry − Drinking coffe • It should be split into 3 classes © 2012 Cognifide Limited. In commercial confidence only.
  • 15. LCOM4 & cohesion • LCOM4 – Lack Of Cohesion Methods. • How many responsibilities class? • LCOM4 = 3 means that − there are 3 not connected sets of methods/properties − class can be split into three new classes. • Class should be designed to have LCOM4 = 1 • Low LCOM4 = high cohesion • Cohesion relates to coupling © 2012 Cognifide Limited. In commercial confidence only.
  • 16. Coupling © 2012 Cognifide Limited. In commercial confidence only.
  • 17. Tight coupling • If a class (eg. Driver) invokes methods on some other class (eg. Car) it’s tightly coupled with it • In this case we can’t easily change Car implementation to something other (eg. Tank) • Real-life example: iPhone and its battery public class Driver { private final Car car; public Driver(Car car) { this.car = car; } public void driveCar() { car.startEngine(); car.drive(); } public class Car { public void startEngine() { … } public void drive() { … } } } © 2012 Cognifide Limited. In commercial confidence only.
  • 18. Loose coupling • Class doesn’t need to know about other classes existence • It only need to know about contracts (interfaces) provided by other class • Real-life example: screw and screwdriver public class Driver { private Vehicle vehicle; public Driver(Vehicle vehicle) { this.vehicle = vehicle; } public void drive() { vehicle.startEngine(); vehicle.drive(); } public interface Vehicle { void startEngine(); void drive(); } public class Car implements Vehicle { public void startEngine() { … } public void drive() { … } } } © 2012 Cognifide Limited. In commercial confidence only.
  • 19. Coupling & cohesion – summary • Classes must be loosely coupled and highly cohesive. • Cohesion is the degree to which the methods of a single class are tight together. − If class is not cohesive (eg. two methods doesn’t share any property), it can be split to two classes. • Coupling is the degree to which each class is tight to the others. − Any change you make in one class has impact to some other classes. − Use interfaces & depdency injection • Read more… • Read even more… © 2012 Cognifide Limited. In commercial confidence only.
  • 20. Technical debt © 2012 Cognifide Limited. In commercial confidence only.
  • 21. Technical debt • „Doing things the quick and dirty way sets us up with a technical debt, which is similar to a financial debt.” • Metric based on: − − − − − • • • • duplications, violations, (lack of) comments, coverage, complexity. Absolute debt in mandays and cash Debt ratio – dependent on the project size. Useful for project manager Read more... © 2012 Cognifide Limited. In commercial confidence only.
  • 22. Other metrics © 2012 Cognifide Limited. In commercial confidence only.
  • 23. Other code metrics • • • • • Lines of code Unit test coverage Responses for class … All these metrics can be checked via Sonar − Free & very useful software − Can be integrated with Eclipse • Visual Studio provides integrated metrics as well © 2012 Cognifide Limited. In commercial confidence only.