SlideShare a Scribd company logo
1 of 66
Download to read offline
1/66
Refactoring mit Delphi
2007 in Rosenheim
Als Refactoring betrachtet man allgemein ein
Vereinfachen, Verbessern und Stabilisieren einer
bestehenden Codestruktur, welche keine Änderung
auf das „beobachtbare“ Verhalten der Applikation
und dessen Ergonomie bewirken soll.
“Refactoring is improving the design of code after it
has been written”
2/66
Agenda 10.10.2008
• What‘s Refactoring ?
• How to recognize Bad Code and what‘s the goal ?
• The Law of Demeter
• Process of Refactoring
• Techniques in Delphi 2007
• Introduction to DUnit
• Experiences, Code Analysis and Optimisations
• Refactoring and other Tools
3/66
Refactoring Context
Advantage: automatisation and integration of
new methods, better testability and
readability (simplification)
SourceSource
improveimprove
TestTest
DUnitDUnit
test
test
RefactoringRefactoring
changechange
UnitsUnits
text
text
binary
binary
4/66
Course Preparation
Tool: softwareschule.ch/maxbox.htm
SourceSource
improveimprove
examplesexamples
TogetherTogether
check
check
maXboxmaXbox
changechange
Delphi 2007Delphi 2007
text
text
binary
binary
5/66
Refactoring: What‘s all
about ?
Never touch a running system ?!:
//System.Get just reads the contents of memory locations:
Get(a, v); or v:= M[a];
o.IntToStr; or v:= IntToStr(o);
GetMem(p, Count * SizeOf(Pointer)); or GetMem(FDataPtr, MemSize);
If you can‘t see how to easily expand, test or
maintain your code, it‘s probably time to
refactor!
- How to recognize bad smells ?
- Refactor to change code, reduce to the Max!
- Build Test Class and run test to confirm it‘s okay
6/66
Refactoring: Definition
• Changes made to a program that only change
its organization (structure), not its function.
• Behaviour preserving program
transformations.
• Simplification: Your classes are small and
your methods too; you’ve said everything and
you’ve removed the last piece of unnecessary
code.
7/66
When and why Refactoring ?
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
• Test after each step
• Each method should do one thing
• Modify not only structure but also code format
8/66
When to refactor ?
• If a new feature seems hard to
implement, refactor.
• If a new or changed feature created some
ugly code, refactor.
• When you can’t stand (understand) to
look at your code, refactor.
UEB: 8_pas_verwechselt.txt
9/66
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.
• Lets you worry about generality tomorrow;
• Estimation of the value (capital) of code!
• Necessary for beautiful software.
10/66
When Refactoring I ?
Bad Coordination
• Single Instance with Singleton
• 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
UEB: 14_pas_primetest.txt
11/66
When Refactoring II ?
Bad Functionality (Organisation)
• General Code Smoke Test (compilation) failed
• Comments, Coding Conventions (D. by Contract)
• Type safety (pointers?), Declare, RTTI, Thread Safeness
• Exception Handling, Resource Leaks (memory)
• Import Statements (uses at runtime)
• Functions and Bindings (design time)
• No Bug Fixes & Release Planning
• No Versioning & System Handbook
UEB: 15_pas_designbycontract.txt
12/66
When Refactoring III ?
Bad Structure
• General Code Size (in module)
• Coupling (between classes or units)
• Cyclic Dependency, Declare+Definition, ACD-Metric
• Cohesion (in classes)
• Layers, Tiers and Control Structures (runtime)
• Interfaces or Packages (design & runtime)
• Static, Public or Private (inheritance or delegate)
• Too big classes or units?
UEB: 10_pas_oodesign_solution.txt
13/66
Top Ten of Bad Smells
1. Duplicated or Dead Code
2. Long Method
3. Large Class (Too many responsibilities)
4. Long Parameter List (Object is missing)
5. Case Statement (Missing polymorphism)
6. Divergent Change (Same class changes differently
depending on addition)
7. Shotgun Surgery (Little changes distributed over too many
objects or procedures patterns missed)
8. Data Clumps (Data always use together (x,y -> point))
9. Middle Man (Class with too many delegating methods)
10. Message Chains (Coupled classes, internal representation
dependencies)
14/66
Refctor/Review Checklist
1. Standards - are the Pascal software standards for
name conventions being followed?
2. Bugs - Are the changes generally correct?
3. Are the Requirements Well Understood (Multilang)?
4. Are all program headers completed?
5. Are changes commented appropriately?
6. Does documentation use Correct Grammar?
7. Are release notes Clear? Complete?
8. Installation Issues, Licenses, Certs. Are there any?
9. Version Control, Are output products clear?
10.Test Instructions - Are they any? Complete?
15/66
Precondition & Goals…
• Dokus, Testklassen zu Algorithmen
• Vorabversionen und Updates
• Installation, Support, Hotline der Firma
• Volle Verfügbarkeit über den Sourcecode
• Compilationsfähigkeit erstellt ($D, $L, tdb32info)
• Benchmarks als Performancekriterium
• Know-how Transfer durch Entwickler
• Tools und Review Kriterien vorhanden
• Sprache und Form bei Dokumenten festlegen
• Referenzarchitektur und Patterns bekannt
16/66
Modularisierung
• Patterns Suche, Test mit einem Dependency Analyzer
Suchen nach einem Schichtenmodell (MVC etc.)
• Ist das Programmdesign optimal gewählt?
– Zu viele globale Variablen?
– Schlecht aufgeteilte Methoden bzw. Klassen?
– Redundante Codesequenzen?
Modularisierung (zur design- oder runtime ?)
TWebModule1 = class(TWebModule)
HTTPSoapDispatcher1: THTTPSoapDispatcher;
HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
WSDLHTMLPublish1: TWSDLHTMLPublish;
DataSetTableProducer1: TDataSetTableProducer;
17/66
Modules of Classes
Large classes
• More than seven or eight variables
• More than fifty methods
• You probably need to break up the class
Components (Strategy, Composite, Decorator)
Inheritance
TWebModule1 = class(TWebModule)
HTTPSoapDispatcher1: THTTPSoapDispatcher;
HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker;
WSDLHTMLPublish1: TWSDLHTMLPublish;
DataSetTableProducer1: TDataSetTableProducer;
18/66
Extendibility
Are Interfaces, Packages available?
type
{ Invokable interfaces must derive from IInvokable }
IVCLScanner = interface(IInvokable)
['{8FFBAA56-B4C2-4A32-924D-B3D3DE2C4EFF}']
function PostData(const UserData : WideString; const
CheckSum : DWORD) : Boolean; stdcall;
procedure PostUser(const Email, FirstName,
LastName : WideString); stdcall;
19/66
Extendibility II
Are Business Objects available (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”.
20/66
Modules and Extendibility
Are Factory Patterns available ?
Shows the use of a simple factory method to generate
various reports or forms:
1. Register a list with objects
2. Generate objects at runtime
3. A report or business-rule has to be done
4. The objectlist is independent from GUI, GUI calls the
list and the list is expandable
“Use collections or lists, not case of or if statements.”
UEB: 55_pas_factorylist.txt
21/66
Wartbarkeit
• Namenskonvention, Namensräume, Kommentare
• Bridge zwischen Interface und Implementation (siehe
auch Modularisierung)
• Installation und Configuration Guide durchsehen
• Abhängigkeiten der Module klären:
for i:= 0 to SourceTable.FieldCount - 1 do
DestTable.Fields[i].Assign(SourceTable.Fields[i]);
DestTable.Post;
22/66
Wartbarkeit II
• die Dokumentation, insbesondere die exakte
Spezifikation von Schnittstellen (Interfaces)
• die lokale Verständlichkeit von Anweisungen resp.
von Kommentar im Code
• das Vermeiden von Duplicated, Dead Code
• das Vermeiden globaler Variablen
• die Parametrisierbarkeit von Methoden
• in das Programm eingebaute Prüfungen der
Annahmen, die man ßber Zustände hat (Assertions)
• ein möglichst großer Umfang von automatisch
ausfĂźhrbaren Tests fĂźr das System
23/66
Wartbarkeit III
• die Dokumentation, insbesondere die
Verständlichkeit von Modulen (Komponenten)
Program MailingLabels //pseudo code referenzieren!
Begin
ask the user for the name of the list
read that list into memory
ask the user how the list should be sorted: name or zip code
sort the list
ask the user about where to send the output: screen or printer
print the labels
End. { Program MailingLabels }
24/66
Testbarkeit (White Box)
• Es lassen sich beim Ändern einer Subroutine
schnell mal die davon abhängigen Prozeduren
aufzeigen (z.B. Bindings Report).
Die Änderung einer Funktion hat Einfluß auf die davon abhängigen
Funktionen, die im Bindings Report aufgelistet werden, demzufolge ein
erneuter Test der Funktion der Qualität nur zugute kommt.
• Viele haben ja ein eigenes, „automatisiertes“
Verfahren entwickelt, um Code auf Fehler hin
zu durchsuchen (Testprotokolle).
• Wurde das Error Handling, Logger getestet ?
• Sind Testklassen wie DUnit im Einsatz?
25/66
Testability – We should
avoid:
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
• Class with too many delegating methods
• Coupled classes UEB: 33_pas_cipher_file_1.txt
26/66
Software Safeness I
• Avoid pointers as you can
• Ex. of the win32API:
pVinfo = ^TVinfo;
function TForm1.getvolInfo(const aDrive: pchar; info:
pVinfo): boolean;
// refactoring from pointer to reference
function TReviews.getvolInfo(const aDrive: pchar; var info:
TVinfo): boolean;
“Each pointer or reference should be checked to see if it is
null. An error or an exception should occur if a parameter is
invalid.”
27/66
Software Safeness II
• Check Exception Handling
function IsInteger(TestThis: String): Boolean;
begin
try
StrToInt(TestThis);
except
on EConvertError do
result:= False;
else
result:= True;
end;
end;
28/66
Fehleranalyse und Refactor
Relevanz
FehlerTyp-Nr / Bezeichnung / Verteilung / Relevant
10 Dokumentation - Kommentare, 0.5, Ja
20 Syntax - Syntax-Fehler (vor dem Kompilieren), 49.3, Nein
30 Build – package library, version control, 15.4, Ja
40 Assignment - Dekl., Doppelte Bezeichnungen, 8.8, Ja
50 Interface - Prozedur-Aufrufe und Referenzen, I/O, 1.5, Ja
60 Logging - Fehler-Nachrichten, Checks, 2.2, Ja
70 Daten - Struktur, Inhalt, Format, 1.5, Ja
80 Funktion - Schleifen, Rekursionen, Logik, 16.6, Ja/Nein
90 System – Konfiguration 1.5, Ja
100 Umgebung - Absturz, Compiler, Support, 3.7, Ja
29/66
Law of Demeter
You should avoid:
• Large classes with strange members
• More than seven or eight variables
• More than fifty methods
• You probably need to break up the class
Components in (Strategy, Composite, Decorator)
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:
30/66
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
31/66
Demeter Test as SEQ
UEB: 56_pas_demeter.txt
32/66
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)
33/66
Let‘s practice
• 1
• 11
• 21
• 1211
• 111221
• 312211
• ???
Try to find the next pattern, look for a rule or
logic behind !
34/66
Before R.
function runString(Vshow: string): string;
var i: byte;
Rword, tmpStr: string;
cntr, nCount: integer;
begin
cntr:=1; nCount:=0;
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
35/66
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
36/66
Testfunction
function teststring(vstring: string): integer;
var a, b, i: integer;
begin
for i:= 1 to length(vstring) do begin
a:= (ord(vstring[i]))
b:= b + a;
end
result:= b
end;
Check with a reference value:
if teststring(charCounter(RUN2)) = 578 then …
//CheckEquals(578, teststring(charCounter(RUN2)))
37/66
Refactoring Techniken
LĂśschen einer Klasse mit ReferenzenSafe DeleteModell
Getter- und Setter einbauenEncapsulate FieldsClass
Ersetze vererbte Methoden durch Delegation
in innere Klasse
Replace Inheritance with
Delegation
Component
Erzeuge Referenzen auf Klasse mit
Referenz auf implementierte Schnittstelle
Use InterfaceInterface
Aus Methoden ein Interface erzeugenExtract InterfaceInterface
Heraustrennen einer CodepassageExtract MethodClass
Ersetzen eines Ausdrucks durch einen
Methodenparameter
Introduce ParameterClass
Aus Methoden, Eigenschaften eine
Oberklasse erzeugen und verwenden
Extract SuperclassClass
Verschieben eines PackagesMove PackagePackage
Umbenennen eines PackagesRename PackagePackage
BeschreibungRefactoring FunktionEinheit
38/66
Consider the Options
The tool should provide options. Whenever you rename certain entities, a
tool should automatically replace all relevant identifiers in code in order
to keep code consistent
Each refactoring operation has its own set of
constraints!
• How to treat extracted code in original method – you can choose
between
• Leave – leaves the original text unchanged,activates the new method.
• Select – leaves the original text unchanged and selects it (does not
activate the new method).
• Comment – puts comment braces around the original text and activates
new method.
• Remove – removes the original code and activates new method.
• copy the vars to the new method,
• add the new class to the same unit as the source method's class (if any).
39/66
Constraints in Delphi
• If an error results from a refactoring, the engine cannot
apply the change. For example, you cannot rename an
identifier to a name that already exists in the same
declaration scope. If you still want to rename your
identifier, you need to rename the identifier that already
has the target name first, then refresh the refactoring.
• You can also redo the refactoring and select a new
name. The refactoring engine traverses parent scopes,
searching for an identifier with the same name. If the
engine finds an identifier with the same name, it issues
a warning.
40/66
Refactoring with D9
If you only select by install win32 refactor won't work!!
• Delphi 2005 provides following refactoring operations:
• Symbol Rename (Delphi, C#)
• Extract Method (Delphi)
• Declare Variable and Field (Delphi)
• Sync Edit Mode (Delphi, C#)
• Find References (Delphi, C#)
• Extract Resource string (Delphi)
• Find unit /import Namespace(Delphi)
• Undo (Delphi, C#)
• Examples…
41/66
Refactoring in D9 SP3
Corrections done
• When extracting methods involving DWords, they are
getting redeclared as Integers.
• When the parameter has the same name as the type,
'Find local Reference' and 'rename parameter' fail
• Rename refactoring fails on overloaded procedures
with at least one parameter of the same name.
• After using ExtractMethod, dragging a code snippet
from the Tools palette causes a stack overflow.
• Rename for components with event handlers doesn't
work for VCL and VCL.Net apps after you save the
project
42/66
Refactoring with D11
Delphi 2007 (since2006) provides following new refactoring operations:
• New! Introduce Variable refactoring d D #
New! Introduce Field refactoring d D #
• New! Inline Variable refactoring d D #
• New! Change Parameters refactoring d D #
• New! Safe Delete refactoring d D #
• New! Push Members Up / Down refactoring d D #
New! Pull Members Up refactoring d D #
• New! Extract Superclass refactoring d D #
New! Extract Interface refactoring d D #
• New! Move Members refactoring d D #
• D2007 Import Namespace and better Undoing
• Examples… UEB: demos/threads/thrddemo.exe
43/66
Refactoring und DUnit
Das Testen mit DUnit kann an Refactoring
koppeln und setzt auf drei Elementen auf:
• Das eigentliche Testobjekt, fixture genannt
• Der Testfall zum Objekt, action genannt
• Das Resultat nach dem Testfall, check
genannt
Als einfaches Beispiel sei hier das fortlaufende Zählen
von Zeichen erwähnt (siehe practice). Das Resultat
kann Positiv, alphanumerisch oder ein unerwarteter
Fehler sein. Der Check testet, ob das Resultat dem
Referenzwert entspricht.
DEMO: review*.exe
44/66
DUnit Conditions
We should keep 3 things in mind:
- Test methods are parameter-less procedures.
- Test methods are declared published.
- Test methods generally map methods from an
application
But it's possible to set private methods which
can be used independent of an application, like
the function WaveFileCheck()!
45/66
Finding Testcases in DUnit
But finding meaningful testcases isn't that easy. As we grow with
experiences I will show few testcases to inspire and deliver some
ideas. Note that DUnit builds a separate instance of the class for
each method that it finds, so test methods cannot share instance
data. We dig into 4 methods:
1. Test Roboter for Forms
2. Test an Exception (like testing a disaster recovery)
3. Test the Fileformat to prevent Viruses, Hoaks and so on
4. Test a Reference Value
It's important to be a little familiar with DUnit. First we build the
testclass which will test our units: (Testing procedures should be
published so RTTI can find their method address with the
MethodAddress method.) Add an instance variable for every
fixture (i.e. starting situation like myForm, FFull...) you wish to
use.
46/66
Testcase 1
1. The following calls check() to see if everything
went OK. It's up to you to enlarge the check with
more calls or methods like a test roboter does.
procedure TTestCaseMethods.testFormRobot;
begin
myForm.lblserial.Caption:= 'another text test';
myform.ShowModal;
check(myForm is TForm, 'this isn’t right type');
end;
47/66
Testcase 2
2. The following example extends exception handling to do a
couple of tests which can prove exception handling works.
This case shows that it is possible to test the exceptions of
the test subject:
procedure TTestCaseMethods.testExceptionIndexTooHigh;
begin
try
fFull.Items[2];
//Check(false, 'should have been an EListError.');
except on E: Exception do
Check(not(E is EListError));
end;
end;
48/66
Testcase 3
3. At last a reference test in this case a function which returns a value,
so we can refactor as much we want as long the reference value is
still the same. This test checks with checkEquals() if the rate of an
income results the right value 1040. // When checkEquals fails, it
will end up in the TestResult as a failure (not reference value,
expected: <1040> but was: <1042>).
procedure TTestCaseMethods.testReferenceValue;
begin
incomeRef:= createIncome2;
incomeref.SetRate(4,1);
checkEquals(1040, incomeRef.GetIncome(1000), 'no
reference value');
incomeRef.FreeObject;
end;
49/66
Test Units in practice
50/66
Experiences armasuisse components
DEMO: magazin/dws
51/66
Patterns & Refactoring
with Factory Example
very highvery highFacadesync/async
very high
very high
middle
middle
high
middle
Platform
Independend
very highMediatorsync/async
middleCommandsync
yes (local or WAN)Chain of
Responsibility
sync/async
middleBridge, Observer,
MVC
sync
yes (WSDL)Abstract Factory,
Prototype
sync
Loose CouplingDesign PatternCommuni-
cation
52/66
Anti Flexibility ?
with TSQLConnection Bsp.
• Connection:= TSQLConnection.Create(NIL);
• with Connection do begin
• ConnectionName:= 'VCLScanner';
• DriverName:= 'INTERBASE';
• LibraryName:= 'dbexpint.dll';
• VendorLib:= 'GDS32.DLL';
• GetDriverFunc:= 'getSQLDriverINTERBASE';
• Params.Add(‚user_name=SYSDBA');
• Params.Add('Password=masterkey');
• with TWebModule1.create(NIL) do begin
• getFile_DataBasePath;
• Params.Add(dbPath);
53/66
Testability Code
• Exception handling
{$IFDEF DEBUG}
Application.OnException:= AppOnException;
{$ENDIF}
• Assert function
accObj:= TAccount.createAccount(FCustNo,
std_account);
assert(aTrans.checkOBJ(accObj), 'bad OBJ cond.');
• Logger
LogEvent('OnDataChange', Sender as TComponent);
LogEvent('BeforeOpen', DataSet);
54/66
Test Based on SEQ
55/66
Optimizations
Some tips and hints to optimize your code:
1. one statement and short var
2. assert call, IfThen()
3. use snippets
4. naming convention
5. $IF-directive
6. Const instead of var parameters
7. extract code from dfm
8. comment your code
9. uses list
56/66
Optimization – one and var
• one statement and with speed
An easy one states that only one statement should exists for every source
line, like next and put a var lifetime as short as possible
begin ..
I := 5; CallProc(I); //bad
//better
begin ..
I:= 5;
CallProc(I);
var Rec: TMyRec;
begin ..
with Rec do begin
.. title:= ´Hello Mars';
end;
end;
57/66
Optimization – assert and
IfThen()
assert call
Use assert calls as much, but don't forget the $C - setting is active or
not. This means that identifiers used in the Assert procedure call, will
be registered in your tests, but when compiled by Delphi, this code
line will be (should) stripped out if $C- is defined.
procedure MyProc(p: pointer);
begin
Assert(p <> NIL, ‘’);
nMax:= IfThen(nA > nB, nA, nB) // no if/then/else
58/66
Optimization – use code
snippets
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;
59/66
Optimization – naming
convention
TBitBtn;bitn
TButton;btn
TCheckBox;chk
TComboBox;cbo
TRadioButton;rb
TRadioGroup;rg .........
Ordinary types start with "T"
Exception types start with "E"
Pointer types tart with "P"
Interface types start with "I"
Class fields by properties (read/write) start with "F"
Method pointers start with "On/Before/After"
60/66
Optimization - $IF-directive and
const
In Delphi 6, the new $IF-directive was introduced. The $IF-directive is followed
by an expression, that evaluates to TRUE or FALSE. Differentiate your code!
Avoid var parameters that are used, but never set in the subprogram they
belong to. You may omit the var keyword, or change it to a const parameter.
Declare with the const directive, resulting in better performance since the
compiler can assume that the parameter will not be changed.
procedure MyHexMaxProc(const i: integer); //not var
begin ..
if i = 5 then // !! i is not set
begin .. end;
end;
by the way: use not more than 3 parameters in a method, so the compiler can
build it with fastcall directives, means registers are used instead of stack!
61/66
Optimization - extract code
from dfm
• Connection:= TSQLConnection.Create(NIL);
• with Connection do begin
• ConnectionName:= 'VCLScanner';
• DriverName:= 'INTERBASE';
• LibraryName:= 'dbexpint.dll';
• VendorLib:= 'GDS32.DLL';
• GetDriverFunc:= 'getSQLDriverINTERBASE';
• Params.Add('User_Name=SYSDBA');
• Params.Add('Password=masterkey');
• with TWebModule1.create(NIL) do begin
• getFile_DataBasePath;
• Params.Add(dbPath);
62/66
Optimization - comment your
code
1. Connection := TSQLConnection.Create(nil);
2. with Connection do begin ...
3. //after connection, create dataset to it
4. DataSet := TSQLDataSet.Create(nil);
5. with DataSet do begin ...
6. SQLConnection := Connection;
7. //build the command string
8. CommandText :=
9. Format('insert into KINGS values("%d","%s", "%s",
"%s","%s")',[10, Email,FName,LName, logdate]);
63/66
Optimization – uses list
We know the linker is a smart one, but nevertheless init and finals are
executed. Removing unused uses references has multiple benefits:
• less code to maintain, no need to bother about code that's not used
• code from initialization and finalization sections in unused units
is not linked in and run, reducing the size of the EXE
• compilation runs smoother and quicker
When you drop a VCL component on a Delphi form the Delphi IDE
automatically adds the unit or units required by the component to the interface
section uses statement. This is done to ensure that the form file (DFM/XFM)
can locate the code needed to stream the form and components. Even if you
later remove the component, the units are not deleted from the uses statement.
(UEB: which unit is not necessary in ThSort ?)
64/66
Expand your Refactoring
Problem:Problem: YouYou mustmust refactorrefactor toto makemake itit compatiblecompatible withwith
UnitUnit TestingTesting oror youyou mustmust createcreate a Unit Testa Unit Test ClassClass beforebefore
refactorrefactor !!!!
Refactoring in UML Diagrams (e. g.Refactoring in UML Diagrams (e. g. statestate -->> superstatesuperstate))
[Code Patterns]
[Syncedit]
[Macro recorder], [Code Completion], [Version Control]
Tools in practice:
„As far as I'm concerned, Castalia or Pascal Analyzer is a must have for any
Delphi developer - I know I'd be lost without it now."
65/66
Refactoring Links:
• Delphi 9…BDS3.0Help.pdf (1656 p.) ab S. 85
• Delphi 11 Tools: http://www.modelmakertools.com/
• Report Pascal Analyzer:
http://www.softwareschule.ch/download/pascal_analyzer.pdf
• Refactoring Martin Fowler (1999, Addison-Wesley)
• Changing the structure of code without changing the functionality
• http://www.refactoring.com
• Discussion site on code smells
• http://c2.com/cgi/wiki?CodeSmell
• Castalia3 - http://www.delphi-expert.com/castalia2/
• http://www.informatics.sussex.ac.uk/courses/sde/Course-
Outline/Lecture_slides/15/slides4.pdf
66/66
Q&A
max@kleiner.com
www.softwareschule.ch

More Related Content

What's hot

Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Session 7 code_functional_coverage
Session 7 code_functional_coverageSession 7 code_functional_coverage
Session 7 code_functional_coverageNirav Desai
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flowPushpa Yakkala
 
Integration Group - Lithium test strategy
Integration Group - Lithium test strategyIntegration Group - Lithium test strategy
Integration Group - Lithium test strategyOpenDaylight
 
How to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveHow to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveAmiq Consulting
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSubash John
 
Top five reasons why every DV engineer will love the latest systemverilog 201...
Top five reasons why every DV engineer will love the latest systemverilog 201...Top five reasons why every DV engineer will love the latest systemverilog 201...
Top five reasons why every DV engineer will love the latest systemverilog 201...Srinivasan Venkataramanan
 
Session 9 advance_verification_features
Session 9 advance_verification_featuresSession 9 advance_verification_features
Session 9 advance_verification_featuresNirav Desai
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesNirav Desai
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Raffi Khatchadourian
 
Verification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICsVerification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICsDr. Shivananda Koteshwar
 
Unit testing on embedded target with C++Test
Unit testing on embedded  target with C++TestUnit testing on embedded  target with C++Test
Unit testing on embedded target with C++TestEngineering Software Lab
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)Shaharyar khan
 
Python unit testing
Python unit testingPython unit testing
Python unit testingDarryl Sherman
 

What's hot (20)

Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Session 7 code_functional_coverage
Session 7 code_functional_coverageSession 7 code_functional_coverage
Session 7 code_functional_coverage
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flow
 
Integration Group - Lithium test strategy
Integration Group - Lithium test strategyIntegration Group - Lithium test strategy
Integration Group - Lithium test strategy
 
L06 process design
L06 process designL06 process design
L06 process design
 
Coverage and Introduction to UVM
Coverage and Introduction to UVMCoverage and Introduction to UVM
Coverage and Introduction to UVM
 
Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
 
How to Connect SystemVerilog with Octave
How to Connect SystemVerilog with OctaveHow to Connect SystemVerilog with Octave
How to Connect SystemVerilog with Octave
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
 
Top five reasons why every DV engineer will love the latest systemverilog 201...
Top five reasons why every DV engineer will love the latest systemverilog 201...Top five reasons why every DV engineer will love the latest systemverilog 201...
Top five reasons why every DV engineer will love the latest systemverilog 201...
 
Session 9 advance_verification_features
Session 9 advance_verification_featuresSession 9 advance_verification_features
Session 9 advance_verification_features
 
system verilog
system verilogsystem verilog
system verilog
 
Session 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfacesSession 8 assertion_based_verification_and_interfaces
Session 8 assertion_based_verification_and_interfaces
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Verification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICsVerification challenges and methodologies - SoC and ASICs
Verification challenges and methodologies - SoC and ASICs
 
Cpp unit
Cpp unit Cpp unit
Cpp unit
 
Unit testing on embedded target with C++Test
Unit testing on embedded  target with C++TestUnit testing on embedded  target with C++Test
Unit testing on embedded target with C++Test
 
What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)What is Java Technology (An introduction with comparision of .net coding)
What is Java Technology (An introduction with comparision of .net coding)
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 

Viewers also liked

Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Manual para la implementacion del sistema de gestion en seguridad y salud en ...
Manual para la implementacion del sistema de gestion en seguridad y salud en ...Manual para la implementacion del sistema de gestion en seguridad y salud en ...
Manual para la implementacion del sistema de gestion en seguridad y salud en ...coviso
 
Tvis viamedia
Tvis viamediaTvis viamedia
Tvis viamediaMediaPost
 
SAP Contabilidad
SAP ContabilidadSAP Contabilidad
SAP Contabilidadcoviso
 
Неизбежное. Информационная сверхпроводимость и авторское право
Неизбежное. Информационная сверхпроводимость и авторское правоНеизбежное. Информационная сверхпроводимость и авторское право
Неизбежное. Информационная сверхпроводимость и авторское правоVladimir Haritonov
 
Ley OrgĂĄnica de RĂŠgimen Tributario Interno
Ley OrgĂĄnica de RĂŠgimen Tributario InternoLey OrgĂĄnica de RĂŠgimen Tributario Interno
Ley OrgĂĄnica de RĂŠgimen Tributario InternoChristiian_p
 
Simbiosis empresarial-final-jit1
Simbiosis empresarial-final-jit1Simbiosis empresarial-final-jit1
Simbiosis empresarial-final-jit1Deysi Delgado Navidad
 
Лекция в «Новой газете»
Лекция в «Новой газете»Лекция в «Новой газете»
Лекция в «Новой газете»Vladimir Haritonov
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resourcelawexchange.co.uk
 
Открытый доступ к науке
Открытый доступ к наукеОткрытый доступ к науке
Открытый доступ к наукеVladimir Haritonov
 
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANO
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANOPLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANO
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANOinnovalabcun
 
Tipos de consultorĂ­a
Tipos de consultorĂ­aTipos de consultorĂ­a
Tipos de consultorĂ­aGabriela Cruz
 

Viewers also liked (17)

Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Manual para la implementacion del sistema de gestion en seguridad y salud en ...
Manual para la implementacion del sistema de gestion en seguridad y salud en ...Manual para la implementacion del sistema de gestion en seguridad y salud en ...
Manual para la implementacion del sistema de gestion en seguridad y salud en ...
 
Armando reverĂłn
Armando reverĂłnArmando reverĂłn
Armando reverĂłn
 
Tvis viamedia
Tvis viamediaTvis viamedia
Tvis viamedia
 
SAP Contabilidad
SAP ContabilidadSAP Contabilidad
SAP Contabilidad
 
Неизбежное. Информационная сверхпроводимость и авторское право
Неизбежное. Информационная сверхпроводимость и авторское правоНеизбежное. Информационная сверхпроводимость и авторское право
Неизбежное. Информационная сверхпроводимость и авторское право
 
Ley OrgĂĄnica de RĂŠgimen Tributario Interno
Ley OrgĂĄnica de RĂŠgimen Tributario InternoLey OrgĂĄnica de RĂŠgimen Tributario Interno
Ley OrgĂĄnica de RĂŠgimen Tributario Interno
 
Simbiosis empresarial-final-jit1
Simbiosis empresarial-final-jit1Simbiosis empresarial-final-jit1
Simbiosis empresarial-final-jit1
 
Лекция в «Новой газете»
Лекция в «Новой газете»Лекция в «Новой газете»
Лекция в «Новой газете»
 
Law-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared ResourceLaw-Exchange.co.uk Shared Resource
Law-Exchange.co.uk Shared Resource
 
Открытый доступ к науке
Открытый доступ к наукеОткрытый доступ к науке
Открытый доступ к науке
 
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANO
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANOPLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANO
PLAN GENERAL DE CONTABILIDAD PUBLICA COLOMBIANO
 
Tipos de consultorĂ­a
Tipos de consultorĂ­aTipos de consultorĂ­a
Tipos de consultorĂ­a
 

Similar to Refactoring_Rosenheim_2008_Workshop

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
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문Sangcheol Hwang
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing SoftwareSteven Smith
 
FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxEhtesham46
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projectsVincent Massol
 
Writing Well Abstracted Automation on Foundations of Jello
Writing Well Abstracted Automation on Foundations of JelloWriting Well Abstracted Automation on Foundations of Jello
Writing Well Abstracted Automation on Foundations of JelloDan Cuellar
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projectsVincent Massol
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?Gerald Villorente
 
Writting Better Software
Writting Better SoftwareWritting Better Software
Writting Better Softwaresvilen.ivanov
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approachEnrico Da Ros
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovypaulbowler
 
What’s expected in Spring 5
What’s expected in Spring 5What’s expected in Spring 5
What’s expected in Spring 5Gal Marder
 
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.All Things Open
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).pptaptechaligarh
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applicationsChandra Sekhar Saripaka
 
Mcknight well built extensions
Mcknight well built extensionsMcknight well built extensions
Mcknight well built extensionsRichard McKnight
 

Similar to Refactoring_Rosenheim_2008_Workshop (20)

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
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptx
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
Code review
Code reviewCode review
Code review
 
Writing Well Abstracted Automation on Foundations of Jello
Writing Well Abstracted Automation on Foundations of JelloWriting Well Abstracted Automation on Foundations of Jello
Writing Well Abstracted Automation on Foundations of Jello
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
New types of tests for Java projects
New types of tests for Java projectsNew types of tests for Java projects
New types of tests for Java projects
 
Why Drupal is Rockstar?
Why Drupal is Rockstar?Why Drupal is Rockstar?
Why Drupal is Rockstar?
 
Writting Better Software
Writting Better SoftwareWritting Better Software
Writting Better Software
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
DSL's with Groovy
DSL's with GroovyDSL's with Groovy
DSL's with Groovy
 
What’s expected in Spring 5
What’s expected in Spring 5What’s expected in Spring 5
What’s expected in Spring 5
 
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.
It’s 2021. Why are we -still- rebooting for patches? A look at Live Patching.
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Best practices in enterprise applications
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
 
Mcknight well built extensions
Mcknight well built extensionsMcknight well built extensions
Mcknight well built extensions
 

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
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Max Kleiner
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max 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 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax 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
 

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
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
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 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
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
 

Recently uploaded

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto GonzĂĄlez Trastoy
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

Refactoring_Rosenheim_2008_Workshop

  • 1. 1/66 Refactoring mit Delphi 2007 in Rosenheim Als Refactoring betrachtet man allgemein ein Vereinfachen, Verbessern und Stabilisieren einer bestehenden Codestruktur, welche keine Änderung auf das „beobachtbare“ Verhalten der Applikation und dessen Ergonomie bewirken soll. “Refactoring is improving the design of code after it has been written”
  • 2. 2/66 Agenda 10.10.2008 • What‘s Refactoring ? • How to recognize Bad Code and what‘s the goal ? • The Law of Demeter • Process of Refactoring • Techniques in Delphi 2007 • Introduction to DUnit • Experiences, Code Analysis and Optimisations • Refactoring and other Tools
  • 3. 3/66 Refactoring Context Advantage: automatisation and integration of new methods, better testability and readability (simplification) SourceSource improveimprove TestTest DUnitDUnit test test RefactoringRefactoring changechange UnitsUnits text text binary binary
  • 5. 5/66 Refactoring: What‘s all about ? Never touch a running system ?!: //System.Get just reads the contents of memory locations: Get(a, v); or v:= M[a]; o.IntToStr; or v:= IntToStr(o); GetMem(p, Count * SizeOf(Pointer)); or GetMem(FDataPtr, MemSize); If you can‘t see how to easily expand, test or maintain your code, it‘s probably time to refactor! - How to recognize bad smells ? - Refactor to change code, reduce to the Max! - Build Test Class and run test to confirm it‘s okay
  • 6. 6/66 Refactoring: Definition • Changes made to a program that only change its organization (structure), not its function. • Behaviour preserving program transformations. • Simplification: Your classes are small and your methods too; you’ve said everything and you’ve removed the last piece of unnecessary code.
  • 7. 7/66 When and why Refactoring ? 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 • Test after each step • Each method should do one thing • Modify not only structure but also code format
  • 8. 8/66 When to refactor ? • If a new feature seems hard to implement, refactor. • If a new or changed feature created some ugly code, refactor. • When you can’t stand (understand) to look at your code, refactor. UEB: 8_pas_verwechselt.txt
  • 9. 9/66 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. • Lets you worry about generality tomorrow; • Estimation of the value (capital) of code! • Necessary for beautiful software.
  • 10. 10/66 When Refactoring I ? Bad Coordination • Single Instance with Singleton • 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 UEB: 14_pas_primetest.txt
  • 11. 11/66 When Refactoring II ? Bad Functionality (Organisation) • General Code Smoke Test (compilation) failed • Comments, Coding Conventions (D. by Contract) • Type safety (pointers?), Declare, RTTI, Thread Safeness • Exception Handling, Resource Leaks (memory) • Import Statements (uses at runtime) • Functions and Bindings (design time) • No Bug Fixes & Release Planning • No Versioning & System Handbook UEB: 15_pas_designbycontract.txt
  • 12. 12/66 When Refactoring III ? Bad Structure • General Code Size (in module) • Coupling (between classes or units) • Cyclic Dependency, Declare+Definition, ACD-Metric • Cohesion (in classes) • Layers, Tiers and Control Structures (runtime) • Interfaces or Packages (design & runtime) • Static, Public or Private (inheritance or delegate) • Too big classes or units? UEB: 10_pas_oodesign_solution.txt
  • 13. 13/66 Top Ten of Bad Smells 1. Duplicated or Dead Code 2. Long Method 3. Large Class (Too many responsibilities) 4. Long Parameter List (Object is missing) 5. Case Statement (Missing polymorphism) 6. Divergent Change (Same class changes differently depending on addition) 7. Shotgun Surgery (Little changes distributed over too many objects or procedures patterns missed) 8. Data Clumps (Data always use together (x,y -> point)) 9. Middle Man (Class with too many delegating methods) 10. Message Chains (Coupled classes, internal representation dependencies)
  • 14. 14/66 Refctor/Review Checklist 1. Standards - are the Pascal software standards for name conventions being followed? 2. Bugs - Are the changes generally correct? 3. Are the Requirements Well Understood (Multilang)? 4. Are all program headers completed? 5. Are changes commented appropriately? 6. Does documentation use Correct Grammar? 7. Are release notes Clear? Complete? 8. Installation Issues, Licenses, Certs. Are there any? 9. Version Control, Are output products clear? 10.Test Instructions - Are they any? Complete?
  • 15. 15/66 Precondition & Goals… • Dokus, Testklassen zu Algorithmen • Vorabversionen und Updates • Installation, Support, Hotline der Firma • Volle VerfĂźgbarkeit Ăźber den Sourcecode • Compilationsfähigkeit erstellt ($D, $L, tdb32info) • Benchmarks als Performancekriterium • Know-how Transfer durch Entwickler • Tools und Review Kriterien vorhanden • Sprache und Form bei Dokumenten festlegen • Referenzarchitektur und Patterns bekannt
  • 16. 16/66 Modularisierung • Patterns Suche, Test mit einem Dependency Analyzer Suchen nach einem Schichtenmodell (MVC etc.) • Ist das Programmdesign optimal gewählt? – Zu viele globale Variablen? – Schlecht aufgeteilte Methoden bzw. Klassen? – Redundante Codesequenzen? Modularisierung (zur design- oder runtime ?) TWebModule1 = class(TWebModule) HTTPSoapDispatcher1: THTTPSoapDispatcher; HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker; WSDLHTMLPublish1: TWSDLHTMLPublish; DataSetTableProducer1: TDataSetTableProducer;
  • 17. 17/66 Modules of Classes Large classes • More than seven or eight variables • More than fifty methods • You probably need to break up the class Components (Strategy, Composite, Decorator) Inheritance TWebModule1 = class(TWebModule) HTTPSoapDispatcher1: THTTPSoapDispatcher; HTTPSoapPascalInvoker1: THTTPSoapPascalInvoker; WSDLHTMLPublish1: TWSDLHTMLPublish; DataSetTableProducer1: TDataSetTableProducer;
  • 18. 18/66 Extendibility Are Interfaces, Packages available? type { Invokable interfaces must derive from IInvokable } IVCLScanner = interface(IInvokable) ['{8FFBAA56-B4C2-4A32-924D-B3D3DE2C4EFF}'] function PostData(const UserData : WideString; const CheckSum : DWORD) : Boolean; stdcall; procedure PostUser(const Email, FirstName, LastName : WideString); stdcall;
  • 19. 19/66 Extendibility II Are Business Objects available (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”.
  • 20. 20/66 Modules and Extendibility Are Factory Patterns available ? Shows the use of a simple factory method to generate various reports or forms: 1. Register a list with objects 2. Generate objects at runtime 3. A report or business-rule has to be done 4. The objectlist is independent from GUI, GUI calls the list and the list is expandable “Use collections or lists, not case of or if statements.” UEB: 55_pas_factorylist.txt
  • 21. 21/66 Wartbarkeit • Namenskonvention, Namensräume, Kommentare • Bridge zwischen Interface und Implementation (siehe auch Modularisierung) • Installation und Configuration Guide durchsehen • Abhängigkeiten der Module klären: for i:= 0 to SourceTable.FieldCount - 1 do DestTable.Fields[i].Assign(SourceTable.Fields[i]); DestTable.Post;
  • 22. 22/66 Wartbarkeit II • die Dokumentation, insbesondere die exakte Spezifikation von Schnittstellen (Interfaces) • die lokale Verständlichkeit von Anweisungen resp. von Kommentar im Code • das Vermeiden von Duplicated, Dead Code • das Vermeiden globaler Variablen • die Parametrisierbarkeit von Methoden • in das Programm eingebaute PrĂźfungen der Annahmen, die man Ăźber Zustände hat (Assertions) • ein mĂśglichst großer Umfang von automatisch ausfĂźhrbaren Tests fĂźr das System
  • 23. 23/66 Wartbarkeit III • die Dokumentation, insbesondere die Verständlichkeit von Modulen (Komponenten) Program MailingLabels //pseudo code referenzieren! Begin ask the user for the name of the list read that list into memory ask the user how the list should be sorted: name or zip code sort the list ask the user about where to send the output: screen or printer print the labels End. { Program MailingLabels }
  • 24. 24/66 Testbarkeit (White Box) • Es lassen sich beim Ändern einer Subroutine schnell mal die davon abhängigen Prozeduren aufzeigen (z.B. Bindings Report). Die Änderung einer Funktion hat Einfluß auf die davon abhängigen Funktionen, die im Bindings Report aufgelistet werden, demzufolge ein erneuter Test der Funktion der Qualität nur zugute kommt. • Viele haben ja ein eigenes, „automatisiertes“ Verfahren entwickelt, um Code auf Fehler hin zu durchsuchen (Testprotokolle). • Wurde das Error Handling, Logger getestet ? • Sind Testklassen wie DUnit im Einsatz?
  • 25. 25/66 Testability – We should avoid: 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 • Class with too many delegating methods • Coupled classes UEB: 33_pas_cipher_file_1.txt
  • 26. 26/66 Software Safeness I • Avoid pointers as you can • Ex. of the win32API: pVinfo = ^TVinfo; function TForm1.getvolInfo(const aDrive: pchar; info: pVinfo): boolean; // refactoring from pointer to reference function TReviews.getvolInfo(const aDrive: pchar; var info: TVinfo): boolean; “Each pointer or reference should be checked to see if it is null. An error or an exception should occur if a parameter is invalid.”
  • 27. 27/66 Software Safeness II • Check Exception Handling function IsInteger(TestThis: String): Boolean; begin try StrToInt(TestThis); except on EConvertError do result:= False; else result:= True; end; end;
  • 28. 28/66 Fehleranalyse und Refactor Relevanz FehlerTyp-Nr / Bezeichnung / Verteilung / Relevant 10 Dokumentation - Kommentare, 0.5, Ja 20 Syntax - Syntax-Fehler (vor dem Kompilieren), 49.3, Nein 30 Build – package library, version control, 15.4, Ja 40 Assignment - Dekl., Doppelte Bezeichnungen, 8.8, Ja 50 Interface - Prozedur-Aufrufe und Referenzen, I/O, 1.5, Ja 60 Logging - Fehler-Nachrichten, Checks, 2.2, Ja 70 Daten - Struktur, Inhalt, Format, 1.5, Ja 80 Funktion - Schleifen, Rekursionen, Logik, 16.6, Ja/Nein 90 System – Konfiguration 1.5, Ja 100 Umgebung - Absturz, Compiler, Support, 3.7, Ja
  • 29. 29/66 Law of Demeter You should avoid: • Large classes with strange members • More than seven or eight variables • More than fifty methods • You probably need to break up the class Components in (Strategy, Composite, Decorator) 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:
  • 30. 30/66 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
  • 31. 31/66 Demeter Test as SEQ UEB: 56_pas_demeter.txt
  • 32. 32/66 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)
  • 33. 33/66 Let‘s practice • 1 • 11 • 21 • 1211 • 111221 • 312211 • ??? Try to find the next pattern, look for a rule or logic behind !
  • 34. 34/66 Before R. function runString(Vshow: string): string; var i: byte; Rword, tmpStr: string; cntr, nCount: integer; begin cntr:=1; nCount:=0; 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
  • 35. 35/66 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
  • 36. 36/66 Testfunction function teststring(vstring: string): integer; var a, b, i: integer; begin for i:= 1 to length(vstring) do begin a:= (ord(vstring[i])) b:= b + a; end result:= b end; Check with a reference value: if teststring(charCounter(RUN2)) = 578 then … //CheckEquals(578, teststring(charCounter(RUN2)))
  • 37. 37/66 Refactoring Techniken LĂśschen einer Klasse mit ReferenzenSafe DeleteModell Getter- und Setter einbauenEncapsulate FieldsClass Ersetze vererbte Methoden durch Delegation in innere Klasse Replace Inheritance with Delegation Component Erzeuge Referenzen auf Klasse mit Referenz auf implementierte Schnittstelle Use InterfaceInterface Aus Methoden ein Interface erzeugenExtract InterfaceInterface Heraustrennen einer CodepassageExtract MethodClass Ersetzen eines Ausdrucks durch einen Methodenparameter Introduce ParameterClass Aus Methoden, Eigenschaften eine Oberklasse erzeugen und verwenden Extract SuperclassClass Verschieben eines PackagesMove PackagePackage Umbenennen eines PackagesRename PackagePackage BeschreibungRefactoring FunktionEinheit
  • 38. 38/66 Consider the Options The tool should provide options. Whenever you rename certain entities, a tool should automatically replace all relevant identifiers in code in order to keep code consistent Each refactoring operation has its own set of constraints! • How to treat extracted code in original method – you can choose between • Leave – leaves the original text unchanged,activates the new method. • Select – leaves the original text unchanged and selects it (does not activate the new method). • Comment – puts comment braces around the original text and activates new method. • Remove – removes the original code and activates new method. • copy the vars to the new method, • add the new class to the same unit as the source method's class (if any).
  • 39. 39/66 Constraints in Delphi • If an error results from a refactoring, the engine cannot apply the change. For example, you cannot rename an identifier to a name that already exists in the same declaration scope. If you still want to rename your identifier, you need to rename the identifier that already has the target name first, then refresh the refactoring. • You can also redo the refactoring and select a new name. The refactoring engine traverses parent scopes, searching for an identifier with the same name. If the engine finds an identifier with the same name, it issues a warning.
  • 40. 40/66 Refactoring with D9 If you only select by install win32 refactor won't work!! • Delphi 2005 provides following refactoring operations: • Symbol Rename (Delphi, C#) • Extract Method (Delphi) • Declare Variable and Field (Delphi) • Sync Edit Mode (Delphi, C#) • Find References (Delphi, C#) • Extract Resource string (Delphi) • Find unit /import Namespace(Delphi) • Undo (Delphi, C#) • Examples…
  • 41. 41/66 Refactoring in D9 SP3 Corrections done • When extracting methods involving DWords, they are getting redeclared as Integers. • When the parameter has the same name as the type, 'Find local Reference' and 'rename parameter' fail • Rename refactoring fails on overloaded procedures with at least one parameter of the same name. • After using ExtractMethod, dragging a code snippet from the Tools palette causes a stack overflow. • Rename for components with event handlers doesn't work for VCL and VCL.Net apps after you save the project
  • 42. 42/66 Refactoring with D11 Delphi 2007 (since2006) provides following new refactoring operations: • New! Introduce Variable refactoring d D # New! Introduce Field refactoring d D # • New! Inline Variable refactoring d D # • New! Change Parameters refactoring d D # • New! Safe Delete refactoring d D # • New! Push Members Up / Down refactoring d D # New! Pull Members Up refactoring d D # • New! Extract Superclass refactoring d D # New! Extract Interface refactoring d D # • New! Move Members refactoring d D # • D2007 Import Namespace and better Undoing • Examples… UEB: demos/threads/thrddemo.exe
  • 43. 43/66 Refactoring und DUnit Das Testen mit DUnit kann an Refactoring koppeln und setzt auf drei Elementen auf: • Das eigentliche Testobjekt, fixture genannt • Der Testfall zum Objekt, action genannt • Das Resultat nach dem Testfall, check genannt Als einfaches Beispiel sei hier das fortlaufende Zählen von Zeichen erwähnt (siehe practice). Das Resultat kann Positiv, alphanumerisch oder ein unerwarteter Fehler sein. Der Check testet, ob das Resultat dem Referenzwert entspricht. DEMO: review*.exe
  • 44. 44/66 DUnit Conditions We should keep 3 things in mind: - Test methods are parameter-less procedures. - Test methods are declared published. - Test methods generally map methods from an application But it's possible to set private methods which can be used independent of an application, like the function WaveFileCheck()!
  • 45. 45/66 Finding Testcases in DUnit But finding meaningful testcases isn't that easy. As we grow with experiences I will show few testcases to inspire and deliver some ideas. Note that DUnit builds a separate instance of the class for each method that it finds, so test methods cannot share instance data. We dig into 4 methods: 1. Test Roboter for Forms 2. Test an Exception (like testing a disaster recovery) 3. Test the Fileformat to prevent Viruses, Hoaks and so on 4. Test a Reference Value It's important to be a little familiar with DUnit. First we build the testclass which will test our units: (Testing procedures should be published so RTTI can find their method address with the MethodAddress method.) Add an instance variable for every fixture (i.e. starting situation like myForm, FFull...) you wish to use.
  • 46. 46/66 Testcase 1 1. The following calls check() to see if everything went OK. It's up to you to enlarge the check with more calls or methods like a test roboter does. procedure TTestCaseMethods.testFormRobot; begin myForm.lblserial.Caption:= 'another text test'; myform.ShowModal; check(myForm is TForm, 'this isn’t right type'); end;
  • 47. 47/66 Testcase 2 2. The following example extends exception handling to do a couple of tests which can prove exception handling works. This case shows that it is possible to test the exceptions of the test subject: procedure TTestCaseMethods.testExceptionIndexTooHigh; begin try fFull.Items[2]; //Check(false, 'should have been an EListError.'); except on E: Exception do Check(not(E is EListError)); end; end;
  • 48. 48/66 Testcase 3 3. At last a reference test in this case a function which returns a value, so we can refactor as much we want as long the reference value is still the same. This test checks with checkEquals() if the rate of an income results the right value 1040. // When checkEquals fails, it will end up in the TestResult as a failure (not reference value, expected: <1040> but was: <1042>). procedure TTestCaseMethods.testReferenceValue; begin incomeRef:= createIncome2; incomeref.SetRate(4,1); checkEquals(1040, incomeRef.GetIncome(1000), 'no reference value'); incomeRef.FreeObject; end;
  • 51. 51/66 Patterns & Refactoring with Factory Example very highvery highFacadesync/async very high very high middle middle high middle Platform Independend very highMediatorsync/async middleCommandsync yes (local or WAN)Chain of Responsibility sync/async middleBridge, Observer, MVC sync yes (WSDL)Abstract Factory, Prototype sync Loose CouplingDesign PatternCommuni- cation
  • 52. 52/66 Anti Flexibility ? with TSQLConnection Bsp. • Connection:= TSQLConnection.Create(NIL); • with Connection do begin • ConnectionName:= 'VCLScanner'; • DriverName:= 'INTERBASE'; • LibraryName:= 'dbexpint.dll'; • VendorLib:= 'GDS32.DLL'; • GetDriverFunc:= 'getSQLDriverINTERBASE'; • Params.Add(‚user_name=SYSDBA'); • Params.Add('Password=masterkey'); • with TWebModule1.create(NIL) do begin • getFile_DataBasePath; • Params.Add(dbPath);
  • 53. 53/66 Testability Code • Exception handling {$IFDEF DEBUG} Application.OnException:= AppOnException; {$ENDIF} • Assert function accObj:= TAccount.createAccount(FCustNo, std_account); assert(aTrans.checkOBJ(accObj), 'bad OBJ cond.'); • Logger LogEvent('OnDataChange', Sender as TComponent); LogEvent('BeforeOpen', DataSet);
  • 55. 55/66 Optimizations Some tips and hints to optimize your code: 1. one statement and short var 2. assert call, IfThen() 3. use snippets 4. naming convention 5. $IF-directive 6. Const instead of var parameters 7. extract code from dfm 8. comment your code 9. uses list
  • 56. 56/66 Optimization – one and var • one statement and with speed An easy one states that only one statement should exists for every source line, like next and put a var lifetime as short as possible begin .. I := 5; CallProc(I); //bad //better begin .. I:= 5; CallProc(I); var Rec: TMyRec; begin .. with Rec do begin .. title:= ´Hello Mars'; end; end;
  • 57. 57/66 Optimization – assert and IfThen() assert call Use assert calls as much, but don't forget the $C - setting is active or not. This means that identifiers used in the Assert procedure call, will be registered in your tests, but when compiled by Delphi, this code line will be (should) stripped out if $C- is defined. procedure MyProc(p: pointer); begin Assert(p <> NIL, ‘’); nMax:= IfThen(nA > nB, nA, nB) // no if/then/else
  • 58. 58/66 Optimization – use code snippets 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;
  • 59. 59/66 Optimization – naming convention TBitBtn;bitn TButton;btn TCheckBox;chk TComboBox;cbo TRadioButton;rb TRadioGroup;rg ......... Ordinary types start with "T" Exception types start with "E" Pointer types tart with "P" Interface types start with "I" Class fields by properties (read/write) start with "F" Method pointers start with "On/Before/After"
  • 60. 60/66 Optimization - $IF-directive and const In Delphi 6, the new $IF-directive was introduced. The $IF-directive is followed by an expression, that evaluates to TRUE or FALSE. Differentiate your code! Avoid var parameters that are used, but never set in the subprogram they belong to. You may omit the var keyword, or change it to a const parameter. Declare with the const directive, resulting in better performance since the compiler can assume that the parameter will not be changed. procedure MyHexMaxProc(const i: integer); //not var begin .. if i = 5 then // !! i is not set begin .. end; end; by the way: use not more than 3 parameters in a method, so the compiler can build it with fastcall directives, means registers are used instead of stack!
  • 61. 61/66 Optimization - extract code from dfm • Connection:= TSQLConnection.Create(NIL); • with Connection do begin • ConnectionName:= 'VCLScanner'; • DriverName:= 'INTERBASE'; • LibraryName:= 'dbexpint.dll'; • VendorLib:= 'GDS32.DLL'; • GetDriverFunc:= 'getSQLDriverINTERBASE'; • Params.Add('User_Name=SYSDBA'); • Params.Add('Password=masterkey'); • with TWebModule1.create(NIL) do begin • getFile_DataBasePath; • Params.Add(dbPath);
  • 62. 62/66 Optimization - comment your code 1. Connection := TSQLConnection.Create(nil); 2. with Connection do begin ... 3. //after connection, create dataset to it 4. DataSet := TSQLDataSet.Create(nil); 5. with DataSet do begin ... 6. SQLConnection := Connection; 7. //build the command string 8. CommandText := 9. Format('insert into KINGS values("%d","%s", "%s", "%s","%s")',[10, Email,FName,LName, logdate]);
  • 63. 63/66 Optimization – uses list We know the linker is a smart one, but nevertheless init and finals are executed. Removing unused uses references has multiple benefits: • less code to maintain, no need to bother about code that's not used • code from initialization and finalization sections in unused units is not linked in and run, reducing the size of the EXE • compilation runs smoother and quicker When you drop a VCL component on a Delphi form the Delphi IDE automatically adds the unit or units required by the component to the interface section uses statement. This is done to ensure that the form file (DFM/XFM) can locate the code needed to stream the form and components. Even if you later remove the component, the units are not deleted from the uses statement. (UEB: which unit is not necessary in ThSort ?)
  • 64. 64/66 Expand your Refactoring Problem:Problem: YouYou mustmust refactorrefactor toto makemake itit compatiblecompatible withwith UnitUnit TestingTesting oror youyou mustmust createcreate a Unit Testa Unit Test ClassClass beforebefore refactorrefactor !!!! Refactoring in UML Diagrams (e. g.Refactoring in UML Diagrams (e. g. statestate -->> superstatesuperstate)) [Code Patterns] [Syncedit] [Macro recorder], [Code Completion], [Version Control] Tools in practice: „As far as I'm concerned, Castalia or Pascal Analyzer is a must have for any Delphi developer - I know I'd be lost without it now."
  • 65. 65/66 Refactoring Links: • Delphi 9…BDS3.0Help.pdf (1656 p.) ab S. 85 • Delphi 11 Tools: http://www.modelmakertools.com/ • Report Pascal Analyzer: http://www.softwareschule.ch/download/pascal_analyzer.pdf • Refactoring Martin Fowler (1999, Addison-Wesley) • Changing the structure of code without changing the functionality • http://www.refactoring.com • Discussion site on code smells • http://c2.com/cgi/wiki?CodeSmell • Castalia3 - http://www.delphi-expert.com/castalia2/ • http://www.informatics.sussex.ac.uk/courses/sde/Course- Outline/Lecture_slides/15/slides4.pdf