SlideShare a Scribd company logo
1 of 31
Download to read offline
Top Ten Metrics
10 print “hello EKON”;
20 Goto 10;

“Metrics measure the design of code after it
has been written”




                                               1
Agenda EKON
• What are Metrics ?
• How to recognize Bad Code ?
• Top Ten (The Law of Demeter)
• Improve your Code (Metric Based Refactoring)
• Optimisation and other Tools




                                                 2
What are Metrics?
Metrics are for
• Evaluate Object Complexity
• Quantify your code
• Highlight Redesign Needs
• Change Impact Analysis

        UEB: 15_pas_designbycontract.txt

                                           3
Metrics deal with
Bad Structure
•   General Code Size (in module)
•   Cohesion (in classes and inheritance)
•   Complexity
•   Coupling (between classes or units)
    • Cyclic Dependency, Declare+Definition, ACD-Metric
• Interfaces or Packages (design & runtime)
• Static, Public, Private (inheritance or delegate)

                 UEB: 10_pas_oodesign_solution.txt

                                                          4
Metric Context
                   Refactoring
                                            DUnit




                          change
                                      st
                                   te

                                      improve
                      Source                    Units
               ie w
          R ev
                    its
                A ud
Metrics




                                                        5
Some Kind of wonderful ?
• statusbar1.simpletext
  • simplepanel:= true!
• TLinarBitmap = TLinearBitmap; //Spelling bug
• aus Win32.VCL.Dialogs.pas
  • WndProcPtrAtom: TAtom = 0;
• aus indy: self.sender!
  • procedure TIdMessageSender_W(Self: TIdMessage;
    const T: TIdEmailAddressItem);
  • begin Self.Sender := T; end;

                UEB: 8_pas_verwechselt.txt

                                                     6
When and why Metrics ?
After a Code Review
By changes of a release
Redesign with UML (Patterns or Profiles)
Law of Demeter not passed
Bad Testability (FAT or SAT)
• Work on little steps at a time
• Modify not only structure but also code format



                                                   7
What‘s Bad Code
Bad Coordination
• Inconsistence with Threads
• Access on objects with Null Pointer
• Bad Open/Close of Input/Output Streams or
  I/O Connections
• Check return values or idempotence
• Check break /exit in loops
• Modification of static or const code
• Access of constructor on non initialized vars


                                                  8
Metric/Review Checklist
1. Standards - are the Pascal software standards for
   name conventions being followed?
2. Are all program headers completed?
3. Are changes commented appropriately?
4. Are release notes Clear? Complete?
5. Installation Issues, Licenses, Certs. Are there any?
6. Version Control, Are output products clear?
7. Test Instructions - Are they any? Complete?
8.   "Die andere Seite, sehr dunkel sie ist" - "Yoda, halt's Maul und iß
     Deinen Toast!"




                                                                           9
Top Ten Metrics
1. VOD Violation of Law of Demeter
2. Halstead NOpmd (Operands/Operators)
3. DAC (Data Abstraction Coupling)(Too many
   responsibilities or references in the field)
4. CC (Complexity Report), McCabe cyclomatic
   complexity, Decision Points)
5. CBO (Coupling between Objects) Modularity




                                                  10
Top Ten II
6. PUR (Package Usage Ratio) access information
   in a package from outside
7. DD Dependency Dispersion (SS, Shotgun Surgery
   (Little changes distributed over too many
   objects or procedures      patterns missed))
8. CR Comment Relation
9. MDC (Module Design Complexity (Class with too
   many delegating methods)
10. NORM      (remote methods called (Missing
   polymorphism))


                                                   11
Law of Demeter
You should avoid:
• Large classes with strange members
•Das Gesetz von Demeter (don‘t talk to
strangers) besagt, dass ein Objekt O als
Reaktion auf eine Nachricht m, weitere
Nachrichten nur an die folgenden Objekte
senden sollte: (all objects to which m sends a
message must be instances of classes)


                                                 12
Demeter konkret
1. [M1] an Objekt O selbst
   Bsp.: self.initChart(vdata);
2. [M2] an Objekte, die als Parameter in der Nachricht
   m vorkommen
   Bsp.: O.acceptmemberVisitor(visitor)
         visitor.visitElementChartData;
3. [M3] an Objekte, die O als Reaktion auf m erstellt
   Bsp.: visitor:= TChartVisitor.create(cData, madata);
4. [M4] an Objekte, auf die O direkt mit einem Member
   zugreifen kann
   Bsp.: O.Ctnr:= visitor.TotalStatistic



                                                          13
Demeter Test as SEQ




        UEB: 56_pas_demeter.txt

                                  14
DAC or Modules of Classes
Large classes with to many references
• More than seven or eight variables
• More than fifty methods
• You probably need to break up the class in
      Components (Strategy, Composite, Decorator)
TWebModule1 = class(TWebModule)
  HTTPSoapDispatcher1: THTTPSoapDispatcher;
  HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
  WSDLHTMLPublish1: TWSDLHTMLPublish;
  DataSetTableProducer1: TDataSetTableProducer;




                                                    15
CC
• Check Complexity
function IsInteger(TestThis: String): Boolean;
begin
 try
   StrToInt(TestThis);
 except
   on EConvertError do
    result:= False;
 else
   result:= True;
 end;
end;




                                                 16
CBO I
CBO measures the number of classes to which a class is
coupled. According to remarks and comments on CBO and
coupling, we include coupling through inheritance.

Two classes are considered coupled, if methods declared in
one class call methods or access attributes defined in the
other class.




                                                         17
CBO II
Are Business Objects available for good CBO (Extensions)?
In a simple business object (without fields in the class),
you do have at least 4 tasks to fulfil:

1. The Business-Class inherits from a Data-Provider
2. The query is part of the class
3. A calculation or business-rule has to be done
4. The object is independent from the GUI, GUI calls the
   object “Business objects are sometimes referred to as conceptual objects,
                because they provide services which meet business requirements,
                                                     regardless of technology”.



                                                                               18
PUR Package Usage Ratio




                          19
Welche Metric?                    DD
• Dependency Dispersion (Code too much Distributed):
  …..
  for i:= 0 to SourceTable.FieldCount - 1 do
     DestTable.Fields[i].Assign(SourceTable.Fields[i]);
   DestTable.Post;
  …..




                                                          20
DD – use small procedures
Procedure CopyRecord(const SourceTable, DestTable:
                                               TTable);
var i: Word;
  begin
   DestTable.Append;
   For i:= 0 to SourceTable.FieldCount - 1 do
     DestTable.Fields[i].Assign(SourceTable.Fields[i]);
   DestTable.Post;
  end;




                                                          21
Finally you can measure:
Bad Naming (no naming convention)
Duplicated Code (side effects)
Long Methods (to much code)
Temporary Fields (confusion)
Long Parameter List (Object is missing)
Data Classes (no methods)
  • Large Class with too many delegating methods
In a Kiviat Chart you get a Best Practices Circle!

                UEB: 33_pas_cipher_file_1.txt

                                                     22
Why is Refactoring important?
• Only defense against software decay.
• Often needed to fix reusability bugs.
• Lets you add patterns or templates after you
  have written a program;
• Lets you transform program into framework.
• Estimation of the value (capital) of code!
• Necessary for beautiful software.



                                                 23
Refactoring Process
The act of serialize the process:
 Build unit test
 Refactor and test the code (iterative!)
 Check with Pascal Analyzer or another tool
 Building the code
 Running all unit tests
 Generating the documentation
 Deploying to a target machine
 Performing a “smoke test” (just compile)


                                              24
Let‘s practice
•   1
•   11
•   21
•   1211
•   111221
•   312211
•   ??? Try to find the next pattern, look for
    a rule or logic behind !


                                                 25
function runString(Vshow: string): string;
var i: byte;
Rword, tmpStr: string;
cntr, nCount: integer;
begin
cntr:=1; nCount:=0;
                         Before R.
Rword:=''; //initialize
tmpStr:=Vshow; // input last result
for i:= 1 to length(tmpStr) do begin
 if i= length(tmpstr) then begin
    if (tmpStr[i-1]=tmpStr[i]) then cntr:= cntr +1;
    if cntr = 1 then nCount:= cntr
    Rword:= Rword + intToStr(ncount) + tmpStr[i]
 end else
  if (tmpStr[i]=tmpStr[i+1]) then begin
     cntr:= cntr +1;
     nCount:= cntr;
  end else begin
    if cntr = 1 then cntr:=1 else cntr:=1; //reinit counter!
    Rword:= Rword + intToStr(ncount) + tmpStr[i] //+ last char(tmpStr)
end;
end; // end for loop
result:=Rword;
end;                              UEB: 9_pas_umlrunner.txt

                                                                         26
After R.
function charCounter(instr: string): string;
var i, cntr: integer; Rword: string;
begin
cntr:= 1;
Rword:=' ';
  for i:= 1 to length(instr) do begin
   //last number in line
   if i= length(instr) then
     concatChars()
   else
   if (instr[i]=instr[i+1]) then cntr:= cntr +1
   else begin
     concatChars()
     //reinit counter!
     cntr:= 1;
   end;
  end; //for
 result:= Rword;
end;

                          UEB: 12_pas_umlrunner_solution.txt

                                                               27
Refactoring Techniken
Einheit       Refactoring Funktion   Beschreibung
Package       Rename Package         Umbenennen eines Packages
Class         Move Method            Verschieben einer Methode
Class         Extract Superclass     Aus Methoden, Eigenschaften eine
                                        Oberklasse erzeugen und verwenden
Class         Introduce Parameter    Ersetzen eines Ausdrucks durch einen
                                         Methodenparameter
Class         Extract Method         Heraustrennen einer Codepassage
Interface     Extract Interface      Aus Methoden ein Interface erzeugen
Interface     Use Interface          Erzeuge Referenzen auf Klasse

Component     Replace Inheritance    Ersetze vererbte Methoden durch
                 with Delegation         Delegation in innere Klasse
Class         Encapsulate Fields     Getter- und Setter einbauen
Modell        Safe Delete            Löschen einer Klasse mit Referenzen




                                                                            28
Metric based Refactoring
:ExtractMethod(EM)-MoveMethod(MM)-DataObject(DO)-ExtractClass(EC)
                                                EM MM DO EC
•   Normalized Cohesion                         WB B B
•   Non-normalized Cohesion                     WB B B
•   General Coupling                            E B N S
•   Export Coupling                             E B E E
•   Aggregated import coupling                  B WWW
    • Best, Worst, Expected, Suboptimal




                                                                    29
Audits & Metric Links:
•   Delphi XE Tool: Together

•   http://www.modelmakertools.com/
•   Report Pascal Analyzer:
    http://www.softwareschule.ch/download/pascal_analyzer.pdf
•   Refactoring Martin Fowler (1999, Addison-Wesley)
•   http://c2.com/cgi/wiki?CodeSmell
•   Model View in Together:
    www.softwareschule.ch/download/delphi2007_modelview.pdf




                                                                30
Q&A
  max@kleiner.com
www.softwareschule.ch




                        31

More Related Content

What's hot

An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...David Beazley (Dabeaz LLC)
 
Virtual platform
Virtual platformVirtual platform
Virtual platformsean chen
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingcppfrug
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? DVClub
 
Address/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerAddress/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerPlatonov Sergey
 
Async await in C++
Async await in C++Async await in C++
Async await in C++cppfrug
 
Processor Verification Using Open Source Tools and the GCC Regression Test Suite
Processor Verification Using Open Source Tools and the GCC Regression Test SuiteProcessor Verification Using Open Source Tools and the GCC Regression Test Suite
Processor Verification Using Open Source Tools and the GCC Regression Test SuiteDVClub
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsDavid Beazley (Dabeaz LLC)
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonYi-Lung Tsai
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...Muhammad Ulhaque
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...Tsundere Chen
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
Intel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionIntel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionPVS-Studio
 

What's hot (20)

An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogramming
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me?
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Address/Thread/Memory Sanitizer
Address/Thread/Memory SanitizerAddress/Thread/Memory Sanitizer
Address/Thread/Memory Sanitizer
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
Processor Verification Using Open Source Tools and the GCC Regression Test Suite
Processor Verification Using Open Source Tools and the GCC Regression Test SuiteProcessor Verification Using Open Source Tools and the GCC Regression Test Suite
Processor Verification Using Open Source Tools and the GCC Regression Test Suite
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
 
TensorFlow XLA RPC
TensorFlow XLA RPCTensorFlow XLA RPC
TensorFlow XLA RPC
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Intel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correctionIntel IPP Samples for Windows - error correction
Intel IPP Samples for Windows - error correction
 

Similar to Metrics ekon 14_2_kleiner

EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4Max Kleiner
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...Antonio de la Torre Fernández
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Conflict-Free Replicated Data Types (PyCon 2022)
Conflict-Free Replicated Data Types (PyCon 2022)Conflict-Free Replicated Data Types (PyCon 2022)
Conflict-Free Replicated Data Types (PyCon 2022)Rebecca Bilbro
 
24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdfFrangoCamila
 
Lessons learned validating 60,000 pages of api documentation
Lessons learned validating 60,000 pages of api documentationLessons learned validating 60,000 pages of api documentation
Lessons learned validating 60,000 pages of api documentationBob Binder
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The EnterpriseDaniel Egan
 
C interview questions
C interview questionsC interview questions
C interview questionsSoba Arjun
 

Similar to Metrics ekon 14_2_kleiner (20)

EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Clean Code V2
Clean Code V2Clean Code V2
Clean Code V2
 
Conflict-Free Replicated Data Types (PyCon 2022)
Conflict-Free Replicated Data Types (PyCon 2022)Conflict-Free Replicated Data Types (PyCon 2022)
Conflict-Free Replicated Data Types (PyCon 2022)
 
24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf24-02-18 Rejender pratap.pdf
24-02-18 Rejender pratap.pdf
 
Lessons learned validating 60,000 pages of api documentation
Lessons learned validating 60,000 pages of api documentationLessons learned validating 60,000 pages of api documentation
Lessons learned validating 60,000 pages of api documentation
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Csharp
CsharpCsharp
Csharp
 
C interview questions
C interview questionsC interview questions
C interview questions
 

More from Max Kleiner

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfMax Kleiner
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfMax Kleiner
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementMax Kleiner
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87Max Kleiner
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapMax Kleiner
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detectionMax Kleiner
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationMax Kleiner
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_editionMax Kleiner
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingMax Kleiner
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP Max Kleiner
 
EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures CodingMax Kleiner
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70Max Kleiner
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIIMax Kleiner
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VIMax Kleiner
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning VMax Kleiner
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3Max Kleiner
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsMax Kleiner
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Max Kleiner
 
EKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningEKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningMax Kleiner
 
TensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningTensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningMax Kleiner
 

More from Max Kleiner (20)

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdf
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdf
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_Implement
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmap
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detection
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data Visualisation
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_edition
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage coding
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP
 
EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures Coding
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning V
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_Diagrams
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2
 
EKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningEKON22 Introduction to Machinelearning
EKON22 Introduction to Machinelearning
 
TensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningTensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 Machinelearning
 

Metrics ekon 14_2_kleiner

  • 1. Top Ten Metrics 10 print “hello EKON”; 20 Goto 10; “Metrics measure the design of code after it has been written” 1
  • 2. Agenda EKON • What are Metrics ? • How to recognize Bad Code ? • Top Ten (The Law of Demeter) • Improve your Code (Metric Based Refactoring) • Optimisation and other Tools 2
  • 3. What are Metrics? Metrics are for • Evaluate Object Complexity • Quantify your code • Highlight Redesign Needs • Change Impact Analysis UEB: 15_pas_designbycontract.txt 3
  • 4. Metrics deal with Bad Structure • General Code Size (in module) • Cohesion (in classes and inheritance) • Complexity • Coupling (between classes or units) • Cyclic Dependency, Declare+Definition, ACD-Metric • Interfaces or Packages (design & runtime) • Static, Public, Private (inheritance or delegate) UEB: 10_pas_oodesign_solution.txt 4
  • 5. Metric Context Refactoring DUnit change st te improve Source Units ie w R ev its A ud Metrics 5
  • 6. Some Kind of wonderful ? • statusbar1.simpletext • simplepanel:= true! • TLinarBitmap = TLinearBitmap; //Spelling bug • aus Win32.VCL.Dialogs.pas • WndProcPtrAtom: TAtom = 0; • aus indy: self.sender! • procedure TIdMessageSender_W(Self: TIdMessage; const T: TIdEmailAddressItem); • begin Self.Sender := T; end; UEB: 8_pas_verwechselt.txt 6
  • 7. When and why Metrics ? After a Code Review By changes of a release Redesign with UML (Patterns or Profiles) Law of Demeter not passed Bad Testability (FAT or SAT) • Work on little steps at a time • Modify not only structure but also code format 7
  • 8. What‘s Bad Code Bad Coordination • Inconsistence with Threads • Access on objects with Null Pointer • Bad Open/Close of Input/Output Streams or I/O Connections • Check return values or idempotence • Check break /exit in loops • Modification of static or const code • Access of constructor on non initialized vars 8
  • 9. Metric/Review Checklist 1. Standards - are the Pascal software standards for name conventions being followed? 2. Are all program headers completed? 3. Are changes commented appropriately? 4. Are release notes Clear? Complete? 5. Installation Issues, Licenses, Certs. Are there any? 6. Version Control, Are output products clear? 7. Test Instructions - Are they any? Complete? 8. "Die andere Seite, sehr dunkel sie ist" - "Yoda, halt's Maul und iß Deinen Toast!" 9
  • 10. Top Ten Metrics 1. VOD Violation of Law of Demeter 2. Halstead NOpmd (Operands/Operators) 3. DAC (Data Abstraction Coupling)(Too many responsibilities or references in the field) 4. CC (Complexity Report), McCabe cyclomatic complexity, Decision Points) 5. CBO (Coupling between Objects) Modularity 10
  • 11. Top Ten II 6. PUR (Package Usage Ratio) access information in a package from outside 7. DD Dependency Dispersion (SS, Shotgun Surgery (Little changes distributed over too many objects or procedures patterns missed)) 8. CR Comment Relation 9. MDC (Module Design Complexity (Class with too many delegating methods) 10. NORM (remote methods called (Missing polymorphism)) 11
  • 12. Law of Demeter You should avoid: • Large classes with strange members •Das Gesetz von Demeter (don‘t talk to strangers) besagt, dass ein Objekt O als Reaktion auf eine Nachricht m, weitere Nachrichten nur an die folgenden Objekte senden sollte: (all objects to which m sends a message must be instances of classes) 12
  • 13. Demeter konkret 1. [M1] an Objekt O selbst Bsp.: self.initChart(vdata); 2. [M2] an Objekte, die als Parameter in der Nachricht m vorkommen Bsp.: O.acceptmemberVisitor(visitor) visitor.visitElementChartData; 3. [M3] an Objekte, die O als Reaktion auf m erstellt Bsp.: visitor:= TChartVisitor.create(cData, madata); 4. [M4] an Objekte, auf die O direkt mit einem Member zugreifen kann Bsp.: O.Ctnr:= visitor.TotalStatistic 13
  • 14. Demeter Test as SEQ UEB: 56_pas_demeter.txt 14
  • 15. DAC or Modules of Classes Large classes with to many references • More than seven or eight variables • More than fifty methods • You probably need to break up the class in Components (Strategy, Composite, Decorator) TWebModule1 = class(TWebModule) HTTPSoapDispatcher1: THTTPSoapDispatcher; HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker; WSDLHTMLPublish1: TWSDLHTMLPublish; DataSetTableProducer1: TDataSetTableProducer; 15
  • 16. CC • Check Complexity function IsInteger(TestThis: String): Boolean; begin try StrToInt(TestThis); except on EConvertError do result:= False; else result:= True; end; end; 16
  • 17. CBO I CBO measures the number of classes to which a class is coupled. According to remarks and comments on CBO and coupling, we include coupling through inheritance. Two classes are considered coupled, if methods declared in one class call methods or access attributes defined in the other class. 17
  • 18. CBO II Are Business Objects available for good CBO (Extensions)? In a simple business object (without fields in the class), you do have at least 4 tasks to fulfil: 1. The Business-Class inherits from a Data-Provider 2. The query is part of the class 3. A calculation or business-rule has to be done 4. The object is independent from the GUI, GUI calls the object “Business objects are sometimes referred to as conceptual objects, because they provide services which meet business requirements, regardless of technology”. 18
  • 19. PUR Package Usage Ratio 19
  • 20. Welche Metric? DD • Dependency Dispersion (Code too much Distributed): ….. for i:= 0 to SourceTable.FieldCount - 1 do DestTable.Fields[i].Assign(SourceTable.Fields[i]); DestTable.Post; ….. 20
  • 21. DD – use small procedures Procedure CopyRecord(const SourceTable, DestTable: TTable); var i: Word; begin DestTable.Append; For i:= 0 to SourceTable.FieldCount - 1 do DestTable.Fields[i].Assign(SourceTable.Fields[i]); DestTable.Post; end; 21
  • 22. Finally you can measure: Bad Naming (no naming convention) Duplicated Code (side effects) Long Methods (to much code) Temporary Fields (confusion) Long Parameter List (Object is missing) Data Classes (no methods) • Large Class with too many delegating methods In a Kiviat Chart you get a Best Practices Circle! UEB: 33_pas_cipher_file_1.txt 22
  • 23. Why is Refactoring important? • Only defense against software decay. • Often needed to fix reusability bugs. • Lets you add patterns or templates after you have written a program; • Lets you transform program into framework. • Estimation of the value (capital) of code! • Necessary for beautiful software. 23
  • 24. Refactoring Process The act of serialize the process: Build unit test Refactor and test the code (iterative!) Check with Pascal Analyzer or another tool Building the code Running all unit tests Generating the documentation Deploying to a target machine Performing a “smoke test” (just compile) 24
  • 25. Let‘s practice • 1 • 11 • 21 • 1211 • 111221 • 312211 • ??? Try to find the next pattern, look for a rule or logic behind ! 25
  • 26. function runString(Vshow: string): string; var i: byte; Rword, tmpStr: string; cntr, nCount: integer; begin cntr:=1; nCount:=0; Before R. Rword:=''; //initialize tmpStr:=Vshow; // input last result for i:= 1 to length(tmpStr) do begin if i= length(tmpstr) then begin if (tmpStr[i-1]=tmpStr[i]) then cntr:= cntr +1; if cntr = 1 then nCount:= cntr Rword:= Rword + intToStr(ncount) + tmpStr[i] end else if (tmpStr[i]=tmpStr[i+1]) then begin cntr:= cntr +1; nCount:= cntr; end else begin if cntr = 1 then cntr:=1 else cntr:=1; //reinit counter! Rword:= Rword + intToStr(ncount) + tmpStr[i] //+ last char(tmpStr) end; end; // end for loop result:=Rword; end; UEB: 9_pas_umlrunner.txt 26
  • 27. After R. function charCounter(instr: string): string; var i, cntr: integer; Rword: string; begin cntr:= 1; Rword:=' '; for i:= 1 to length(instr) do begin //last number in line if i= length(instr) then concatChars() else if (instr[i]=instr[i+1]) then cntr:= cntr +1 else begin concatChars() //reinit counter! cntr:= 1; end; end; //for result:= Rword; end; UEB: 12_pas_umlrunner_solution.txt 27
  • 28. Refactoring Techniken Einheit Refactoring Funktion Beschreibung Package Rename Package Umbenennen eines Packages Class Move Method Verschieben einer Methode Class Extract Superclass Aus Methoden, Eigenschaften eine Oberklasse erzeugen und verwenden Class Introduce Parameter Ersetzen eines Ausdrucks durch einen Methodenparameter Class Extract Method Heraustrennen einer Codepassage Interface Extract Interface Aus Methoden ein Interface erzeugen Interface Use Interface Erzeuge Referenzen auf Klasse Component Replace Inheritance Ersetze vererbte Methoden durch with Delegation Delegation in innere Klasse Class Encapsulate Fields Getter- und Setter einbauen Modell Safe Delete Löschen einer Klasse mit Referenzen 28
  • 29. Metric based Refactoring :ExtractMethod(EM)-MoveMethod(MM)-DataObject(DO)-ExtractClass(EC) EM MM DO EC • Normalized Cohesion WB B B • Non-normalized Cohesion WB B B • General Coupling E B N S • Export Coupling E B E E • Aggregated import coupling B WWW • Best, Worst, Expected, Suboptimal 29
  • 30. Audits & Metric Links: • Delphi XE Tool: Together • http://www.modelmakertools.com/ • Report Pascal Analyzer: http://www.softwareschule.ch/download/pascal_analyzer.pdf • Refactoring Martin Fowler (1999, Addison-Wesley) • http://c2.com/cgi/wiki?CodeSmell • Model View in Together: www.softwareschule.ch/download/delphi2007_modelview.pdf 30