Вы находитесь на странице: 1из 8

• Na9sini tsawer

• na9sini des example


• n7ot l planmta3i


• la principale raison de créer de nouvelles routines au lieu de laisser tout le code dans une grosse
routine était d'éviter le code en double.
• reduce a program’s complexity.
• routine is an individual method or procedure sinvocable for a single purpose :: example
methode in java
• les point negative :
■ The routine has a bad name. HandleStuff() tells you nothing about what the rou-
tine does.
■ The routine isn’t documented.
■ bad physical organization of the code on the page
■ The routine’s input variable, inputRec, is changed. If it’s an input variable, its
value should not be modified (and in C++ it should be declared const). If the
value of the variable is supposed to be modified, the variable should not be
called inputRec.
■ The routine reads and writes global variables—it reads from corpExpense and
writes to profit. It should communicate with other routines more directly than
by reading and writing global variables.A Low-Quality Routine
163
■ The routine doesn’t have a single purpose. It initializes some variables, writes to
a database, does some calculations—none of which seem to be related to each
other in any way. A routine should have a single, clearly defined purpose.
■ The routine doesn’t defend itself against bad data. If crntQtr equals 0, the expres-
sion ytdRevenue * 4.0 / (double) crntQtr causes a divide-by-zero error.
■ Some of the routine’s parameters are unused: screenX
■ The routine has too many parameters.
■ The routine’s parameters are poorly ordered and are not documented.
(na7ki 3ala l machakel ili l9inahom fel methode w ba3dika nebda na3ti betarf betarf kifeh nhel )
1. importance of routines:

• makes programs easier to read


• easier to understand
• saving space and improving performance. (kif n9asmou l classes mta3na l des methode w kol
methode tkoun 3andha haja ta3mal faha akeka chn9awiw fel perfermance s f nitham mta3 lk
code mta3na w bech tna7kilna l redendence mtta3 l code )
and it’s a crime to abuse this senior statesman of computer science with code like that in the example
just shown.

1. OK! I already know that routines are great, so what do you want me to do about it?
2. Valid Reasons to Create a Routine
• Reduce complexity
• minimizing code size
• hide information hat you won’t need to think about it (you should be able to forget the details
and use the routine without any knowledge of its internal workings.) nroudou belna but without
the abstractive power of routines,
malazemnach n5aliyouha abstract de telle facons el methode bech t5ali l code mta3na as3ab w akthar
complexité
• Reduce the containing routine’s complexity: Each routine should do ONE thing and do it
well

ki tel9ana ne5dem fi algorithme wala 9a3din ndevelopiyou w kol matra nest7a9ou ena ne9elbou chaine
chnou na3mlou nesn3ou methode reversmethode w kol mara n3aytioulha 5ir meli kol mara nektbouh l
code awal 7aja china9as 3lina t3ab wel l ktiba el code mta3nabech youn lesible akthar w mafhoum 7ata
kif na9raweh (n7awel n7ot example )

• Avoid duplicate code


En effet, la création de code similaire dans deux routines implique une
erreur de décomposition. Extrayez le code en double des deux routines, mettez une version
générique du code commun dans une classe de base, puis déplacez les deux routines
spécialisées diviser en sous-classes.
• reduce the chance of error in subclass implementations ???

• Modifications will be easier : La modification plus faciles car vous devrez modifier le code
dans un seul emplacement.
• Support subclassing yo9sed beha surcharge mta3 l methode mta3na
kifeh l methode y3awena fel overide subclass ? Ena nest3mlou des methode bien factoriser fel overide
fel les classe fils 5ir meli neesta3mlou code yebda compliqué
• Hide sequences :
methode max(x,y) ykoun 3andi methode max_In_Arbre lena chnesta3mal max(x’y) maghir
manest7a9 eni na3rafha hiya kifeh te5dem

• lazem el routines ykoun 3zndhz a good name


• Hide pointer operations lena generalement les operation de pointeur ykounou m3a9din s3ab
donc mel mousta7san ena n7otouhom w n5abiyouhom f les methode w betari9a hethi nrakzou
3al but l asli enti nhebou ne5dmouh fel methode mta3na
asert verifiier

• Simplify complicated boolean tests


• Improve performance
l perfermance mta3 l code ili 9a3din nektbou fi bech teber kif nesta3lou les methode
nesn3 methode eli bech najam nesta3malha fel code lkol ()centraliser
• To ensure all routines are small?
Laken hatha mayemna3ch enou sa3at l 5edma tkoun plus perfermant ki ne5dmouha fi routine
twila (dima 3ana des cas particulier)
Operations That Seem Too Simple to Put Into Routines
parmis les chose ili yemn3ouna ena n9asmou lcode l des methode houwa ena nraw el methode
presque mahi ta3mal fi chay
or 7aseb l experience mta3ou 9al bel3aks l 9ad matkoun el methode mta3na simle w te3mel ken fi haja
pressi 9ad ma5ir lina
• Small routines offer several advantages
• more readable

EXMPLE :

points = deviceUnits * ( POINTS_PER_INCH / DeviceUnitsPerInch() )

VS

Function DeviceUnitsToPoints( deviceUnits: Integer ) Integer;


if ( DeviceUnitsPerInch() <> 0 )
DeviceUnitsToPoints = deviceUnits *( POINTS_PER_INCH /DeviceUnitsPerInch())
else
DeviceUnitsToPoints = 0
end if
End Function
//***************
points = DeviceUnitsToPoints( deviceUnits )

==>>Si cette ligne de code originale avait encore été dans une douzaine d’endroits, le test aurait été
répété une douzaine de fois, pour un total de 36 nouvelles lignes de code. Une routine simple réduite
les 36 nouvelles lignes à 3.
7.2 Design at the Routine Level
• do one thing well and not do anything else: High cohesion is when you have a class that does
a well defined job.
Bech nothemnou tkoun 3ana cohésion 9wiya lezem l role ta3 l methode ykoun 3ala haja
wahda

public void setName(String name)


{
this.name = name;
}

public void setAge(int age)


{
this.age = age;
}

//*********** VS

public void getInformation(String name , String age)


{
this.age = age;
this.name = name;
}

comparaison :
• 50 percent of the highly cohesive routines with out error
• 18 percent of routines with low cohesion with out error
• error in routines with low cohesion cost 20 times to fix

cohesion are normally considered to be less than ideal:


• Sequential cohesion:
operations in order
share data
◦ (e type hethi les operation ili fih synchriniser by order w kol ooperation tnajam tes7a9 lel
resulta mta3 l operation lo5ra )
◦ example : mauvaise example

void CalculeAgeRetraite(Date naissance )


{
int age = 2018-naissance.year();
retraite= 60-age;
System.out.println(“age : ”+age+” temps reste pour le retraite ”+retraite);

}
// donc l methode hethi 9a3da te5dem f 2 role calcule age et la retraite

• Communicational cohesion:

Вам также может понравиться