Clean Architecture


Symptom 1: Accidental Duplication



Download 6,37 Mb.
Pdf ko'rish
bet46/259
Sana26.02.2022
Hajmi6,37 Mb.
#465587
1   ...   42   43   44   45   46   47   48   49   ...   259
Bog'liq
Clean Architecture A Craftsman’s Guide to Software Structure and Design by Robert C. Martin (z-lib.org)

Symptom 1: Accidental Duplication
63
That word “cohesive” implies the SRP. Cohesion is the force that binds 
together the code responsible to a single actor.
Perhaps the best way to understand this principle is by looking at the 
symptoms of violating it.
S y m p to m 1: Acc i d e n ta l D u pl i c ati o n
My favorite example is the 
Employee
class from a payroll application. It has 
three methods: 
calculatePay()

reportHours()
, and 
save()
(Figure 7.1).
Figure 7.1 
The 
Employee
class
This class violates the SRP because those three methods are responsible to 
three very different actors.

The 
calculatePay()
method is specified by the accounting department, 
which reports to the CFO.

The 
reportHours()
method is specified and used by the human resources 
department, which reports to the COO.

The 
save()
method is specified by the database administrators (DBAs), 
who report to the CTO.
By putting the source code for these three methods into a single 
Employee
class, the developers have coupled each of these actors to the others. This 
www.EBooksWorld.ir


Chapter 7 SRP: The Single Responsibility Principle
64
coupling can cause the actions of the CFO’s team to affect something that the 
COO’s team depends on.
For example, suppose that the 
calculatePay()
function and the 
reportHours()
function share a common algorithm for calculating non-
overtime hours. Suppose also that the developers, who are careful not to 
duplicate code, put that algorithm into a function named 
regularHours()
(Figure 7.2).
Figure 7.2 
Shared algorithm
Now suppose that the CFO’s team decides that the way non-overtime hours 
are calculated needs to be tweaked. In contrast, the COO’s team in HR does 
not want that particular tweak because they use non-overtime hours for a 
different purpose.
A developer is tasked to make the change, and sees the convenient 
regularHours()
function called by the 
calculatePay()
method. 
Unfortunately, that developer does not notice that the function is also called 
by the 
reportHours()
function.
The developer makes the required change and carefully tests it. The CFO’s 
team validates that the new function works as desired, and the system is 
deployed.
Of course, the COO’s team doesn’t know that this is happening. The HR 
personnel continue to use the reports generated by the 
reportHours()
function—but now they contain incorrect numbers. Eventually the problem is 
discovered, and the COO is livid because the bad data has cost his budget 
millions of dollars.
www.EBooksWorld.ir



Download 6,37 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   259




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish