Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet189/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   185   186   187   188   189   190   191   192   ...   384
Bog'liq
Clean Code

14
Successive Refinement
Case Study of a Command-Line Argument Parser
This chapter is a case study in successive refinement. You will see a module that started
well but did not scale. Then you will see how the module was refactored and cleaned. 
Most of us have had to parse command-line arguments from time to time. If we
don’t have a convenient utility, then we simply walk the array of strings that is passed
into the 
main
function. There are several good utilities available from various sources,


194
Chapter 14: Successive Refinement
but none of them do exactly what I want. So, of course, I decided to write my own. I call
it:
Args
.
Args
is very simple to use. You simply construct the 
Args
class with the input argu-
ments and a format string, and then query the 
Args
instance for the values of the argu-
ments. Consider the following simple example:
You can see how simple this is. We just create an instance of the 
Args
class with two
parameters. The first parameter is the format, or 
schema,
string: 
"l,p#,d*
.
"
It defines three
command-line arguments. The first, 
–l
, is a boolean argument. The second, 
-p
, is an integer
argument. The third, 
-d
, is a string argument. The second parameter to the 
Args
constructor
is simply the array of command-line argument passed into 
main
.
If the constructor returns without throwing an 
ArgsException
, then the incoming
command-line was parsed, and the 
Args
instance is ready to be queried. Methods like
getBoolean
,
getInteger
, and 
getString
allow us to access the values of the arguments by
their names.
If there is a problem, either in the format string or in the command-line arguments
themselves, an 
ArgsException
will be thrown. A convenient description of what went
wrong can be retrieved from the 
errorMessage
method of the exception.

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   185   186   187   188   189   190   191   192   ...   384




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