SlideShare une entreprise Scribd logo
1  sur  52
What are they and why do they matter
 Attila Bertók
C# technical lead
 Contact:
 bertok.atti@gmail.com
 https://www.linkedin.com/in/bertokattila/
 After a certain size, code becomes worse
 Sheer size
 Complexity
 Lack of ownership
 everyone’s code is no one’s code
 broken windows theory
 Duplication
 Conditional logic and logical exceptions
 Legacy code (the no dev’s land)
 code that other developers wrote
 code that you wrote, but more than two weeks ago
 Decreased performance
 Developers need more time to understand the code
 Developers need more time to implement changes to the code
 Developers’ confidence is decreased
 Increased bug count
 New bugs get introduced
 Old bugs surface and re-surface
 And how do I measure this “worse-ness”?
The things that my machine can do for me
 Static (as opposed to dynamic): Done without actually
executing the program
 Quantifiable measurements
 General code health
 Early warning system
 Visual Studio (Roslyn)
 Extensions:
 CodeIt.Right
 CodeRush
 FxCop
 StyleCop
 Static Analysis tool:
 SonarQube
 NDepend
 Critical & Non-Critical rules
 Critical rule violation breaks build
 Non-Critical rule violation adds a warning
 Non-Critical violations are still useful for monitoring and
code health, and prioritizing refactorings, and clean-up
efforts.
 Code readability
 Code comprehension
 Code maintainability
“Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
[Martin Fowler]
 Spacing, bracing, indenting, etc.
 Aim: give the code uniform look, so intent is obvious at the
first glance
 Common example: braces for single line statements
 Enforce braces:
if (areBracesEnforced)
{
return bracedCode;
}
 Omit braces:
if (areBracesOmitted)
return bracelessCode;
 The developers get used to the style and do their “internal
pattern-matching” based on their expectations
 Inconsistent style leads to errors
 Code should always convey intent
 and do so as clearly as possible
 Patterns in code should always be obvious
 and easy to recognize
 on any level
 be it codestyle
 or architectural design
private static Bitmap ConvolutionFilter(
Bitmap sourceBitmap,
double[,] filterMatrix) {
var lockedRectangle =
new Rectangle(
0,
0,
sourceBitmap.Width,
sourceBitmap.Height);
BitmapData sourceData =
sourceBitmap.LockBits(lockedRectangle);
// […]
if (sourceBitmap.IsGreyScale) {
for (int k = 0; k < buffer.Length; k += 4) {
rgb = buffer[k] * 0.11f;
buffer[k] = (byte)rgb;
}
}
}
private static Bitmap ConvolutionFilter(
Bitmap sourceBitmap,
double[,] filterMatrix)
{
var lockedRectangle =
new Rectangle(
0,
0,
sourceBitmap.Width,
sourceBitmap.Height);
BitmapData sourceData =
sourceBitmap.LockBits(lockedRectangle);
// […]
if (sourceBitmap.IsGreyScale)
{
for (int k = 0; k < buffer.Length; k += 4)
{
rgb = buffer[k] * 0.11f;
buffer[k] = (byte)rgb;
}
}
}
xxxxxxx xxxxxx xxxxxx xxxxxxxxxxxxxxxxx(
xxxxxx xxxxxxxxxxxx,
xxxxxx[,] xxxxxxxxxxxx) {
xxx xxxxxxxxxxxxxxx =
xxx xxxxxxxxx(
x,
x,
xxxxxxxxxxxx.xxxxx,
xxxxxxxxxxxx.xxxxxx);
xxxxxxxxxx xxxxxxxxxx =
xxxxxxxxxxxx.xxxxxxxx(xxxxxxxxxxxxxxx);
// […]
xx (xxxxxxxxxxxx.xxxxxxxxxxx) {
xxx (xxx x = x; x < xxxxxx.xxxxxx; x += x) {
xxx = xxxxxx[x] * x.xxx;
xxxxxx[x] = (xxxx)xxx;
}
}
}
xxxxxxx xxxxxx xxxxxx xxxxxxxxxxxxxxxxx(
xxxxxx xxxxxxxxxxxx,
xxxxxx[,] xxxxxxxxxxxx)
{
xxx xxxxxxxxxxxxxxx =
xxx xxxxxxxxx(
x,
x,
xxxxxxxxxxxx.xxxxx,
xxxxxxxxxxxx.xxxxxx);
xxxxxxxxxx xxxxxxxxxx =
xxxxxxxxxxxx.xxxxxxxx(xxxxxxxxxxxxxxx);
// […]
xx (xxxxxxxxxxxx.xxxxxxxxxxx)
{
xxx (xxx x = x; x < xxxxxx.xxxxxx; x += x)
{
xxx = xxxxxx[x] * x.xxx;
xxxxxx[x] = (xxxx)xxx;
}
}
}
 Lay down some foundations
 Common expectations
 Rules that the environment and the tools enforce
 Rules that code reviews should enforce
 Rules for:
 Documentation
 File layout
 Maintainability
 Naming
 Ordering
 Readability
 Spacing
That are not merely stylistic in approach…
 The title says it all. 
 Recommendation:
 The best method is a method that has no arguments (niladic)
 One argument (monadic) or two (dyadic) is acceptable
 Three arguments (triadic) should be avoided whenever possible
 More than that (polyadic) require a rather good justification
 Move things to instance variables!
 Separate functions (initialization, ctor, etc.)
 Rearrange classes (are many of your methods accepting the
same class as argument? It is possible that those methods
belong to that class!)
 Avoid out parameters
 Avoid flag arguments
 The title says it all – again.
 Recommendation: keep it low. Anything above 8 is a serious
overkill, anything above 5 is potentially too many.
 Guess what.
 No overload is 1.
 Recommendation: keep it low. Many overloads can be
difficult to keep in mind. However, there are no hard rules for
this.
CC = 1 + …
the number of any of the following expressions found in the
method body:
 if/case/default/?:/??/.?
 while/for/foreach/continue
 goto
 &&/||
 catch
 Recommendations: The smaller the better. Static analysis
tools recommend a max of 15 per method.
 I’ve heard of companies where anything over 8 automatically breaks
the build.
 In case of .net it is generally lines of IL code (Logical LOC)
 Code style (and language) does not influence it
 Interfaces, abstract methods, enumerations have a LOC of 0.
 Namespaces, namespace references, and declarations have a
LOC of 0.
 LOC of classes is the sum of the LOC of their methods.
 Recommendation: keep as low as possible
 Simple as it sounds, the number of the lines containing
comment
 Inline comments and full-line comments are worth the same
 Recommendation: keep it as low as possible
 (100 * LOComments) / (LOComments + LOC)
 The amount of required comment strongly depends on the
quality of code
 Code should be self-explanatory and should not require comments
 Recommendation: keep as low as possible
 Percentage of code covered by (unit/acceptance) tests.
 Recommendation: The closer to 100% the better.
 If using test first, anything below 100% is pretty bad.
… and methods, in some cases.
Classes should have a small number of instance variables. Each
of the methods of a class should manipulate one or more of
those variables.
In general the more variables a method manipulates the more
cohesive that method is to its class.
A class in which each variable is used by each method is
maximally cohesive.
The strategy of keeping functions small and keeping parameter
lists short can sometimes lead to a proliferation of instance
variables that are used by a subset of methods.
When this happens, it almost always means that there is at
least one other class trying to get out of the larger class. You
should try to separate the variables and methods into two or
more classes such that the new classes are more cohesive.
M is the number of methods in class (both static and instance
methods are counted, it includes also constructors, properties
getters/setters, events add/remove methods).
F is the number of instance fields in the class.
MF is the number of methods of the class accessing a particular
instance field.
Sum(MF) is the sum of MF over all instance fields of the class.
LCOM = 1 – (sum(MF)/M*F)
LCOM HS = (M – sum(MF)/F)(M-1)
DI: Incoming dependencies: the number of classes that depend on
this class
DO: Outgoing dependencies: the number of classes that this class
depends on.
Instability: DO / (DI + DO) (0..1)
I = 0: no other components depend on this component.
I = 1: this component depends on no other components.
NC: the number of classes in the assembly
NA: the number of abstract classes in the assembly
A: Abstractness:
A = NA / NC
 Zone of Uselessness:
Abstract components that
are never inherited
 Zone of Pain: very stable,
thus difficult to change,
meanwhile very concrete,
thus difficult to extend
D = |A + I - 1 |
Recommendation: Classes should be as close to the main
sequence as possible.
 Methods
 Fields
 Recommendation: keep classes small.
For a class/namespace/assembly/etc.:
Sum of CC from methods/classes/assemblies.
 The title (almost) says it all. 
 For derived classes, it also includes the interfaces
implemented by the base class.
 Recommendation: keep low. ;)
 The title (almost) says it all (again). 
 (Including System.Object, so always starts from 1.)
 Recommendation: keep low. ;)
 Types
 Namespaces
 Assemblies
Average number of internal relationships per type.
Let R be the number of type relationships that are internal to
this assembly (i.e. that do not connect to types outside the
assembly).
Let N be the number of types within the assembly.
H = (R + 1)/ N.
Recommendation: Keep it high.
The things that my machine cannot do for me
 SOLID principles
 Consistent style
 Succinct but readable code
 Good naming
 The Boy Scout Rule and Opportunistic refactoring
Code metrics in Visual Studio:
https://msdn.microsoft.com/en-us/library/bb385914.aspx
A Short Primer on Code Quality Metrics:
https://ardalis.com/static-code-analysis-and-quality-metrics
NDepend Code Metrics:
https://www.ndepend.com/docs/code-metrics
NDepend
SonarQube
FxCop
StyleCop
Code Metrics

Contenu connexe

Tendances

Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Qualityguest268ee8
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Daniyal Mughal
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08Terry Yoast
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard partsShaun Abram
 
Type checking
Type checkingType checking
Type checkingrawan_z
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13Terry Yoast
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03Terry Yoast
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataCory Foy
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06Terry Yoast
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07Terry Yoast
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 

Tendances (18)

CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6
 
9781111530532 ppt ch08
9781111530532 ppt ch089781111530532 ppt ch08
9781111530532 ppt ch08
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
Type checking
Type checkingType checking
Type checking
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 
Chap04
Chap04Chap04
Chap04
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
9781111530532 ppt ch06
9781111530532 ppt ch069781111530532 ppt ch06
9781111530532 ppt ch06
 
9781111530532 ppt ch07
9781111530532 ppt ch079781111530532 ppt ch07
9781111530532 ppt ch07
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 

Similaire à Code Metrics

C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questionsnicolbiden
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphismsanjay joshi
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphismumesh patil
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Groupbrada
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Anil Bapat
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldDavid McCarter
 
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
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean CodingMetin Ogurlu
 

Similaire à Code Metrics (20)

C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Code Quality
Code QualityCode Quality
Code Quality
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
Code review
Code reviewCode review
Code review
 
Code quality
Code qualityCode quality
Code quality
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Back-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real WorldBack-2-Basics: .NET Coding Standards For The Real World
Back-2-Basics: .NET Coding Standards For The Real World
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Generics
GenericsGenerics
Generics
 
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
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Modern C++
Modern C++Modern C++
Modern C++
 

Dernier

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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
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
 
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
 
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
 
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
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 

Dernier (20)

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...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 

Code Metrics

  • 1. What are they and why do they matter
  • 2.  Attila Bertók C# technical lead  Contact:  bertok.atti@gmail.com  https://www.linkedin.com/in/bertokattila/
  • 3.  After a certain size, code becomes worse  Sheer size  Complexity  Lack of ownership  everyone’s code is no one’s code  broken windows theory  Duplication  Conditional logic and logical exceptions  Legacy code (the no dev’s land)  code that other developers wrote  code that you wrote, but more than two weeks ago
  • 4.  Decreased performance  Developers need more time to understand the code  Developers need more time to implement changes to the code  Developers’ confidence is decreased  Increased bug count  New bugs get introduced  Old bugs surface and re-surface
  • 5.  And how do I measure this “worse-ness”?
  • 6. The things that my machine can do for me
  • 7.  Static (as opposed to dynamic): Done without actually executing the program  Quantifiable measurements  General code health  Early warning system
  • 8.  Visual Studio (Roslyn)  Extensions:  CodeIt.Right  CodeRush  FxCop  StyleCop  Static Analysis tool:  SonarQube  NDepend
  • 9.  Critical & Non-Critical rules  Critical rule violation breaks build  Non-Critical rule violation adds a warning  Non-Critical violations are still useful for monitoring and code health, and prioritizing refactorings, and clean-up efforts.
  • 10.  Code readability  Code comprehension  Code maintainability
  • 11. “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” [Martin Fowler]
  • 12.  Spacing, bracing, indenting, etc.  Aim: give the code uniform look, so intent is obvious at the first glance  Common example: braces for single line statements
  • 13.  Enforce braces: if (areBracesEnforced) { return bracedCode; }  Omit braces: if (areBracesOmitted) return bracelessCode;
  • 14.  The developers get used to the style and do their “internal pattern-matching” based on their expectations  Inconsistent style leads to errors
  • 15.  Code should always convey intent  and do so as clearly as possible  Patterns in code should always be obvious  and easy to recognize  on any level  be it codestyle  or architectural design
  • 16. private static Bitmap ConvolutionFilter( Bitmap sourceBitmap, double[,] filterMatrix) { var lockedRectangle = new Rectangle( 0, 0, sourceBitmap.Width, sourceBitmap.Height); BitmapData sourceData = sourceBitmap.LockBits(lockedRectangle); // […] if (sourceBitmap.IsGreyScale) { for (int k = 0; k < buffer.Length; k += 4) { rgb = buffer[k] * 0.11f; buffer[k] = (byte)rgb; } } } private static Bitmap ConvolutionFilter( Bitmap sourceBitmap, double[,] filterMatrix) { var lockedRectangle = new Rectangle( 0, 0, sourceBitmap.Width, sourceBitmap.Height); BitmapData sourceData = sourceBitmap.LockBits(lockedRectangle); // […] if (sourceBitmap.IsGreyScale) { for (int k = 0; k < buffer.Length; k += 4) { rgb = buffer[k] * 0.11f; buffer[k] = (byte)rgb; } } }
  • 17. xxxxxxx xxxxxx xxxxxx xxxxxxxxxxxxxxxxx( xxxxxx xxxxxxxxxxxx, xxxxxx[,] xxxxxxxxxxxx) { xxx xxxxxxxxxxxxxxx = xxx xxxxxxxxx( x, x, xxxxxxxxxxxx.xxxxx, xxxxxxxxxxxx.xxxxxx); xxxxxxxxxx xxxxxxxxxx = xxxxxxxxxxxx.xxxxxxxx(xxxxxxxxxxxxxxx); // […] xx (xxxxxxxxxxxx.xxxxxxxxxxx) { xxx (xxx x = x; x < xxxxxx.xxxxxx; x += x) { xxx = xxxxxx[x] * x.xxx; xxxxxx[x] = (xxxx)xxx; } } } xxxxxxx xxxxxx xxxxxx xxxxxxxxxxxxxxxxx( xxxxxx xxxxxxxxxxxx, xxxxxx[,] xxxxxxxxxxxx) { xxx xxxxxxxxxxxxxxx = xxx xxxxxxxxx( x, x, xxxxxxxxxxxx.xxxxx, xxxxxxxxxxxx.xxxxxx); xxxxxxxxxx xxxxxxxxxx = xxxxxxxxxxxx.xxxxxxxx(xxxxxxxxxxxxxxx); // […] xx (xxxxxxxxxxxx.xxxxxxxxxxx) { xxx (xxx x = x; x < xxxxxx.xxxxxx; x += x) { xxx = xxxxxx[x] * x.xxx; xxxxxx[x] = (xxxx)xxx; } } }
  • 18.  Lay down some foundations  Common expectations  Rules that the environment and the tools enforce  Rules that code reviews should enforce
  • 19.  Rules for:  Documentation  File layout  Maintainability  Naming  Ordering  Readability  Spacing
  • 20. That are not merely stylistic in approach…
  • 21.
  • 22.  The title says it all.   Recommendation:  The best method is a method that has no arguments (niladic)  One argument (monadic) or two (dyadic) is acceptable  Three arguments (triadic) should be avoided whenever possible  More than that (polyadic) require a rather good justification
  • 23.  Move things to instance variables!  Separate functions (initialization, ctor, etc.)  Rearrange classes (are many of your methods accepting the same class as argument? It is possible that those methods belong to that class!)  Avoid out parameters  Avoid flag arguments
  • 24.  The title says it all – again.  Recommendation: keep it low. Anything above 8 is a serious overkill, anything above 5 is potentially too many.
  • 25.  Guess what.  No overload is 1.  Recommendation: keep it low. Many overloads can be difficult to keep in mind. However, there are no hard rules for this.
  • 26. CC = 1 + … the number of any of the following expressions found in the method body:  if/case/default/?:/??/.?  while/for/foreach/continue  goto  &&/||  catch
  • 27.  Recommendations: The smaller the better. Static analysis tools recommend a max of 15 per method.  I’ve heard of companies where anything over 8 automatically breaks the build.
  • 28.  In case of .net it is generally lines of IL code (Logical LOC)  Code style (and language) does not influence it  Interfaces, abstract methods, enumerations have a LOC of 0.  Namespaces, namespace references, and declarations have a LOC of 0.  LOC of classes is the sum of the LOC of their methods.  Recommendation: keep as low as possible
  • 29.  Simple as it sounds, the number of the lines containing comment  Inline comments and full-line comments are worth the same  Recommendation: keep it as low as possible
  • 30.  (100 * LOComments) / (LOComments + LOC)  The amount of required comment strongly depends on the quality of code  Code should be self-explanatory and should not require comments  Recommendation: keep as low as possible
  • 31.  Percentage of code covered by (unit/acceptance) tests.  Recommendation: The closer to 100% the better.  If using test first, anything below 100% is pretty bad.
  • 32. … and methods, in some cases.
  • 33. Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general the more variables a method manipulates the more cohesive that method is to its class. A class in which each variable is used by each method is maximally cohesive.
  • 34. The strategy of keeping functions small and keeping parameter lists short can sometimes lead to a proliferation of instance variables that are used by a subset of methods. When this happens, it almost always means that there is at least one other class trying to get out of the larger class. You should try to separate the variables and methods into two or more classes such that the new classes are more cohesive.
  • 35. M is the number of methods in class (both static and instance methods are counted, it includes also constructors, properties getters/setters, events add/remove methods). F is the number of instance fields in the class. MF is the number of methods of the class accessing a particular instance field. Sum(MF) is the sum of MF over all instance fields of the class. LCOM = 1 – (sum(MF)/M*F) LCOM HS = (M – sum(MF)/F)(M-1)
  • 36. DI: Incoming dependencies: the number of classes that depend on this class DO: Outgoing dependencies: the number of classes that this class depends on. Instability: DO / (DI + DO) (0..1) I = 0: no other components depend on this component. I = 1: this component depends on no other components.
  • 37. NC: the number of classes in the assembly NA: the number of abstract classes in the assembly A: Abstractness: A = NA / NC
  • 38.  Zone of Uselessness: Abstract components that are never inherited  Zone of Pain: very stable, thus difficult to change, meanwhile very concrete, thus difficult to extend
  • 39. D = |A + I - 1 | Recommendation: Classes should be as close to the main sequence as possible.
  • 40.  Methods  Fields  Recommendation: keep classes small.
  • 41. For a class/namespace/assembly/etc.: Sum of CC from methods/classes/assemblies.
  • 42.  The title (almost) says it all.   For derived classes, it also includes the interfaces implemented by the base class.  Recommendation: keep low. ;)
  • 43.  The title (almost) says it all (again).   (Including System.Object, so always starts from 1.)  Recommendation: keep low. ;)
  • 44.
  • 46. Average number of internal relationships per type. Let R be the number of type relationships that are internal to this assembly (i.e. that do not connect to types outside the assembly). Let N be the number of types within the assembly. H = (R + 1)/ N. Recommendation: Keep it high.
  • 47. The things that my machine cannot do for me
  • 48.  SOLID principles  Consistent style  Succinct but readable code  Good naming
  • 49.  The Boy Scout Rule and Opportunistic refactoring
  • 50. Code metrics in Visual Studio: https://msdn.microsoft.com/en-us/library/bb385914.aspx A Short Primer on Code Quality Metrics: https://ardalis.com/static-code-analysis-and-quality-metrics NDepend Code Metrics: https://www.ndepend.com/docs/code-metrics